Difference between revisions of "Introduction to OBSE arrays"
m
→Some final, miscellaneous notes: Punctuation correction
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: | ||
:::let MyArray := ar_Construct array | |||
:::let MyArray := ar_Construct map | |||
:::let MyArray := ar_Construct stringmap | |||
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) | ||
:::let SomeArray := ar_Construct array | |||
:::Let MyArray := SomeArray | |||
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: | ||
::let MyArray := ar_Construct array | |||
::Let MyArray[0] := 123 | |||
::Let MyArray[1] := 456 | |||
::Let MyArray[2] := 789 | |||
::let MyArray := ar_Construct map | |||
::Let MyArray[-1.5] := 123 | |||
::Let MyArray[2.5] := 456 | |||
::Let MyArray[3] := 789 | |||
::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 | ||
::float MyFloat | |||
::. . . | |||
::Let MyFloat := MyArray["PosX"] | |||
::Player.setpos x MyFloat | |||
::ref MyRef | |||
::. . . | |||
::Let MyRef := MyArray["ReferenceID"] | |||
::MyRef.Kill | |||
::string_var MyString | |||
::. . . | |||
::Let MyString := MyArray[3.7] | |||
::MessageBoxEX "The element text is: %z" MyString | |||
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: | ||
::string_var MyString | |||
::. . . | |||
::Let MyString := "Item value" | |||
::Let MyArray[MyString] := 300 | |||
::short MyShort | |||
::. . . | |||
::Let MyShort := 3 | |||
::Let MyArray[MyShort] := "Text" | |||
Line 275: | Line 264: | ||
ForEach loops iterate over the elements of an array. | ForEach loops iterate over the elements of an array. | ||
::array_var item | |||
::short MyShort | |||
::string_var MyString | |||
::. . . | |||
::ForEach item <- MyArray | |||
:::Let MyShort := item["key"] | |||
:::Let MyString := item["value"] | |||
:::MessageEX "The element %g is: %z" MyShort MyString | |||
::Loop | |||
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: | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := 0 | |||
::While MyIndex < ar_size MyArray | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
:::MyIndex += 1 | |||
::Loop | |||
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 | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := -1 | |||
::While (MyIndex += 1) < ar_size MyArray | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
::Loop | |||
While loops also allows for walking Arrays backward: | While loops also allows for walking Arrays backward: | ||
::short MyIndex | |||
::string_var MyString | |||
::. . . | |||
::Let MyIndex := ar_size MyArray | |||
::While (MyIndex -= 1) >= 0 | |||
:::Let MyString := MyArray[MyIndex] | |||
:::MessageEX "The element %g is: %z" MyIndex MyString | |||
::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. |