Difference between revisions of "Introduction to OBSE arrays"

351 bytes removed ,  22:30, 13 May 2014
m
imported>8asrun6aer
m (Just displaying the toc for easier navigation)
imported>Syscrusher
m (→‎Some final, miscellaneous notes: Punctuation correction)
 
(2 intermediate revisions by one other user not shown)
Line 169: Line 169:
::When initializing an array with ar_Construct, the type of array must be provided as the argument:
::When initializing an array with ar_Construct, the type of array must be provided as the argument:


<dl><dd><dl><dd>
:::let MyArray := ar_Construct array
let MyArray := ar_Construct array
:::let MyArray := ar_Construct map
let MyArray := ar_Construct map
:::let MyArray := ar_Construct stringmap
let MyArray := ar_Construct stringmap
</dl></dl>




2. Assigning the value of another array_var to it
2. Assigning the value of another array_var to it
:: In this case MyArray will be of the same type of SomeArray (actually, both refer to the same array)
:: In this case MyArray will be of the same type of SomeArray (actually, both refer to the same array)
<dl><dd><dl><dd>
let SomeArray := ar_Construct array
Let MyArray := SomeArray
</dl></dl>


:::let SomeArray := ar_Construct array
:::Let MyArray := SomeArray


3. Assigning it the return value of a command returning an array such as GetItems.
<dl><dd><dl><dd>
Let MyArray := GetItems
</dl></dl>




3. Assigning it the return value of a command returning an array such as GetItems.
::Let MyArray := GetItems


==Populating arrays==
==Populating arrays==
Line 198: Line 192:
The keys, of course, must comply with the type of array:
The keys, of course, must comply with the type of array:


<dl><dd><dl><dd>
let MyArray := ar_Construct array
Let MyArray[0] := 123
Let MyArray[1] := 456
Let MyArray[2] := 789
</dl></dl>


<dl><dd><dl><dd>
::let MyArray := ar_Construct array
let MyArray := ar_Construct map
::Let MyArray[0] := 123
Let MyArray[-1.5] := 123
::Let MyArray[1] := 456
Let MyArray[2.5] := 456
::Let MyArray[2] := 789
Let MyArray[3] := 789
 
</dl></dl>
 
::let MyArray := ar_Construct map
<dl><dd><dl><dd>
::Let MyArray[-1.5] := 123
let MyArray := ar_Construct stringmap
::Let MyArray[2.5] := 456
Let MyArray["Cost"] := 123
::Let MyArray[3] := 789
Let MyArray["Qty"] := 456
 
Let MyArray["Taxes"] := 789
 
</dl></dl>
::let MyArray := ar_Construct stringmap
::Let MyArray["Cost"] := 123
::Let MyArray["Qty"] := 456
::Let MyArray["Taxes"] := 789




Line 226: Line 217:
The [[Let]] statement is array-aware and is the most common way of accessing array elements
The [[Let]] statement is array-aware and is the most common way of accessing array elements


<dl><dd><dl><dd>
 
float MyFloat
::float MyFloat
. . .
::. . .
Let MyFloat := MyArray["PosX"]  
::Let MyFloat := MyArray["PosX"]  
Player.setpos x MyFloat
::Player.setpos x MyFloat
</dl></dl>
 
<dl><dd><dl><dd>
 
ref MyRef
::ref MyRef
. . .
::. . .
Let MyRef := MyArray["ReferenceID"]  
::Let MyRef := MyArray["ReferenceID"]  
MyRef.Kill
::MyRef.Kill
</dl></dl>
 
<dl><dd><dl><dd>
 
string_var MyString
::string_var MyString
. . .
::. . .
Let MyString := MyArray[3.7]
::Let MyString := MyArray[3.7]
MessageBoxEX "The element text is: %z" MyString
::MessageBoxEX "The element text is: %z" MyString
</dl></dl>




In all examples up to here, keys were explicitly coded, but they may also be in a variable and the variable itself going within the brackets:
In all examples up to here, keys were explicitly coded, but they may also be in a variable and the variable itself going within the brackets:


<dl><dd><dl><dd>
 
string_var MyString
::string_var MyString
. . .
::. . .
Let MyString := "Item value"
::Let MyString := "Item value"
Let MyArray[MyString] := 300
::Let MyArray[MyString] := 300
</dl></dl>
 
<dl><dd><dl><dd>
 
short MyShort
::short MyShort
. . .
::. . .
Let MyShort := 3
::Let MyShort := 3
Let MyArray[MyShort] := "Text"
::Let MyArray[MyShort] := "Text"
</dl></dl>




Line 275: Line 264:
ForEach loops iterate over the elements of an array.
ForEach loops iterate over the elements of an array.


<dl><dd><dl><dd>
 
array_var item
::array_var item
short MyShort
::short MyShort
string_var MyString
::string_var MyString
. . .
::. . .
ForEach item <- MyArray
::ForEach item <- MyArray
    Let MyShort := item["key"]
:::Let MyShort := item["key"]
    Let MyString := item["value"]
:::Let MyString := item["value"]
    MessageEX "The element %g  is: %z" MyShort MyString
:::MessageEX "The element %g  is: %z" MyShort MyString
Loop
::Loop
</dl></dl>
 


At each iteration, "item" is initialized with two elements:  
At each iteration, "item" is initialized with two elements:  
Line 302: Line 291:


The same example as above using a While loop:
The same example as above using a While loop:
<dl><dd><dl><dd>
 
short MyIndex
::short MyIndex
string_var MyString
::string_var MyString
. . .
::. . .
Let MyIndex := 0
::Let MyIndex := 0
While MyIndex < ar_size MyArray
::While MyIndex < ar_size MyArray
    Let MyString := MyArray[MyIndex]
:::Let MyString := MyArray[MyIndex]
    MessageEX "The element %g  is: %z" MyIndex MyString
:::MessageEX "The element %g  is: %z" MyIndex MyString
    MyIndex += 1
:::MyIndex += 1
Loop
::Loop
</dl></dl>
 


Notice that with while loops, you have to increase the index yourself, as opposed to ForEach loops that automatically go to the next element.
Notice that with while loops, you have to increase the index yourself, as opposed to ForEach loops that automatically go to the next element.
Line 318: Line 307:


A somewhat different (perhaps more elegant) way of increasing the index
A somewhat different (perhaps more elegant) way of increasing the index
<dl><dd><dl><dd>
 
short MyIndex
 
string_var MyString
::short MyIndex
. . .
::string_var MyString
Let MyIndex := -1
::. . .
While (MyIndex += 1) < ar_size MyArray
::Let MyIndex := -1
    Let MyString := MyArray[MyIndex]
::While (MyIndex += 1) < ar_size MyArray
    MessageEX "The element %g  is: %z" MyIndex MyString
:::Let MyString := MyArray[MyIndex]
Loop
:::MessageEX "The element %g  is: %z" MyIndex MyString
</dl></dl>
::Loop
 




While loops also allows for walking Arrays backward:
While loops also allows for walking Arrays backward:
<dl><dd><dl><dd>
 
short MyIndex
 
string_var MyString
::short MyIndex
. . .
::string_var MyString
Let MyIndex := ar_size MyArray
::. . .
While (MyIndex -= 1) >= 0
::Let MyIndex := ar_size MyArray
    Let MyString := MyArray[MyIndex]
::While (MyIndex -= 1) >= 0
    MessageEX "The element %g  is: %z" MyIndex MyString
:::Let MyString := MyArray[MyIndex]
Loop
:::MessageEX "The element %g  is: %z" MyIndex MyString
</dl></dl>
::Loop
 




Line 347: Line 338:
*References cannot be used as array keys.
*References cannot be used as array keys.
*An array can contain any mix of types for its values.
*An array can contain any mix of types for its values.
*As array elements may contain any type of data, it is the script responsibility to know which is which, so you don't try to assign a string to a ref variable.
*As array elements may contain any type of data, it is the script's responsibility to know which is which, so you don't try to assign a string to a ref variable.
*OBSE keeps track of the number of references to each array and destroys the array when no references to it remain. This makes it unnecessary for scripts to worry about destroying arrays explicitly.
*OBSE keeps track of the number of references to each array and destroys the array when no references to it remain. This makes it unnecessary for scripts to worry about destroying arrays explicitly.
*ForEach and While loops both define structured blocks in the same way that If and Endif or Begin and End do. Every While or ForEach in a script must be matched by exactly one Loop command.
*ForEach and While loops both define structured blocks in the same way that If and Endif or Begin and End do. Every While or ForEach in a script must be matched by exactly one Loop command.
Anonymous user