Jump to content
  • 0

How to use a variable as part of an array name?


mgm

Question

HI guys,

Need some scripting help please!

I learned how to create String variables, with a "variable part" in the variable name, like this: UnitID01, UnitID02, using format [%1] and a variable at the end of the code block.

Now I would like to do the same with arrays. I am trying to save some playerUIDs in a series of arrays just like this:

Array_SU27ACL = [76561197978409269,74554545656546459,71232334197856645,72345435345345349];
The 27 is based on a variable, let's call it "_currentID"...

The next time we call the SQF script, at the top of the page it will increase the index value _currentID by one.

Therefore when it reaches the array creation code and it should then create this:

Array_SU28ACL =[76561197978409269,74554545656546459,71232334197856645,72345435345345349];
However when I try the same format[] trick, it creates a properly named variable but of "string" type, rather than array.

How can I do this please?

(By the way, in the subsequent lines I would like to manipulate that freshly-created array; e.g.: if X or Y pushback someValue to Array_SU28ACL. If other conditions are met perhaps remove something or add another array element etc. etc.

How am I supposed to 'address' this variable-named array since at the time of writing SQF code I don't know what the run-time ID would be - hope this makes sense).

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

You can also create/retrieve dynamic variable names using this method (I find this preferable):

arrayOfValues1 = [0,1,2]; //original array defined somewhere
arrayOfValues2 = [2,2,2]; //original array defined somewhere
arrayOfValues3 = [3,2,2]; //original array defined somewhere

_arrayIndex = 2; //We want only the second array of values
_variableValue = missionNamespace getVariable [format ["arrayOfValues%1",_arrayIndex],[]]; //Return the wanted array, return empty array if it doesn't exist
Link to comment
Share on other sites

  • 0

 

If there is no way to avoid it, not that it's a bad thing, call compile formathttps://community.bistudio.com/wiki/call

 

Quite often there is a way to handle the arrays, whatever is less costly i guess :)

_n = 3;
call compile format ["var%1 = 0",_n];

Thanks for the quick response. 

 

For now, until I reach the capacity to come up with an alternative (better) way, I will take whatever works... To make this call compile format work could I have some more help please. I already have "call compile format" in a notepad here and I have been trying different variations but couldn't make it work so far...

 

 

 

Some details: mgmTfA_gvdb_PV_TfAGUJIDNumber <== this is the ID number that keeps getting an increment every time script is called.

mgmTfA_gvdb_PV_TfAGUJIDNumber = mgmTfA_gvdb_PV_TfAGUJIDNumber + 1;
publicVariable "mgmTfA_gvdb_PV_TfAGUJIDNumber";

Now I want to do this:

// Create a new array
_myTempArray27ACL = [];

// Grant access to totalOmniscience Group
_myTempArray27ACL = mgmTfA_staticgv_totalOmniscienceGroupTextStringArray;

// Grant access to job requestor
_myTempArray27ACL pushback mgmTfA_gv_pvs_requestorPlayerUID;

Here my goal is removing the 27 from the  _myTempArray27ACL and replacing it with something like _myTempArray%1ACL, mgmTfA_gvdb_PV_TfAGUJIDNumber so that I can write the code once and use happily ever after.

This is 2 minute job for strings but for arrays google didn't help much...

 

If I remove _myTempArray27ACL in the above 3 lines what exactly should I put in its place?

 

 

 

 

I tried this -of course it didn't work-:

call compile process format["mgmTfA_gvdb_PV_SU%1ACL = [];", _mgmTfA_gvdb_PV_TfAGUJIDNumber];
call compile process format["mgmTfA_gvdb_PV_SU%1ACL pushback %2;", _mgmTfA_gvdb_PV_TfAGUJIDNumber, mgmTfA_staticgv_totalOmniscienceGroupTextStringArray];
call compile process format["mgmTfA_gvdb_PV_SU%1ACL pushback %2;", _mgmTfA_gvdb_PV_TfAGUJIDNumber, mgmTfA_gv_pvs_requestorPlayerUID];
Link to comment
Share on other sites

  • 0

 

You can also create/retrieve dynamic variable names using this method (I find this preferable):

arrayOfValues1 = [0,1,2]; //original array defined somewhere
arrayOfValues2 = [2,2,2]; //original array defined somewhere
arrayOfValues3 = [3,2,2]; //original array defined somewhere

_arrayIndex = 2; //We want only the second array of values
_variableValue = missionNamespace getVariable [format ["arrayOfValues%1",_arrayIndex],[]]; //Return the wanted array, return empty array if it doesn't exist

 

Face thanks for this. I think I understand how this can help read values: it will either return first underlined block (the actual value) or the second underlined block (the empty block) - cool.

 

Can you also tell me how to write values? getVariables should be replaced with setVariable, and drop the second empty block [], is that all?

Link to comment
Share on other sites

  • 0

To be perfectly honest I'm having a bit of trouble understanding your situation due to tiredness, but I will try the best I can.

 

To answer directly, you can use setVariable in a similar way, as you said.

 

If you wanted to make a series of arrays with a number incrementing with each successive array, you could do something like this:

indexValue = 0;
arrayValues = [0,1,2]; //This is the initial array of data you're starting from
missionNamespace setVariable [format ["arrayValues%1",indexValue],arrayValues]; //Made a new array named arrayValues0 with [0,1,2] as contents

indexValue = indexValue + 1; //Later incremented the index value

missionNamespace setVariable [format ["arrayValues%1",indexValue],arrayValues]; //Made a new array named arrayValues1 with [0,1,2] as contents

Not sure if this is what you're looking to do. Also keep in mind that arrays are funny in that they're defined by reference (not sure how to word this out). So if you modify arrayValues0 after arrayValues1 is defined, arrayValues1 will be also modified since they're both referincing arrayValues.

 

To make a new independent array each time, simply add a +:

missionNamespace setVariable [format ["arrayValues%1",indexValue],+arrayValues]; //Made a new [independent] array named arrayValues1 with [0,1,2] as contents
Link to comment
Share on other sites

  • 0

To be perfectly honest I'm having a bit of trouble understanding your situation due to tiredness, but I will try the best I can.

 

To answer directly, you can use setVariable in a similar way, as you said.

 

If you wanted to make a series of arrays with a number incrementing with each successive array, you could do something like this:

indexValue = 0;
arrayValues = [0,1,2]; //This is the initial array of data you're starting from
missionNamespace setVariable [format ["arrayValues%1",indexValue],arrayValues]; //Made a new array named arrayValues0 with [0,1,2] as contents

indexValue = indexValue + 1; //Later incremented the index value

missionNamespace setVariable [format ["arrayValues%1",indexValue],arrayValues]; //Made a new array named arrayValues1 with [0,1,2] as contents

Not sure if this is what you're looking to do.

 

 

 

 

A little background info: I am trying to set up access control lists "per job". content of each array will be Steam64IDs of players. Filled with some examples, this is what I understand:

// Make a new array named arrayValues1 with [2 separate Steam64 IDs] as contents:
missionNamespace setVariable [format ["arrayValues%1",1],"76561198070011111","76561198070022222"];



// Now I want to add 1 more later on, can I use pushback here?
// If pushBack is not supported, I guess this below should work?



// First, read the array [in case it got changed since our last visit]
_tmpArray = missionNamespace getVariable [format ["arrayOfValues%1",1]];
_tmpArray = _tmpArray + "76561198070033333";
// Now re-write it with original contents PLUS the addition:
missionNamespace setVariable [format ["arrayValues%1",1],_tmpArray];

As per above - can I use pushBack? On KK's blog I read it is much faster for adding to the end of arrays and that's what I will be doing almost always with the above.

 

Another (and hopefully last) question is public availability of the data above. The reason I set up these ACLs is because clients will refer to the contents.

How are the clients going to access - do they need simply the exact same command? ==> _tmpArray = missionNamespace getVariable [format ["arrayOfValues%1",1]];

Last question - partB: Do I need to still 'publicVariable' each time I change any array or does 'namespace setvariable' mean publicvariable by default?

 

Thanks

Link to comment
Share on other sites

  • 0

PushBack will work for what you want it to do, note that it modifies the original array:

indexValue = 1;

//Create array named arrayValues1:
missionNamespace setVariable [format ["arrayValues%1",indexValue],["76561198070011111","76561198070022222"]]; //Note: I added array brackets around the ID numbers, don't forget this

_arrayValues = missionNamespace getVariable [format ["arrayValues%1",indexValue],[]]; //retrieve a reference to the current array, arrayValues1
indexValue = indexValue + 1; //Increment the index in preparation for the next array
missionNamespace setVariable [format ["arrayValues%1",indexValue],_arrayValues]; //Created new array arrayValues2, which now refers to arrayValues1. Note: use +_arrayValues instead of _arrayValues if you want to duplicate the array instead of referencing the old one.
_arrayValues pushBack "ThirdID"; //we added "ThirdID" to the array. arrayValues2 (as well as all references to the array) is now equal to ["76561198070011111","76561198070022222","ThirdID"]

publicVariable format ["arrayValues%1",indexValue]; //arrayValues2 is now public

Note that setVariable has an optional third parameter to make the variable public, but I don't think syncs with new clients, that is why I used publicVariable. Just keep in mind that publicVariable commands are considered priority traffic and may cause desync with larger amounts of connected players as well as variable size and complexity. Arrays are the most bulky variable types to send as public variables.

Link to comment
Share on other sites

  • 0

PushBack will work for what you want it to do:

indexValue = 1;

//Create array named arrayValues1:
missionNamespace setVariable [format ["arrayValues%1",indexValue],["76561198070011111","76561198070022222"]]; //Note: I added array brackets around the ID numbers, don't forget this

_arrayValues = missionNamespace getVariable [format ["arrayValues%1",indexValue],[]]; //retrieve a reference to the current array, arrayValues2
indexValue = indexValue + 1; //Increment the index in preparation for the next array
missionNamespace setVariable [format ["arrayValues%1",indexValue],_arrayValues]; //Created new array arrayValues2, which now refers to arrayValues1. Note: use +_arrayValues instead of _arrayValues if you want to duplicate the array instead of referencing the old one.
_arrayValues pushBack "ThirdID"; //we added "ThirdID" to the array. arrayValues2 (as well as all references to the array) is now equal to ["76561198070011111","76561198070022222","ThirdID"]

publicVariable format ["arrayValues%1",indexValue]; //arrayValues2 is now public

Note that setVariable has an optional third parameter to make the variable public, but I don't think syncs with new clients, that is why I used publicVariable. Just keep in mind that publicVariable commands are considered priority traffic and may cause desync with larger amounts of connected players as well as variable size and complexity. Arrays are the most bulky variable types to send as public variables.

Well, this is what I have been trying to do for the last 6 or so hours. Thanks a lot Face, much appreciated!

Link to comment
Share on other sites

  • 0

You guys are scaring the shit out of me.... go outside right now and play basketball.... or garden... hell BBQ if you want. just stop it! :)

Some people find peace and quiet in a good book or tending to their gardens. Other folks find solace in debugging code. Lol! Personally, the deeper I get into coding the more beautiful I find it to be.

Link to comment
Share on other sites

  • 0

AH YES, tis the life to spend 6 hours on something besides work!!!!!

yup. going to work tomorrow with the newly acquired knowledge, can't wait for coming back home to test this. the ability to make my variable names based some other variables. pure happiness.

 

here, this is me. (forgot my skype on when I read Face's post #8 above): http://media.giphy.com/media/J8Iqja8CXfMw8/giphy.gif

Link to comment
Share on other sites

  • 0
19:25:07 "[TfA] [init.sqf]  [DEV-DEBUG]          mgmTfA_gvdb_PV_TfAGUJIDNumber PEH has just been triggered on this client!		mgmTfA_gvdb_PV_TfAGUJIDNumber has been updated to: 1."
19:25:07 "[TfA] [init.sqf]  [DEV-DEBUG]          Here is the corresponding ACL (mgmTfA_gvdb_PV_SU1ACL)'s contents as a string (["76561198070011111","76561198070022222","76561198070033333","76561198070044444","76561198070055555","76561198070066666","76561198124251001"]). I will now work on this array and determine whether I am to track this SU..."
19:25:07 "traversing _suIDACLContents...	_n=(1)		_x=("76561198070011111")."
19:25:07 "traversing _suIDACLContents...	_n=(2)		_x=("76561198070022222")."
19:25:07 "traversing _suIDACLContents...	_n=(3)		_x=("76561198070033333")."
19:25:07 "traversing _suIDACLContents...	_n=(4)		_x=("76561198070044444")."
19:25:07 "traversing _suIDACLContents...	_n=(5)		_x=("76561198070055555")."
19:25:07 "traversing _suIDACLContents...	_n=(6)		_x=("76561198070066666")."
19:25:07 "traversing _suIDACLContents...	_n=(7)		_x=("76561198124251001")."

Now you tell me, is this not a thing of unmatched beauty. setVar getVar. my new fav. Once again thanks a lot Face!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...