The skill level of folks here on our forum varies widely, and many readers are beginners at programming. Could you give short description of what an array is and why a person might want to use such a thing, or perhaps a brief example of how it's a time-saver or problem solver?
Well basically an array is a bunch of values stored in single variable (pointer), you can have as many values in one array as you want. In many cases, when you need to work with many of the same (anything), arrays are great.
My English is not very good, so I'll just write some real examples.
1) Let's say we have a mod that delete items from inventory of dying critters (like stimpaks) to make game harder. And you want this configurable, so you add an INI config directive like this:
And checking if certain item would qualify for removal would be simple:
Code:
// do this once on script init:
// this is just a function that splits string and then converts all values to integers
pidsList := string_split_ints(get_ini_string(....), ",");
....
// use this in hs_ondeath to check if item qualify:
if (is_in_array(obj_pid(item), pidsList)) then ...
Without arrays we would've needed some dirty solutions like in party orders addon (where NPCs PIDs are defined on different lines, and you have to iterate over INI sections every time - this is a huge disk performance issue), or using some hard-to-understand functions to iterate over string of tokens using "tokenize" function.
2) Let's say we need to implement placable traps. Currently the engine not allows you to add spatial scripts dynamically (only through the mapper), so you need to use "list_begin, list_next" functions to iterate over all critters and check if someone should trigger the trap. How do you store the trap? You can use sfall globals, but since one global is just one value, you will have to limit maximum number of active traps to something like 4 or 5 and your script will have the same code repeated 5 times (repeated code is always HORRIBLE, as it leads to great many bugs and difficult to modify).
Arrays saves the day, as you can place as many of them (information about each trap - tile, elevation, status, trap type, etc.) in the array as you want, and easily save it to the savegame.
When you need to check if trap is triggered, you just iterate over all traps using "foreach" or "while" loop.
Benefits: Unlimited number of traps, less code (easy to modify).
Having arrays that work in expected way and safe to use, you will surely find many applications for them in your daily scripting (store NPCs PIDs to iterate over party members with ease, store object pointers with additional information to implement some new game mechanics, etc.).
What I also want to have is an
associative arrays. Normal arrays have specific length (number of values) and to access/change those values you use numeric indexes starting from 0. Those are called "lists" or "vectors". This behavior is good in many applications.
But, let's say you need to make it so some item PIDs may have some information associated with them (like price modifiers). Example:
Code:
; Klamath: Gecko Pelts cost much more, stimpacks less, leather armor less, scorpion tails more (illogical, but balanced)
2=276*2.5|277*2.5|623*2.0|40*0.7|3*0.8|74*0.8|92*2.0|591*2.0
In this case I specify that some items should cost less/more in Klamath. So how do I use this string? Associative arrays come in handy here:
Code:
pidList := some_parse_func(get_ini_string(...)); // this parses the string using string_split and creates array with 8 key => value pairs, where each listed item PID is associated with float price modifier
...
// modify prices like this (simplified pseude-code):
foreach ( pid in all_item_PIDs) begin
mod := get_assoc_array(pidList, pid);
if (mod) then
set_proto_data(pid, PRODATA_IT_COST, cost * mod);
end
I actually have function called "get_assoc_array", but what I want is to be able to use square brackets for this:
Which is currently not possible, because the compiler will simple translate this to:
Code:
mod := get_array(pidList, pid);
And get_array only works with normal arrays (lists), in which we have indexes 0, 1, 2 .... (and not 276, 277, ...).
I hope this is enough to understand that they are must-have in every script language
![Smile :) :)](/../../xencustomimages/smilies/icon_smile.gif)