Jump to content
Sign in to follow this  
beno_83au

Problem with if...then condition.

Recommended Posts

I'm having trouble running a forEach loop and I think the problem is in the if...then condition, but I'm not sure what is wrong.

if (!isServer) exitWith {};

_retreatpos = [07226,03010,0];
_centremkrpos = [06691,02649,0];

centremkr = createMarker ["Centre Marker", _centremkrpos];
centremkr setMarkerShape "Icon";
centremkr setMarkerType "Empty";

{if(side _x != west && getPos _x distance getMarkerPos "centremkr" < 1000) then
{
uisleep 0.5;	
_x forceSpeed -1;
_x setUnitPos "Auto";
_x enableAI "Move";
_x move _retreatpos;
_x setSpeedMode "Full";
_x setCombatMode "Green";
_x setBehaviour "Combat";
};
} forEach allUnits;

There's some more code after this but that executes fine, but the endstate is that the AI I'm trying to reference with the if...then condition are supposed to move to a new loc, being _retreatpos, but they don't . Where am I going wrong here?

Cheers.

Share this post


Link to post
Share on other sites

I'm not too sure about that if statement being the cause, but just to be safe, I threw in some appropriate parenthesis. I always say, "when it fucks up, wrap it up". Leaving out parenthesis is pretty hit-or-miss in arma scripting. Sometimes the code will work as intended, other times it will bone your entire script. At best it causes undefined behavior. Try this:

if((side _x) != west && {(getPos _x) distance (getMarkerPos "centremkr") < 1000}) then

Also this:

if (!isServer) exitWith {};

is a horrible practice. Just don't. You shouldn't ever need a catch-all like this. It's really not so hard to write:

if (isServer) then
{
//server scripts
} else
{
//client scripts
};

in init.sqf. Good structure will save you a lot of headaches...trust me.

Edited by DreadedEntity

Share this post


Link to post
Share on other sites

dont need qoutes around "centremkr"

---------- Post added at 10:22 ---------- Previous post was at 10:20 ----------

if (!isServer) exitWith {};

is a horrible practice. Just don't.

nonsense

Share this post


Link to post
Share on other sites

Well, thanks for the quick replies both of you, but still no joy.

I don't know if this makes a difference, but I've got two scripts that spawn in EAST AI (a simple generic createUnit script I did up and an old one, Grimes' Building Occupation Script from ArmA 2 (I think?)) and I've had the loop work fine when I've placed units in the editor, but not when they've been spawned in mid-mission.

Share this post


Link to post
Share on other sites

Do you have -showScriptErrors on?

Share this post


Link to post
Share on other sites
Do you have -showScriptErrors on?

Yeah, but nothing returns as wrong, the script runs as though there is nothing wrong with it as the next few lines after this destroy some cars just fine, but none of the enemy move to _retreatpos.

Edit: Sorry, I should've actually confirmed that because NO. I thought I had the parameter running......

The uiSleep command was giving errors so I pulled that but there's still no movement from the AI.

Edited by beno_83au

Share this post


Link to post
Share on other sites

Hmm, try:

group _x move _retreatpos;

Or:

_x doMove _retreatpos;

Also try putting the forceSpeed after the setSpeedMode.

Share this post


Link to post
Share on other sites

[sOLVED]

Thanks, got it working. Seems like I had made a bit of a mess of this one from the start. In between helping out with homework and handling some of my own dramas though I've changed a few things, both mentioned above and not, that got it working so I'm not sure what the catalyst(s) was/were. I ended up adding in a deleteWaypoint though which made a big difference:

if (!isServer) exitWith {};

_retreatpos = [07226,03010,0];
_centremkrpos = [06691,02649,0];

centremkr = createMarker ["Centre Marker", _centremkrpos];
centremkr setMarkerShape "Icon";
centremkr setMarkerType "Empty";

{if((side _x != west) && (getPos _x distance getMarkerPos centremkr) < 1000) then
{
while {(count (waypoints (group _x))) > 0} do
		{
 			deleteWaypoint ((waypoints (group _x)) select 0);
		};	
_x forceSpeed -1;
_x setUnitPos "Auto";
_x enableAI "Move";
_x move _retreatpos;
_x setSpeedMode "Full";
_x setCombatMode "Red";
_x setBehaviour "Aware";
};
} forEach allUnits;

So, thanks guys.

Edit: Although I don't understand why a sleep command isn't working inside either of the forEach loops, but I can do without it.

---------- Post added at 14:21 ---------- Previous post was at 14:01 ----------

Also try putting the forceSpeed after the setSpeedMode.

Just to explain why though, a negative value for forceSpeed resets the unit back to default, so that's why I've got it before setSpeedMode. I had some units on forceSpeed 0 so I just blanketed everyone back to default to make it easy.

Edited by beno_83au

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  

×