Jump to content
Sign in to follow this  
scajolly

_x undefined in forEach. Problem w array

Recommended Posts

Hello, good people.

 

Was hoping I could have your help today for something frustratingly trivial. I'll outline my separate intentions first, and then I'll describe the precise problem.

 

I want to have a trigger (bounceTrigger2) detect different units repeatedly (but never running for more than 4 instances), then call a script which spawns an object, an object which is then relocated every few seconds, meanwhile the script is also counting down to create another object, relocating that as well, and so on, for i ierations. However, I'm running into some pretty basic problems before I can finish all of my structure.

 

 

 

Init sqf: BounceVal = 0;

 

Trigger condition: this

Trigger activation: 

bounceVal = bounceVal+1;bounceHandle= [thislist select 0,bounceTrigger2] execVM "bounceJ.sqf";hint "go"

Trigger deactivation: bounceVal = bounceVal - 1;

 

 

 

BounceTrigger2.sqf

_vic = _this select 0;
_list = _this select 1;

_dist = 5;
_dist2 = _dist*2;
_time = random 3;
_tgArray = [];
_i = 0;

while {_vic in list _list} do {
  private["_tg1"];
  _tg1 = "16AA_veh_vt5_bmp2" createVehicle position _vic;
  _tgArray = + [_tg1];
  _dist = _dist+5;

  //debug pause and hint
  _i = _i+1;
  sleep 1;
  hint format ["iteration %1, dist = %2, array = %3",_i,_dist,_tgArray];

  while {(true)} do {
    _x setposasl [(getposasl _vic select 0)+(random _dist2 - _dist)*sin(random 359),(getposasl _vic select 1)+(random _dist2 - _dist)*sin(random 359),(getposasl _vic select 2)+(1.4+(random 2))];
    sleep _time+(random 5);
  } forEach _tgArray;

  // x seconds for testing
  sleep 10; 
  hint "a new target is made";
  sleep 5;
}

then {
  {deleteVehicle _x} forEach _tgArray;
}

Main problem:

|#|_x setposasl ....

Error Undefined variable in expression: _x

 

I tried to get the array to return its contents, and that reads "array = [9c5d6b00# 378656: bmp2_2.p3d]"

 

What am I getting wrong here? Shouldn't _x naturally point to every entry in _tgArray?

 

Secondary problem

For my intentions, should the trigger be repeatable?

The trigger will ideally detect several BLUFOR detected by OPFOR, so in my head it should be repeatable. A BLUFOR entity might exit the trigger area and come back in, in which case the script should run (assuming bounceVal <= 4). A BLUFOR entity already in the list must NOT, however, call the script more than once. So then it seems like it shouldn't be repeatable.

 

Share this post


Link to post
Share on other sites

In the script it looks like you're missing a set of curly braces. You're opening up the while loop, but you forgot to "open" the forEach loop with one. Corrected version:

  while {(true)} do {
  {
    _x setposasl [(getposasl _vic select 0)+(random _dist2 - _dist)*sin(random 359),(getposasl _vic select 1)+(random _dist2 - _dist)*sin(random 359),(getposasl _vic select 2)+(1.4+(random 2))];
    sleep _time+(random 5);
  } forEach _tgArray;

  // x seconds for testing
  sleep 10; 
  hint "a new target is made";
  sleep 5;
}

Also yeah the trigger should be repeatable. Repeatable triggers don't call themselves every frame if a unit is standing inside of them (afaik but it's been a while) but rather "Repeatable" just tells the engine "this thing may be called more than once".

Share this post


Link to post
Share on other sites

Thank you so much, Tryteyker, that was absolutely the issue. I can't believe I didn't spot that.

Share this post


Link to post
Share on other sites

I've now encountered a new problem

 

Each loop now relocates the same vehicle. But I wanted to create a -new- vehicle _tg1 with each loop. How do I create new iterations of _tg1?

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  

×