Tutorial: Array Variables 2

From GECK
Jump to: navigation, search

This is is the second part of a tutorial on array variables, the first part can be found here.

Retrieving and Storing Elements

If you know under what key a value is stored you can look it up directly like this:

let somevar := somearrayvar[key]   ; note the brackets

applied to the array examples in the previous tutorial chapter:

let rSomeCellForm := CellMap[-9]                              
   ; * rSomeCellForm is now the MojaveDriveIn cell

let iSomeInt := CharacterStringMap["Age"]
   ; * iSomeInt is now 27

let fSomeFloat := CharacterStringMap["Time played (hrs)"]    
   ; * fSomeFloat is 106.5

let rSomeCellForm := CharacterStringMap["Cells visited"][0] 
   ; * rSomeCellForm is now GSDocMitchellHouse (the first cell held as value in the regular array under the "Cells visited" key in our character stringmap)

let sv_somestring := NukaColaArray[2]                         
   ; * sv_somestring now refers to a copy of the "You can't beat the real thing." string stored in our array.

You don't strictly have to pass the values from an array to a local script variable everytime you want to do something with them; if you optionally use the Script Compiler Override, you can make this work:

let fSomeFloat := 3 * (somearrayvar[key] - iSomeInt) ; if the value held in that array is a number
someRef.call someUDF fSomeFloat somearrayvar[key]    ; if the UDF expects a variable type like what's in that array as second parameter

and a variety of other combinations like that, but if using the override, it's up to you to test what works and what doesn't.

And you add or replace elements to an array like this:

 
let somearrayvar[index] := something

let NukaColaArray[2] := "Everything goes better with Nuka." 
                        ; replaces the old string as value, which is destroyed if no longer referred to by anything else.
let NukaColaArray[2] := sv_someStringVar
                        ; same, just different
let CellMap[-18] := GoodSprings
                        ; adds the GoodSprings exterior cell's form under index -18
let CharacterStringMap["Race"] := PlayerRef.GetRace
                        ; adds the Hispanic race form under the "Race" key because that's what she is
let CharacterStringMap["Cells visited"][1] := GoodSprings    
                        ; adds the Goodsprings exterior cell as the second element (key = 1) of the array held under the "Cells Visited" key in our stringmap

Note that there are times when letting an array element to the result of a function directly, like with the race example, can crap out on you and you need to store it to a local script var first. Don't expect GetItemCount to be reliable for instance. Also, yes- whether you're retrieving from or storing to a stringmap, you can use string vars in place of the actual key string.

No matter in what order you add elements to an array, it'll automatically sort keys itself in ascending order - numerically for regular arrays and maps, alphabetically for stringmaps:

let somearray[1] := value1           
let somearray[0] := value0          

;  0     value0  
;  1     value1          

let somestringmap["B"] := valueB
let somestringmap["A"] := valueA

; will be stored as
;  A    valueA
;  B    valueB

Populating Arrays Quickly

Doesn't take a genius to tell that sticking stuff in your arrays like this:

let somearrayvar[index] := Value

Can be something of a typing chore if you need to put a lot of elements in it, and it also takes up precious script lines. We have a few shortcuts though.

For regular arrays, you can use the Ar_List function, which lets you create an array with up to 20 elements in a single line, usually separated by commas:

let somearrayvar := ar_List value1, value2, value3, value4, ... value20

let NukaColaArray := ar_List NukaCola, 2, "You can't beat the real thing.", RecipesInvolvingNukaColaArrayVar

The order in which you add the elements will be how the array is indexed (from 0 to 19).

0   NukaCola
1   2
2   "You can't beat the real thing."
3   RecipesInvolvingNukaColaArrayVar

Ar_list is an array-returning function, so it also immediately initializes the array you "let" it to.


If the elements you want to add to a regular array are a range of integers, consider the Ar_Range function:

let someArrayVar := Ar_Range FirstInt LastInt StepInt   ; StepInt is optional, if you leave it out it's assumed to be 1

let someArrayVar := Ar_Range 1 9 2   
    ; * is the same as: let someArrayVar := Ar_List 1, 3, 5, 7, 9

let someArrayVar := Ar_Range 1 4   
    ; * is the same as: let someArrayVar := Ar_List 1, 2, 3, 4


For maps and stringmaps, you can use the Ar_Map function, again with up to 20 elements you define, but of course the keys can be anything so you need to define them for each element yourself, associating each key with the value by using a double colon:

let somemap := ar_Map someKeyNumber::someValue someKeyNumber::someValue someKeyNumber::someValue ... the20thKeyNumber::the20thValue

let somestringmap := ar_Map someKeyString::someValue someKeyString::someValue ... the 20thKeyString::the20thValue

let CellMap := ar_Map -21::MojaveOutpost -9::MojaveDrivein 7::188TradingPost 12::BoulderCity 17::Bittersprings

let CharacterStringmap := ar_Map "Name"::"Prudencia" "Age"::27 "Body"::"T6M" "Profession"::"Test character" "Time Played (hrs)"::106.5 "Cells visited"::CellsVisitedArray

Maps and stringmaps will again auto-sort themselves in ascending order when you're done.


Part 3: Inspecting Arrays


External Links