Difference between revisions of "SCI Studio Tutorial Chapter 11 - Variables"
m (1 revision) |
Latest revision as of 18:16, 5 August 2013
Variables
Like any other programming language, SCI contains variables. Variables are areas in memory used to store data which can be manipulated. Variables can be used for virtually anything. You can store numbers in them, characters, pointers to objects, strings, said blocks, and more.
There are five different types of variables in SCI games: local , global, parameter, temp and property.
- Local variables are declared in each script. They can be accessed by any of the methods or procedures within the same script.
- Global variables are similar to local variables, except they can be accessed by the methods and procedures of any script. These are infact the local variables of script.000 (main.sc). All of the local variables in main.sc can be accessed by any method or property in any of the 1000 possible scripts.
- Parameter variables are those which are sent to methods and procedures. These will be discussed in detail in Chapter 12.
- Temp variables operate like any other variable, but can only be accessed from within the method or procedure in which they are declared.
- Property variables are declared in each object. They can be accessed one of two ways. They can be accessed from within the object's methods by name, or from other objects and procedures using the send command.
Manipulating Variables
To manipulate variables, you use operators. We'll start with the basic operators: addition (+), subtraction (-), multiplication (*), division (/) and assignment (=).
Assignment
The assignment operator assigns a value to a variable.
Example:= score 100In this example, the variable named "score" is set to contain 100 as it's value.
Addition
The addition operator adds two values
Example:=score (+ 100 20)In this example, the variable named "score" is set to contain 120 as it's value (100 + 20).
Subtraction
The subtraction operator subtracts two values
Example:= score (- 100 20)In this example, the variable named "score" is set to contain 80 as it's value (100 - 20).
Multiplication
The multiplication operator multiplies two values
Example:= score (* 100 20)In this example, the variable named "score" is set to contain 2000 as it's value (100 * 20).
Division
The division operator divides two values
Example:= score (/ 100 20)In this example, the variable named "score" is set to contain 5 as it's value (100 / 20).
Declaring and Using Variables
Local & Global Variables
Local and global variables are set up in the local segment, as discussed briefly in chapter 9. The variables each begin with a name label to create an identifier for them. Following the label, you can optionally specify the array size if it will be used as an array, or the value if you wish to set it to something.
An example of declaring the variables:( local // a variable with no set value, so it will be set to 0 aVariable // A variable set to 30 anotherVariable = 30 // 30 word sized variables (60 bytes total), all set to zero anArray [30] // The six variables in the array are set, the rest will be zero aDeclaredArray [10] = (0 1 2 $ABCD 3 4 ) )
Examples of using the variables:= aVariable 100 Wait(aVariable) = anotherVariable (* aVariable 2) = anArray [ 0] 5 (for (= aVariable 0) (< aVariable 10) (++aVariable ) = anArray [ aVariable ]123 )The use of variables is very straight forward. For more information on the local segment, refer to the Variables section from the SCI Studio Help File.
Parameter Variables
Parameter variables are sent to methods and procedures when called, and can be accessed within them. The variables' labels are defined in the method or procedure's definition.
An example of declaring and using the variables:// two params are defined. The first named aParam, the second named anotherParam. (procedure (aProc aParam anotherParam) FormatPrint ("The first variable is: %d, the second is: %d." aParam anotherParam) )The "aProc" is the name of the procedure. The "aParam" and "anotherParam" are the names of the parameters. This is because the SCI language is derived from the "Lisp" language.
The use of variables is very straight forward. For further information, refer to the Procedures section from the SCI Studio Help File.
Temp Variables
Temp variables are allocated at the beginning of methods and procedures. They operate like any other variable, but are only accessible within the method or procedure which they are declared.
An example of declaring and using the variables:( procedure (aProc ) /* three variables are defined. The first named aVariable is not initialized, the second named anotherVariable is initialized to 30, the last named anArray is an uninitialized 30 variable array.*/ ( var aVariable , anotherVariable= 30, anArray [ 30]) = aVariable 100 Wait(aVariable ) = anotherVariable (* aVariable 2) = anArray [0] 5 (for (= aVariable 0 ) (< aVariable 10 ) (++aVariable) = anArray [aVariable] 123 ) )The use of variables is very straight forward. For more information on the local segment, refer to the Variables section from the SCI Studio Help File.
Property Variables
Property variables are the properties of classes and instances. When accessing them from within their owner object, they operate like any other variable. When accessing them from outside the class, the send operation is used (discussed in the previous chapter).
An example of declaring and using the properties within a class:( class aClass ( properties aProperty 123 anotherProperty 2456 ) ( method (someMethod ) = aProperty 100 Wait(aProperty ) = anotherProperty (* aProperty 2) ) )The use of variables is very straight forward. For further information, refer to the section in the SCI Studio help File.
That sums up the variables of a script. If you don't fully understand them yet, don't worry. Continue on with the tutorial, doing the step by step examples. When done, you should have a good grasp on them. If you still do not, come back to this chapter and read it again, and look at the links to the help file.
< Previous: Chapter 10 - Getting Familiar With ObjectsNext: Chapter 12 - Methods and Procedures >