Jump to content
Sign in to follow this  
[frl]myke

[Beginners guide]: Arrays

Recommended Posts

It's real difficult to say for certain what unit actually activated a trigger. triggerActivation won't do that for you either. It just lists what is being checked, not who or what actually activated it. The thislist I mentioned before would be as close as you can get as that's the list of units that might have set it off. There's no "this unit set this trigger" off command unfortunately (unless you specified it of course).

I imagine you're trying to do something like "if you leave this area you get teleported back to the middle" kind of thing? It's usually easier to do the reverse - "if you leave this area you get teleported back" since that way you can know it's the player triggering it.

Share this post


Link to post
Share on other sites

You're correct, unfortunately I have to make "precautions" like this due to someone I play with likes to not follow directions or ruin the intents of the mission. I have set up four triggers in a box formation, all set to run an .sqf file with the code "player setpos [(getpos..." running. It seems to work. Though your idea actually does sound better. I'm assuming that I could just create one trigger, and on the deact. line I put the same as I would use for the on act. line? My final end goal would be to make sure each unit of that side would be able to set the trigger off and make only that unit teleport back.

I'm thinking using an array with the names all the units, have a trigger count units in trigger area, and if one leaves it can bring the one missing from the array back into the trigger area.

Edited by BlackIce

Share this post


Link to post
Share on other sites

You could put this in your init.sqf and if any player gets more than 50m away from your center position they'll get teleported back to it. Of course change 50 to whatever you want for range.

centerPoint = getMarkerPos "centerPoint";
_trgStayInside = createTrigger ["EmptyDetector", [1,1,1]];
_trgStayInside setTriggerActivation ["NONE", "", true];
_trgStayInside setTriggerType "NONE";
_trgStayInside setTriggerStatements
["player distance centerPoint > [color="Red"]50[/color]",
"player setPos centerPoint",
""];

Share this post


Link to post
Share on other sites

When you take elements out of an array, do the remaining objects in the array keep their original index?

_sideX = ["WEST","EAST","Guerrila"];

_sideX = _sideX - ["WEST","Guerrila"];

would "EAST" keep the index of 1 or would it be at index 0 when the other 2 are taken out?

Do you have to set the index after you subtract elements?

Edited by Iceman77

Share this post


Link to post
Share on other sites

Sickboy code to select randomly from a dimensional nested array. It's very useful, so I'll share here.

_groupArray = [
              ["Infantry", ["RU_InfSquad","RU_InfSection","RU_InfSection_AT","RU_InfSection_AA","RU_InfSection_MG","RU_SniperTeam","RUS_ReconTeam","MVD_AssaultTeam"]],
              ["Motorized", ["RU_MotInfSquad","RU_MotInfSection_Recon","RU_MotInfSection_Patrol"]],
              ["Mechanized", ["RU_MechInfSquad_1","RU_MechInfSquad_2"]],
              ["Armored", ["RU_TankPlatoon"]]
             ];

_randomGroup = _groupArray select (floor(random(count _groupArray))); // Randomly select type category
_type = _randomGroup select 0; // Select type category name
_randomGroups = _randomGroup select 1; // Select available group types in the category
_randomGroupType = _randomGroups select (floor(random(count _randomGroups))); // Finally randomly select one of the group types.

_group = [getPos _spawnLocation, EAST, (configFile >> "CfgGroups" >> "East" >> "RU" >> _type >> _randomGroupType)] call BIS_fnc_spawnGroup;

Edited by Iceman77

Share this post


Link to post
Share on other sites

Since you're calling spawnGroup function, you have Functions loaded. Thus, you could use fnc_selectRandom to tidy it up a bit.

_randomGroup = _groupArray call BIS_fnc_selectRandom;
_type = _randomGroup select 0;
_randomGroups = _randomGroup select 1;
_randomGroupType = _randomGroups call BIS_fnc_selectRandom;

Share this post


Link to post
Share on other sites

I got a simple question about selecting a value within an array (I guess that's called subarray).

So for example I execute a script like this:

[player,[0.3,1]] execVM "somescript.sqf"

Let's assume the subarray is a skill level.

When I want to select a value within the skill-array it should look like this, right?

_skill = (_this select 1) select 0;

This would select 0.3 from the skill array.

//Edit:

Solution explained here, further down the post:

http://forums.bistudio.com/showthread.php?100559-Beginners-guide-Arrays&p=1652016&viewfull=1#post1652016

Edited by tryteyker

Share this post


Link to post
Share on other sites

Will this kind of statements work ?

_restypes =["Air","Static"];
_target = cursorTarget;
_targetype = typeOf _target;
While { !isNull _target && ({!(_targetype isKindOf _x)} forEach _restypes)} do { code lines .....};

Share this post


Link to post
Share on other sites

No, it won't (at least not the way you expect it to).

This will, however:

_restypes =["Air","Static"];
_target = cursorTarget;
_targetype = typeOf _target;
while { !isNull _target && ({_targetype isKindOf _x} count _restypes) == 0 } do { code lines .....};

Share this post


Link to post
Share on other sites

Hi,

I have some problem with following script. I have an array and I want to randomize 4 DIFFERENT elements (array has 20). I have a problem with substraction the once selected element from the array. I'm using this code:

_P1 = getMarkerPos "POST_01";
_P2 = getMarkerPos "POST_02";
_P3 = getMarkerPos "POST_03";

_candidateList = [_P1,_P2,_P3];
_candidateCount = count _candidateList;


_number1 = random (_candidateCount - 1);
_number1 = round _number1;
_post01 = _candidateList select _number1;

_number2 = random (_candidateCount - 1);
_number2 = round _number2;
if (_number1 == _number2) then {_number2 = _number2 + 1};
_post02 = _candidateList select _number2;

but this is a bad solution (it can random "select 4" and also its very weak if I want to random more elements).

I know i can substract an element from array, but it leaves a "null" inside and the array has still 20 (3 in code preview) elements. How to remove the element correctly, or how to randomize 4 DIFFERENT elements from an array in a better way?

Thanks in advance,

Share this post


Link to post
Share on other sites

I would stick with removing the element after it's selected other wise you will have to keep rechecking the number hasn't been taken.

The following script should work but instead of setting your variables post01,post2 ect A new array is created

_post = [] which should contain the markers in a random order, to access them just use (_post select 0) or (_post select 1) ect.

_P1 = getMarkerPos "POST_01";
_P2 = getMarkerPos "POST_02";
_P3 = getMarkerPos "POST_03";

_candidateList = [_P1,_P2,_P3];
_candidateCount = count _candidateList;
_post = [];

_number1 = 0;

for [{_i = 0},{_i < _candidateCount},{_i = _i + 1}] do {
 _number1 = floor random (count _candidateList); // gets current number of elements
   _post=_post + [_candidateList select _number1];// places selected marker name in post array
     _candidateList set [_number1,-1];// you cant delete a nested element/array so we replace it with a normal element that can 
       _candidateList = _candidateList -[_candidateList select _number1]; // removes selected element
   };

// used for debug only 
 hint str _post;
 sleep  2;

 hint str (_post select 0);

Share this post


Link to post
Share on other sites

Thanks, a lot it's exactly what I was looking for!

There's one thing I don't understand:

candidateList set [_number1,-1] what does it do and why after this I can remove it from array?

Thank You,

Share this post


Link to post
Share on other sites

I think the array looks like this [[array1],[array2],[array3]] and you can,t remove [array1] as it's a nested array, an array within an array.

What you do it replace [array1] or the array of your choice with -1 so it then looks like this [-1,[array2],[array3]] you can now remove -1 as it's just an element.

That's how I understand it any way.

Edited by F2k Sel

Share this post


Link to post
Share on other sites

I have the following problem:

After counting the amount of positions are in a house, I would like to apply a set of code for each house position.

To do that I figured I should transform that amount into an array with each entry representing a house position.

Like this:

Counting the house positions:

	_c = 0;
while { format ["%1", _x buildingPos _c] != "[0,0,0]" } do {_c = _c + 1};
_spawnlocs = _c;

_array = [an entry for each spawnloc]

Is that even a good approach?

Share this post


Link to post
Share on other sites

Hi,

I briefly skimmed through this thread but didn't notice a way of doing this..

Is there a way to check if a variable is an array? Or a way to get the type of the variable?

I see isArray but this seems to be for config entries. Basically I'm looking for a way to differentiate between a variable being of the String type versus the Array type..

EDIT: Ok I think I found a way for this in another discussion thread. Is this considered a good approach or is there a better/easier way?

if (isNil {_this select 1}) then { //not an array
   } else { }; //array

Edited by R3ality

Share this post


Link to post
Share on other sites

R3ality, there are a couple of ways to do this.

use typeName

You can do:

_input = _this select 0;
_inputtype = typename _input;
if (_inputtype == typename "") then {} for string
if (_inputtype == typename []) then {} for array

or just use

if (typeName _input == "STRING") then {}

if (typeName _input == "ARRAY") then {}

etc, etc

Share this post


Link to post
Share on other sites
R3ality, there are a couple of ways to do this.

use typeName

Thanks! Using typeName seems a lot better than what I found..

Share this post


Link to post
Share on other sites

Hi folks,

I've got a quick question on arrays. How do I go about applying a unique variable to each element in an array. So for example if I wanted to give each unit in the array 'allunits' a unique letter of the alphabet from an array I'd called 'alphaarray' how would i do that?

Share this post


Link to post
Share on other sites

Using an alphabet will only allow 24 units

alphaarray = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
_i=0;
{
_x setvariable ["letter",alphaarray select _i,true];
_i=_i+1;
} foreach allunits; 

to retrieve the letter you need to use getvariable

hint   (name_of_unit getvariable "letter")

you could just assign a number

_i=0;
{
_x setvariable ["number",_i,true];
_i=_i+1;
} foreach allunits; 

to retrieve the number you need to use getvariable

hint   str (name_of_unit getvariable "number")

or a little of both

_i=0;
{
_x setvariable ["string","man_" + str _i,true];
_i=_i+1;
} foreach allunits; 

retrieve with

hint   (name_of_unit getvariable "string")

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  

×