Home | SCI Documentation | SCI Tutorials | SCI Tools | SCI Wiki | Community

Chapter 11 - 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.

Manipulating Variables

To manipulate variables, you use operators. We'll start with the basic operators: addition (+), subtraction (-), multiplication (*), division (/) and assignment (=).

The assignment operator assigns a value to a variable.

Code:
= score 100

In this example, the variable named "score" is set to contain 100 as it's value.

The addition operator adds two values

Code:
=score (+ 100 20)

In this example, the variable named "score" is set to contain 120 as it's value (100 + 20).

The subtraction operator subtracts two values

Code:
= score (- 100 20)

In this example, the variable named "score" is set to contain 80 as it's value (100 - 20).

The multiplication operator multiplies two values

Code:
= score (* 100 20)

In this example, the variable named "score" is set to contain 2000 as it's value (100 * 20).

The division operator divides two values

Code:
= 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:
Code:

( 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:
Code:
= 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 further information, refer to the section in 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:
Code:
// 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 section in 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 accessable within the method or procedure which they are declared.

An example of declaring and using the variables:
Code:

( 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 further information, refer to the section in 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:
Code:

( 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 Objects Next: Chapter 12 - Methods and Procedures >
 

by helping to defray some of the costs of hosting this site. If it has been of help to you, please consider contributing to help keep it online.
Thank you.
pixe
Top

© 2013 to present The Sierra Help Pages. All rights reserved. All Sierra games, artwork and music © Sierra.