Difference between revisions of "SCI Programming Language/Data Types and Variables"

From SCI Wiki
Jump to navigationJump to search
 
(9 intermediate revisions by the same user not shown)
Line 30: Line 30:
  
 
===<br /> Variables ===
 
===<br /> Variables ===
<blockquote>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.
+
<blockquote>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: <nowiki># () , . @ [ ] ` " { - nul ^| ^J ^M</nowiki> 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).
+
 
 +
<div id="LocalVars"></div>'''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:
 
Local variables may be single variables:
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 45: Line 47:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
  
 
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:
 
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:
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 57: Line 61:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
  
 
Use the statement:
 
Use the statement:
  
[myArray n]
+
<blockquote><code>[myArray n]</code></blockquote>
  
 
to access element n of myArray. To access the fourth element of firstArray (the ten element array from the above example), write:
 
to access element n of myArray. To access the fourth element of firstArray (the ten element array from the above example), write:
  
[firstArray 3]
+
<blockquote><code>[firstArray 3]</code></blockquote>
  
 
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:
 
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:
  
 
+
<blockquote>
Page 10
 
 
 
 
 
 
 
 
 
 
 
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 84: Line 83:
 
       ...
 
       ...
 
)
 
)
 +
</syntaxhighlight>
 +
</blockquote>
  
</syntaxhighlight>
 
 
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:
 
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:
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 97: Line 98:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
  
 
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:
 
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)
+
<blockquote>
(= var1 [var2 2])
+
<code>(= var1 var4)<br /></code>
(= var1 [var3 1])
+
<code>(= var1 [var2 2])</code><br />
(= [var2 -1] [var1 3])
+
<code>(= var1 [var3 1])</code><br />
 +
<code>(= [var2 -1] [var1 3])</code>
 +
</blockquote>
  
 
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).
 
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:
+
<div id="GlobalVars"></div>'''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:
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 118: Line 123:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
  
 
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:
 
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:
  
 
+
<blockquote>
Page 11
 
 
 
 
 
 
 
 
 
 
 
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 136: Line 136:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
  
 
To access the seventh element of var2, write:
 
To access the seventh element of var2, write:
  
[var2 6]
+
<blockquote><code>[var2 6]</code></blockquote>
  
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.
+
<div id="TempVars"></div>'''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.
 
</blockquote>
 
</blockquote>
  
Line 147: Line 148:
 
<blockquote>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.
 
<blockquote>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.")
+
<blockquote><code>(Prints "This is immediate text.")</code></blockquote>
  
 
prints the text string within the quotes.
 
prints the text string within the quotes.
  
(= textToPrint "This text is referenced through a variable.")
+
<blockquote><code>(= textToPrint "This text is referenced through a variable.")</code></blockquote>
  
 
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).</blockquote>
 
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).</blockquote>
  
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:
+
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 '''[[SCI Programming Language/Definitions#define|define]]''' statement:
  
 
<blockquote><code>(define lotsOfText "This is a long text string. I am using a define statement to avoid having to type it repeatedly.")</code></blockquote>
 
<blockquote><code>(define lotsOfText "This is a long text string. I am using a define statement to avoid having to type it repeatedly.")</code></blockquote>
Line 170: Line 171:
  
 
===<br /> Characters ===
 
===<br /> Characters ===
 +
<blockquote>Characters are single ASCII characters, denoted by preceding the character with a reverse single quote (or tick). For example:
  
Characters are single ASCII characters, denoted by preceding the character with a reverse single quote (or tick). For example:
+
'A represents uppercase A<br />
 
 
'A represents uppercase A
 
 
'? represents the question mark
 
'? represents the question mark
  
 
Several character sequences represent special key combinations:
 
Several character sequences represent special key combinations:
  
'^a represents ctrl-A
+
'^a represents ctrl-A<br />
'@b represents alt-B
+
'@b represents alt-B<br />
'#4 represents the F4 key
+
'#4 represents the F4 key</blockquote>
 
 
  
 
===<br /> Literal selectors ===
 
===<br /> Literal selectors ===
Line 196: Line 195:
 
[[SCI Programming Language | Table of Contents]]
 
[[SCI Programming Language | Table of Contents]]
  
<span style="float: left">[[SCI Programming Language/Primitive Procedures/Introduction|&lt; Previous: Primitive Procedures]]</span>
+
<span style="float: left">[[SCI Programming Language/Primitive Procedures|&lt; Previous: Primitive Procedures]]</span>
 
<span style="float: right">[[SCI Programming Language/Definitions|Next: Definitions &gt;]]</span>
 
<span style="float: right">[[SCI Programming Language/Definitions|Next: Definitions &gt;]]</span>
  

Latest revision as of 22:49, 25 May 2016

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 >