Jump to content
Sign in to follow this  
Heeeere's johnny!

[FUNCTION] fn_arrayValue.sqf – get, set, add and remove array values by ID (string)

Recommended Posts

fn_arrayValues.sqf - v1.2

This script gives you the possibility to get, set, add and remove elements from and in an array by using unique IDs (strings) for each array element without having to worry about indexes. The logic behind it is totally simple.

TL;DR: I don't know if some people already created and use something like this, but either way, it's useful and took less than an hour to write.

Slightly longer version:
While developing some multiplayer missions now and then, I was moving data back and forth between client and server, sometimes using relatively large arrays. At some point, I found it annoying having to be careful about what value is at which index and when I somehow change the array, I had to double check again if the correct indexes are read and written.

Coming from object-oriented programming languages, I always found it not satisfactory being unable to create custom objects in SQF. I know there is a „pseudo OO“ framework by code34, but that's just too heavy for my needs. All I wanted was being able to have one thing where I can get, set, add and remove values without having to care about anything but identifiers, just like the getters and setters in an object. And that's the idea of this script.

This script MUST NOT be spawned, because it's got return values (no shit! ;)). Be aware of that when using remote execution.
Every ID has to be a string and can only exist once throughout the whole array.
Parameters and which are optional (or not) is described in the top of the script so I'll leave that out here.

All examples are assuming “someId†exists, except if mentioned differently.
//Creating a new array of this kind:
_array = [[], []];

//Getting a value:
_value = [_array, "someId"] call fnc_arrayValue;

//Setting a value:
_array = [_array, "someId", 123] call fnc_arrayValue;

//Adding a value (assuming “someId†does not exist):
_array = [_array, "someId", 123, true] call fnc_arrayValue;

//Removing a value:
_array = [_array, "someId", nil, false] call fnc_arrayValue;

//Removing a value, only if it is 3:
_array = [_array, "someId", 3, false] call fnc_arrayValue;

//Setting a value explicitly to nil, instead of removing it from the array:
_array = [_array, "someId", nil, true] call fnc_arrayValue;


Kudos to Killzone_Kid for his SQF to BBCode Converter.

It doesn't matter how much the array changes. You'll always get the desired value behind the same ID, independent from which index it currently has got.

Unfortunately, last time I checked, SQF does not allow arrays like this:

[123, , true]

which is why there has to be a value (e.g. nil) even if you want to delete something from the array.

If you did not pass the “isAdd†parameter, hence accidently try to set a value for an ID which does not exist, for instance due to a typo, the value won't be set. Some might consider it a feature if it would, some might not. I personally like my scripts in a way that only very few things happen implicitly, but I'd like to know what you think about that.

Download the file here (SpiderOak)

Share this post

Link to post
Share on other sites

Update to v1.2


params made it to into the stable branch, so changed the script back to support it again.


Updated to v1.1

Version 1.0 was using the "params" command which is not yet available on the stable branch.

Share this post

Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now
Sign in to follow this