Jump to content
Sign in to follow this  
austin_medic

_x is undefined in a forEach loop?

Recommended Posts

Well I've crashed straight into another brick wall with this games error box that simply leaves me scratching my head.

Trying to make a mission with lots of randomization, got this function that spawns a group:

AUSMD_spawn_group =
{
_side = _this select 0;
_position = _this select 1;
_position set[2,0];
_amount = _this select 2;
_units = [_side] call AUSMD_generate_group;
_grp = createGroup _side;
//_lead = _units select 0;
_position = [(_position select 0) + (random _amount - random _amount),(_position select 1) + (random _amount - random _amount),_position select 2];
//_lead createUnit[_position, _grp,"", (0.35 + random 0.7), "CAPTAIN"];
//_units set[0,nil];
{
	_x createUnit [_position, _grp,"",(0.2 + random 0.5)];
} forEach _units;
_grp;
};

as you can see it calls another function to get an array of units to spawn inside the foreach loop at the bottom.

AUSMD_generate_group =
{
_side = _this select 0;
_group = [];
westGroups = [["B_Soldier_TL_F","B_soldier_AR_F","B_Soldier_GL_F","B_soldier_LAT_F"],["B_Soldier_TL_F","B_soldier_AT_F","B_soldier_AT_F","B_soldier_AAT_F"]];
eastGroups = [["O_Soldier_TL_F","O_Soldier_AR_F","O_Soldier_GL_F","O_Soldier_LAT_F"],["O_Soldier_TL_F","O_Soldier_AT_F","O_Soldier_AT_F","O_Soldier_AAT_F"]];
if(_side == west) then
{
	_group = westGroups call BIS_fnc_selectRandom;
}
else
{
	_group = eastGroups call BIS_fnc_selectRandom;
};
_group;
};

that returns an array of units fine, but the foreach loop at the bottom of the first function throws an error saying _x is undefined.

Any ideas of my problem?

---------- Post added at 19:11 ---------- Previous post was at 19:06 ----------

Solved my problem byp utting sleep 0.1 above the foreach loop. I was under the impression that SQF didnt continue on reading until the command it was currently processing was finished completely.

Share this post


Link to post
Share on other sites

Remove the last semicolon in AUSMD_generate_group,

otherwise it won't return the _group to the _units handle in AUSMD_spawn_group.

AUSMD_generate_group =
{
_side = _this select 0;
_group = [];
westGroups = [["B_Soldier_TL_F","B_soldier_AR_F","B_Soldier_GL_F","B_soldier_LAT_F"],["B_Soldier_TL_F","B_soldier_AT_F","B_soldier_AT_F","B_soldier_AAT_F"]];
eastGroups = [["O_Soldier_TL_F","O_Soldier_AR_F","O_Soldier_GL_F","O_Soldier_LAT_F"],["O_Soldier_TL_F","O_Soldier_AT_F","O_Soldier_AT_F","O_Soldier_AAT_F"]];
if(_side == west) then
{
	_group = westGroups call BIS_fnc_selectRandom;
}
else
{
	_group = eastGroups call BIS_fnc_selectRandom;
};
_group
};

Cheers

Share this post


Link to post
Share on other sites
Remove the last semicolon in AUSMD_generate_group,

otherwise it won't return the _group to the _units handle in AUSMD_spawn_group.

Sorry, but this is utter nonsense. Semicolon should make no difference whatsoever.

---------- Post added at 20:56 ---------- Previous post was at 20:53 ----------

that returns an array of units fine, but the foreach loop at the bottom of the first function throws an error saying _x is undefined.

Have you got a copypaste of the error?

Share this post


Link to post
Share on other sites

These functions work fine for me...

[west, position player, 0] call AUSMD_spawn_group;
[east, position player, 0] call AUSMD_spawn_group;
//both working

As MDCC said, the problem is with the _units array.

Somewhere in your code you've probably put a string for the "side" parameter. Or you've misspelled something causing the interpreter to look for an undefined variable. Check your code for "weast" ;) I know I've typed that plenty of times by accident.

Share this post


Link to post
Share on other sites

Please do yourself a favor and make sure your variables in your functions are all(!) declared private! Otherwise a call to your function can and will happily overwrite variables a scope below, and these kind of bugs are the most annoying, because things might work fine for so long, and all of a sudden... :mad: and then they tend to be super hard to debug. :936:

Seriously, best make it a habit to first declare variables private before first use/initialization.

Otherwise chances are you'll forget to make them private and move on...

P.S. Make sure you understand the dynamic/late binding of sqf. ;)

And don't think this wont bite you in the ass. Chances that you're using variables like "_side", "_group", "_unit", and similar everyday identifiers (at some lower scope too) are high, and tend to go to 1 the more you code and make (re-)use of your own functions.

^^ this might be one way to fuck up your units arrays, btw. So you might wanna check that. In general; if you have a problem with one of your functions, start with diag_log'in and having a look at all the inputs to your function. And then you might wanna consider some parameter-verification/checking and error reporting too. :cool:

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  

×