Jump to content
Jamsd22

Having a Script Repeat For Each Unit inside Trigger Zone

Recommended Posts

Hey, i want a script ive written to dynamically spawn ai to repeat twice for each Player in the Trigger boundaries. I know that i need to be using some form of in thislist command to do that but i dont know how to do it TWICE foreach as i dont think foreach has the ability to take a multiplier

 

_Marker = ["Mark1", "Mark2"]; //Add more marks for more spawns

_group = ["_1group"];

_Enemy_Class = ["LM_OPCAN_CMA_Engineer"];  //UnitType Here

{

    _Ai_Selected = selectRandom _Enemy_Class; //Selects A Random Class

  _Marker_R = selectRandom _Marker;     //Gives Unit a Random Spawn 

  _Marker_R_M = selectRandom _Marker;  //Selects a Random Move Objective

    _1group = creategroup [independent, true];     

    {   

      _unit = _1group createunit [_x, getmarkerpos _Marker_R,[],0,"NONE"];    

      _unit domove (getmarkerpos _Marker_R_M);   

    } forEach [_Ai_Selected];

} forEach Units in thisList;  //is my current code

Share this post


Link to post
Share on other sites

Welcome on forum.

Why are you thinking "foreach has[n't] the ability to take a multiplier" ?

You can run :

for "_i" from 0 to 1 do { your code };

or

{ you code } forEach [1,2];

If you want more help, report your script. It's easier. It seems you don't have so much skill in MP. I suggest you to read these pages and their links:
https://community.bistudio.com/wiki/Code_Best_Practices

https://community.bistudio.com/wiki/Code_Optimisation

https://community.bistudio.com/wiki/Initialization_Order

https://community.bistudio.com/wiki/Event_Scripts

https://community.bistudio.com/wiki/Multiplayer_Scripting

especially: https://community.bistudio.com/wiki/Multiplayer_Scripting#Locality

 

  • Like 2

Share this post


Link to post
Share on other sites
7 minutes ago, pierremgi said:

Welcome on forum.

Why are you thinking "foreach has[n't] the ability to take a multiplier" ?

You can run :

for "_i" from 0 to 1 do { your code };

or

{ you code } forEach [1,2];

If you want more help, report your script. It's easier. It seems you don't have so much skill in MP. I suggest you to read these pages and their links:
https://community.bistudio.com/wiki/Code_Best_Practices

https://community.bistudio.com/wiki/Code_Optimisation

https://community.bistudio.com/wiki/Initialization_Order

https://community.bistudio.com/wiki/Event_Scripts

https://community.bistudio.com/wiki/Multiplayer_Scripting

especially: https://community.bistudio.com/wiki/Multiplayer_Scripting#Locality

 


im very new to arma's scripting, I need to ask you what [1,2] is there?
i should also say that the script is a .sqf in the mission file and is being run when a trigger is activated using VMexec

Share this post


Link to post
Share on other sites

Well... [1,2] is just an array of 2 elements. These elements don't matter because they are not parameters used in your script. You could replace the array by [48,12] or ["bla","bla"] or [objNull,objNull]... Here, only the number of elements matter. On the other hand, avoid [var1,var2] if var1 and var2 are variables not defined.

Share this post


Link to post
Share on other sites
43 minutes ago, Jamsd22 said:

 

_Marker = ["Mark1", "Mark2"]; //Add more marks for more spawns

_group = ["_1group"];

_Enemy_Class = ["LM_OPCAN_CMA_Engineer"];  //UnitType Here

{

    _Ai_Selected = selectRandom _Enemy_Class; //Selects A Random Class

  _Marker_R = selectRandom _Marker;     //Gives Unit a Random Spawn 

  _Marker_R_M = selectRandom _Marker;  //Selects a Random Move Objective

    _1group = creategroup [independent, true];     

    {   

      _unit = _1group createunit [_x, getmarkerpos _Marker_R,[],0,"NONE"];    

      _unit domove (getmarkerpos _Marker_R_M);   

    } forEach [_Ai_Selected];

} forEach Units in thisList;  //is my current code

 

here: _marker is an array. Start with good habits, rather choose _markers or _mkrArray ... something logic with the related data.

_group = ["_1group"];   // no What for? Why a string "_1group" ? why an array of a string ["_1group"]?

_Enemy_Class = ["LM_OPCAN_CMA_Engineer",.....]; // OK with a unique class or multiple

{ } forEach units in thisList // No and no.

First NO:

 thisList works in condition field and on act field of a trigger, not in an sqf/code execVMed/spawned from them.

You need to script something like: [thisList] execVM "your.sqf"  // so thisList is passed as parameter.

then inside your.sqf : params ["_blabla"]; // where _blabla is a local variable refering to thisList.

Spoiler

Note: thisList is the list of the units or vehicles satisfying the preset condition of the trigger. So, you pass the "picture" of these possible candidates as the time the trigger is activated. Say any player present >>> the first entering player fires the code, so thisList is [firstPlayerVariableHere] . When the second player enters the trigger, there is no more activation (already activated) except if the 1st player formerly exited the area before 2nd player entrance.

 

Second "NO" the thisList (or _blabla inside the sqf) is already an array. So forEach _blabla will work.

 

There are some other problems but, at this time, try to understand how a trigger works, what returns thisList, what you want exactly, at what time. As wrote above, thisList (for anyplayer presen) will return an array with a unique (vehicle) player most of the time. Have fun learning all these things, (but start in single player).

 

 

Share this post


Link to post
Share on other sites

//To clarify and add on Fixes i have made from your Recommendations 
1.) The trigger is on a 10 second "conditions met" delay, so that all the players have an opportunity to enter the trigger before it checks the [thislist] for the trigger
2.) Marker is now Markers, for sanity
3.) I Have the groups defined because it also assigns the Unit to a group, i just havent written that in here, but yes, it needed cleaning up,
4.) I will play around with the [thislist] execVM recommendation, thanks thats where my knowledge failed, i figured it would pull thislist from the trigger the script was running from.
5.) do i define the params for it in my code by saying  _Tlist = Thislist in the activation box of my trigger and then in my script do params [_Tlist]
6.) your [1,2] worked, thank you much
my code now looks something like 
 

params [_Tlist];

 

_Marker = ["Mark1", "Mark2"]; //Add more marks for more spawns

_Group = ["Group1", "Group2"]; //Which Group The Unit Will Join

_Enemy_Class = ["LM_OPCAN_CMA_Engineer", "LM_OPCAN_CMA_Rifleman", "LM_OPCAN_CMA_Rifleman_AT", "LM_OPCAN_CMA_Officer"];  //UnitType Here

 {

    {

     _Ai_Selected = selectRandom _Enemy_Class; //Selects A Random Class

      _Marker_R = selectRandom _Marker;     //Gives Unit a Random Spawn 

     _Marker_R_M = selectRandom _Marker;  //Selects a Random Move Objective

     _1group = creategroup [independent, true];     

      {   

        _unit = Group1 createunit [_x, getmarkerpos _Marker_R,[],0,"NONE"];    

        _unit domove (getmarkerpos _Marker_R_M);   

      } forEach [_Ai_Selected];

    } forEach [1,2];  //How Many Units Spawn

 } forEach [Blufor in _Tlist];

 

//Edit, The Tlist still isnt working, i dont understand the params func in relation to my trigger

even with the wiki if anyone could help

Share this post


Link to post
Share on other sites
1 hour ago, Jamsd22 said:

1.) The trigger is on a 10 second "conditions met" delay, so that all the players have an opportunity to enter the trigger before it checks the [thislist] for the trigger
2.) Marker is now Markers, for sanity
3.) I Have the groups defined because it also assigns the Unit to a group, i just havent written that in here, but yes, it needed cleaning up,

Yes,

just pay attention _underscoredVariable are local to a script (or even a simple scope), as nonUnderscoredVar are global (known by any script on a PC once defined)

There are also public variables, shared by script on server + clients... but not the topic here.

Do not mix variable names for groups and units (you can do what you want with variable names (in regard of syntax) but you'll save time for debug if there is a logic in these names.

thisList is a "magic variable" (roughly that means: easy, useful but limited)

 

1 hour ago, Jamsd22 said:

5.) do i define the params for it in my code by saying  _Tlist = Thislist in the activation box of my trigger and then in my script do params [_Tlist]

 

You don't need to define a variable _Tlist, just for passing it inside the sqf. thislist is already a workable variable in no act field, so pass it to the sqf as shown

 

Inside the sqf, the first thing to do is to grab the outside variable(s) (parameters) so params is the best tools. (you can use also the magic variable _this, see the link)

But as any command, params has a syntax (see BIKI for params but also BIKI each time you are using a command or a function. Believe me, I'm all day long reading them!)

If you read params on biki (and other commands), your script looks like:


 

params ["_Tlist"];
private _markers = ["Mark1", "Mark2"]; //Add more marks for more spawns (if existing of course! If not create them)

private _Groups = [Group1, Group2]; //Which Group The Unit Will Join. Groups are variable, so without quote as markers! These groups must exists !! If not create them... typically the same way you do bellow for nuts! Not sure what _Groups is defined for...

private _Enemy_Classes = ["LM_OPCAN_CMA_Engineer", "LM_OPCAN_CMA_Rifleman", "LM_OPCAN_CMA_Rifleman_AT", "LM_OPCAN_CMA_Officer"];  //UnitType Here

private ["_markersUnit","_marker_R","_marker_R_N","_Ai_Selected","_unit"];
{
  {
/* NO NO NO
_Marker_R = selectRandom _Markers;     //Gives Unit a Random Spawn
_Marker_R_M = selectRandom _Markers;  //Selects a Random Move Objective... NO can be the same as spawn position. Prefer the lines below:
*/
        _markersUnit = _markers; // just to reset the array (see below) for each unit
      private _marker_R = _markersUnit deleteAt floor random count _markers;  // this way _marker_R is no more present in _markersUnit array.
      private _marker_R_N = selectRandom _markersUnit;
// private _1group = createGroup [independent, true];     // What for? if you don't use it!! see above

    private _Ai_Selected = selectRandom _Enemy_Classes; //Selects A Random Class
    private _unit = Group1 createUnit [_Ai_Selected, getMarkerPos _marker_R,[],0,"NONE"];     // same remarks as above: group1 must exists somewhere before (I guess not)
    _unit doMove (getMarkerPos _Marker_R_M);   
  } forEach [1,2];  //How Many Units Spawn
} forEach _Tlist;

 

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

×