Spybots Smart Parts  
  The Unofficial Resource Centre for Lego Spybotic  
   
img  
   
   

TUTORIALS

TUTORIALS

Intermediate

constants, variables & COUNTERS

Tutorial Details:
Difficulty Level: Intermediate
Topics Covered: Declaration and use of constants, variables and counters in Mindscript and NQC.
Assumed Knowledge: The Basics, My First Program
Written By: BILL LANE

BACK

Constants, variables and Counters are containers that we use to hold things we might need to use. For example, when controlling the ARC lights we could refer to the first red light by using 0x01. Which is awkward and hard to remember. So the spybot.h header file creates a constant cRed1 and sets it equal to 0x01. Now whenever we want to refer to the first red light we can write cRed1. Which is much easier to remember than 0x01. We create a constant in Mindscript as follows:

const cRed1 = 0x01

We use a constant when we know the value won't change. For example, cRed1 will always equal 0x01. But sometimes we need to keep track of a value that may need to change. The most common example of this is the game score. In a game you may want to gain a point everytime you shoot something and lose a point everytime you run into something. To do this we would need to use a variable. We create a variable in Mindscript as follows:

var myVariable           //variable declared but not initialised
var myVaraible = 5     //variable declared and initialised

Here is the same example for NQC:

int myVariable;           //variable declared but not initialised
int myVaraible = 5;    //variable declared and initialised

N.B. You can declare more than one variable on a line in NQC by seperating them with a comma.

Variables can be either global or local. A global variable can be used and changed anywhere within your program. You make a variable global by declaring it at the program level. A local variable can only be used inside the code block in which they were defined. In NQC you declare a local variable in exactly the same manner as the global variable; it's the context of the declaration that makes it local or global. In Mindscript a local variable is declared by starting with the word local as well as it's context. For example:

program mytest
{

var myVariable = 5
       //this variable is global

main{
local myLocalVar = 1 
    //this variable is local
}

}

These examples just set a value for a variable. You can, of course, change the value of a variable using addition, subtraction, multiplication and/or division. You may also want to use variables to test for a certain situation. For example if the game score is equal to the required points then it's game over and your Spybot should dance it's victory dance.

Counters are a cross between constants and variables. A counter can be changed but it can only be incremented, decremented or cleared. Spybots have 3 counters which are named in spybot.h (if using Mindscript). The good thing about counters is that they can be used to generate events. For example, you could regularly update a counter (say every 10mS), then when it reaches a certain value use an event to end the game.

To refer to a counter in Mindscript you use it's name, e.g:

myVariable = nBioTick //nBioTick is a named counter from spybot.h

In NQC counters are referred to by their numerical position:

myVariable = counter (1); //values could be 0, 1 or 2

To increment a counter:

nBioTick +=1 //increment by 1 in Mindscript
IncCounter(1); //increment by 1 in NQC

To decrement a counter:

nBioTick -=1 //decrement by 1 in Mindscript
DecCounter(1); //decrement by 1 in NQC

To clear a counter:

clear nBioTick //clear counter in Mindscript
ClearCounter(1); //clear counter in NQC

That's all for this time. Constants, variables and counters all have their uses and their part to play in programming Spybots.

 

This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution.

 

 
 
DISCLAIMER: All content is provided as is, with no warranty stated or implied regarding the quality or accuracy of any content on or off this site. All trademarks, service marks, and copyrights are property of their respective owners. This site is not sponsored, authorized or sanctioned by the LEGO Group nor representative of their opinions in any way.PRIVACY POLICY