SCI Programming Language/Data Types and Variables

From SCI Wiki
Jump to: navigation, search

Official SCI Documentation

Chapter: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Index


Data Types and Variables
Author: Jeff Stephenson

 

Page 9


Numbers

All numbers in SCI are 16 bit integers with a range of -32768 to +32767. Numbers may be written as decimal (1024), hex ($400), or binary (%10000000000).


Variables

Variables hold numbers. Variables can be either global, local, or temporary, depending on when they are created and destroyed. The maximum variable name length is 2047 characters. SCI variables are case sensitive (i.e.: MyVariable!= myVARIABLE). A variable cannot begin with a number or a special character: # () , . @ [ ] ` " { - nul ^| ^J ^M space.


Local variables are created when a script is loaded and destroyed when it is purged. They are only available while the script is in memory and will not retain a value through a purge-reload cycle. Local variables may also be assigned an optional value at the time of definition (as in the following example: firstVar is assigned the value of 4).

Local variables may be single variables:

Code:
(local
   firstVar      = 4
   secondVar
   thirdVar
      ...
)

or defined as arrays. When defining an array, you have the option of assigning a value to the first element of the array only (the succeeding elements must be assigned their values in additional statements). Note that the brackets surrounding the array definitions do not mean "optional." They are required. Also note that the first element of an array is designated as element 0, the second as element 1, etc. In the following, firstArray is defined to have 10 elements (0 -9), the first element of which is assigned the value of 2:

Code:
(local
   [firstArray 10]      = 2
   [secondArray 5]
   [thirdArray 3]
      ...
)

Use the statement:

[myArray n]

to access element n of myArray. To access the fourth element of firstArray (the ten element array from the above example), write:

[firstArray 3]

Since each file may have only one local statement, that statement must include all the local variables used in that file. Therefore, the statement may contain both single and multiple (array) definitions:

Code:
(local
   [firstVar 5]
   secondVar
   thirdVar
   [fourthVar 3]
      ...
)

Despite the syntactic difference between single variable and local array declarations, SCI really makes no distinction between them. Think of the local declaration statement as a single array containing all the variables (including array elements) listed consecutively. Any variable may be accessed as an element of this "super-array," using any other variable as an index into the array. To clarify this concept, consider the following statement:

Code:
(local
   var1
   var2
   var3
   var4
)

Although these are all single variables, they are considered by SCI to be elements of a four element "super-array." Thus, the value of var1 can be set to that of var4 by any of the following statements:

(= var1 var4)
(= var1 [var2 2])
(= var1 [var3 1])
(= [var2 -1] [var1 3])

The first method is obviously the preferred method for clarity, but this array property of all variables allows access to variable numbers of parameters in a procedure (see section on procedures).

Global variables live for the duration of the entire game and are accessible to all scripts at all times. Thus they must be defined at the start of the game, either in room 0 or in a header file included by room O. The definition of a global variable includes its name followed by a unique index number to be used by the table of global variables. An optional value assignment is also permitted. In this example, firstVar has the index number 0 and contains the value 7:
Code:
(global
   firstVar  0      = 7
   secondVar 1
   thirdVar  2      = 20
      ...
)

The syntax for a global array differs slightly from that of a local array, though the philosophy of consecutive elements remains the same. To declare a global variable, leave an array-sized gap in the numbering sequence. In the following, var2 is defined as a global array of 10 elements:

Code:
(global
   varl	23
   var2	24
   var3	34
)

To access the seventh element of var2, write:

[var2 6]
Temporary variables are created when a procedure or method is entered and destroyed when it is left. Therefore they are only available to the declaring procedure and do not retain a value between calls to that procedure. Temporary variables are defined using the symbol &tmp. The discussion on temporary variables will be continued in the section on user-defined procedures.


Text

Text strings are strings of characters enclosed in double quotes, and may be used anywhere you like. Note that the older notation enclosed text within curly braces; this notation may still be seen in some code modules.
(Prints "This is immediate text.")

prints the text string within the quotes.

(= textToPrint "This text is referenced through a variable.")
sets the variable to the pointer to the text string. A text string may also be defined as the name property of an object (this concept will be clarified in the appendix on object-oriented programming).

When SCI goes to squirrel a text string away, it first checks to see if it has seen the string before. If so, it just uses the previous text, rather than duplicating it in another location. If you are using the same lengthy text string in several places, it is possible that you will not type the identical string in each case. Therefore, you can save yourself some trouble (and some memory) by putting the text into a define statement:

(define lotsOfText "This is a long text string. I am using a define statement to avoid having to type it repeatedly.")

This introduces another aspect of text strings. If text is too long fit on a single line, you may enter it on several lines. Multiple white-spaces (spaces, tabs, and new lines) get converted to a single space, so the text above ends up with just one space between the words on each line. If you want multiple spaces, enter them as underscores (_). These are converted to spaces in the string and are not compacted.

To include an underscore in the text, type \_ where \ (backslash) is the escape character. Explicit new lines (line feeds without carriage returns) are entered as \n (as they are in C). A CR/LF (carriage return/line feed) pair is entered as \r. Note that the \r should be used in place of \n in all strings destined for a file. Characters which are not on the keyboard, but are defined in a font can be included in a text string by preceding the character's two-digit hex value with the \. For example:

(Prints "This is the Sierra symbol: \01")

would put the value 1 at the end of the string, and this character in the default font is the Sierra symbol.

The maximum length of a text string is 2047 bytes.


Characters

Characters are single ASCII characters, denoted by preceding the character with a reverse single quote (or tick). For example:

'A represents uppercase A
'? represents the question mark

Several character sequences represent special key combinations:

'^a represents ctrl-A
'@b represents alt-B

'#4 represents the F4 key


Literal selectors

Selectors represent methods or properties (and will be further defined in the appendix on object-oriented programming). Placing a pound sign (#) in front of the selector will return the numeric value for use as a parameter. In the following example, the value of the selector showSelf is passed as a parameter to the method eachElementDo:
(cast eachElementDo: #showSelf)

Literal selectors are commonly used by Collection objects to pass a selector to each of their elements in turn.

Notes


Table of Contents

< Previous: Primitive Procedures Next: Definitions >