Jump to content
Sign in to follow this  
Undeceived

Accessing array elements

Recommended Posts

Hello, can someone help me with that, please? :confused:

I have the following array:

Group = ["Ondrej","Marek","Ivan","Jay"];

Due to certain reasons, the elements HAVE to be in the "".

Ok, apart from that I have 4 AI units in the editor with the names Ondrej, Marek, Ivan and Jay.

I want these units to join the player according to the order they have in the array. So what I need is a "connection" between the array elements in the "" and the AI unit names... How can I (in a script) make Ondrej join the player first, Marek secondly, and so on?

Please keep in mind that the element order can be different each time the script launches. E.g.

Group = ["Ondrej","Jay","Marek","Ivan"];

Thanks a lot for your help!

Share this post


Link to post
Share on other sites

The 'select' function is what you want to use. In SQF you would do something like this:

Group = ["Ondrej","Jay","Marek","Ivan"];

// Group select 0; is "Ondrej"
// Group select 1; is "Jay"
// Group select 2; is "Marek"
// Group select 3; is "Ivan"

[Group select 0] joinSilent (group player); // adds "Ondrej" to player group
[Group select 1] joinSilent (group player); // adds "Jay" to player group
// etc...

Is this what you mean?

Share this post


Link to post
Share on other sites

not sure this is what your asking for, but simple iteration using a for loop

_num = count units group; 
for [{_i=0} {_i < _num} {_i = i + i}] do
{
[Group select _i] joinSilent (group player); 
};

Share this post


Link to post
Share on other sites

Hey, thanks for your answers.

The problem is that (Group select 0) is "Ondrej", not Ondrej (without the "").

Using [Group select 0] joinSilent player, would be the same as ["Ondrej"] joinSilent player; and this doesn't work. As the unit's name is Ondrej without the "", the join command has to be [Ondrej] joinsilent player;.

That is what I meant when I wrote that somehow I need a "connection" between the array elements in the "" and the AI unit names.

Share this post


Link to post
Share on other sites

I know it wouldn't be universal, but wouldn't a quick work around be to have either a switch or multiple if statements like so?:

Group = ["Ondrej","Jay","Marek","Ivan"];

for [{_i = 0} {_i < _num} {_i = i + i}] do {
_unit = [Group select _i];
if (_unit == "Ondrej") then {[Ondrej] joinSilent (group player)};
if (_unit == "Jay") then {[Jay] joinSilent (group player)};
if (_unit == "Marek") then {[Marek] joinSilent (group player)};
if (_unit == "Ivan") then {[ivan] joinSilent (group player)};
};

It would probably be more efficient to dynamically parse each unit object to remove quotation marks when found, but I don't know if that's possible with SQF...

Share this post


Link to post
Share on other sites

_dude = toArray(str(group select _i)

make a new array - place each character of _dude into that array. you cant then remove the "" or resize that array accordingly, then use toString to change it back

thats how i used Arma2netmysqlplugin returns - failing that CBA does the job

Share this post


Link to post
Share on other sites

You can try use call compile format. I am not sure about possible performance problems.

{call compile format["[%1] joinSilent (group player)",_x]} forEach ["Ondrej","Jay","Marek","Ivan"];

Share this post


Link to post
Share on other sites
You can try use call compile format. I am not sure about possible performance problems.

{call compile format["[%1] joinSilent (group player)",_x]} forEach ["Ondrej","Jay","Marek","Ivan"];

Thanks a lot, the call comply format did the trick! :yay: I have no idea what happens :D but it works.

I modified it a little bit so it works with the name of an array.

//Defining the array:
GrpArray = ["Ondrej","Marek","Ivan","Jay"];

//And then Later:
{call compile format["[%1] joinSilent (group player)",_x]} forEach GrpArray;

The units Ondrej, Marek, Ivan and Jay (which have their names without the "") now join the player in the order given in the array.

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  

×