Jump to content
Sign in to follow this  
CarlGustaffa

What's my syntax error?

Recommended Posts

The following code does seem to work:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_storedtarget = "HeliHEmpty" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

if (debug) then

{

   _storedtarget = "HeliH" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

};

But this original one failed whenever the else statement should be active:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (debug) then

{

   _storedtarget = "HeliH" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

}

else

{

   _storedtarget = "HeliHEmpty" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

};

Later on I call an AI artilscript with:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

nul = [E1,_storedtarget] execVM "bat\eastari\fireshell.sqf";

E1 reffering to an AI D-30 cannon in this case.

Fireshell.sqf, looks like this (a rewrite of Mr. Murrays arty script):

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

sleep random 2;

_artilunit = _this select 0;

_target = _this select 1;

_targetx = getPos _target select 0;

_targety = getPos _target select 1;

_artilunit doWatch [_targetx,_targety,5000];

_ammo =_artilunit Ammo "D30";

sleep 2 + random 2;

_artilunit fire "D30";

waituntil {_ammo > _artilunit ammo "D30"};

sleep 15;

_N = nearestObject [_artilunit,"HeatD30"];

_targetx = _targetx+((random 60)-30);

_targety = _targety+((random 60)-30);

_H = "HeliHEmpty" createVehicle [_targetx,_targety];

sleep 1;

_H say "Ari";

sleep 1;

_N setpos [_X,_Y,0];

"Sh_122_HE" CreateVehicle [_targetx,_targety,0];

sleep 1.5;

deleteVehicle _H;

debug=true; (or false) is set in missions init.sqf. Problem appear to be the _targetx and _targety values when fireshell.sqf is called, undefined or something.

How on earth can't the else statement be properly executed? Where am I going wrong here? banghead.gif

Share this post


Link to post
Share on other sites

try outputting debug and _storedTarget after the if/else:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">player sideChat ("Debug: " + debug);

if (debug) then

{

_storedtarget = "HeliH" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

}

else

{

_storedtarget = "HeliHEmpty" createVehicle [getMarkerPos "mDest" select 0, getMarkerPos "mDest" select 1];

};

player sideChat ("storedTarget: " + _storedTarget + " Pos: " + getPos _storedTarget);

And check the outcome when debug is false and true.

I have seen the weirdest things happen when private variables were not set in private array (private ["_var1", "_var2", "_var3"]; etc etc), you might wanna look into that.

A tip btw, you can just use: "...." createVehicle (getMarkerPos "mDest");

Share this post


Link to post
Share on other sites
Quote[/b] ]A tip btw, you can just use: "...." createVehicle (getMarkerPos "mDest");

I would write it to be even more concise:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">  _storedtarget = (if (debug) then {"HeliH"} else {"HeliHEmpty"}) createVehicle getMarkerPos "mDest"

Quote[/b] ]I have seen the weirdest things happen when private variables

This really seems to be causing the issue. The variable _storedtarget  is introduced in the if / then / else scope, and is destroyed once you leave it. With the concise construct the variable is created outside of the scope and you will not face the issue. If you want to keep your script as it is structured now. you need to add private "_storedtarget" before the if, so that the variable exists in proper scope.

Share this post


Link to post
Share on other sites
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> _storedtarget = (if (debug) then {"HeliH"} else {"HeliHEmpty"}) createVehicle getMarkerPos "mDest"

Great point, out of habbit I don't use if/else in this manner, but it is cleaner indeed smile_o.gif

Quote[/b] ]With the concise construct the variable is created outside of the scope and you will not face the issue. If you want to keep your script as it is structured now. you need to add private "_storedtarget" before the if, so that the variable exists in proper scope.
Thank you for explaining this. I have not put too much effort in finding out the specifics, but it's interesting to know the exact how and why smile_o.gif

Share this post


Link to post
Share on other sites

Thank you all. Yes when I look at it now, it seems "obvious" regarding the variable being inside the if/else scope. The new scope considerations take a little getting used to I think.

The suggested structure is naturally also a lot better looking. Reason for getPos select was earlier I tried using a third option for height. But will change this as well too now that I know the reason for it not working.

Thanks again, problem solved I think.

Share this post


Link to post
Share on other sites

I would do:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_type = "";

if (debug) then

{

_type = "HeliH";

} else

{

_type = "HeliHEmpty";

};

_storedtarget = type createVehicle (getMarkerPos "mDest");

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  

×