Home   Help Search Login Register  

Author Topic: if/then script causing CTD  (Read 1189 times)

0 Members and 1 Guest are viewing this topic.

Offline samusi01

  • Members
  • *
if/then script causing CTD
« on: 23 Oct 2008, 02:52:23 »
I am trying to figure out how to use if/then statements in scripts.  The following script is what I am using to randomly position a man on the island for a search and rescue mission.

Code: [Select]
_start = random 4;

?_start<1 :goto "P1";
?_start<2 :goto "P2";
?_start<3 :goto "P3";
?_start<4 :goto "P4";

#P1
_pos = [13452, 15159, 0];
_radius = 300;
_pos = [(_pos select 0)+_radius/2-random _radius,(_pos select 1)+_radius/2-random _radius, _pos select 2];
_this setPos _pos;
exit;

#P2
_pos = [12517, 15952, 0];
_radius = 350;
_pos = [(_pos select 0)+_radius/2-random _radius,(_pos select 1)+_radius/2-random _radius, _pos select 2];
_this setPos _pos;
exit;

#P3
_pos = [12784, 15294, 0];
_radius = 250;
_pos = [(_pos select 0)+_radius/2-random _radius,(_pos select 1)+_radius/2-random _radius, _pos select 2];
_this setPos _pos;
exit;

#P4
_pos = [14224, 14689, 0];
_radius = 200;
_pos = [(_pos select 0)+_radius/2-random _radius,(_pos select 1)+_radius/2-random _radius, _pos select 2];
_this setPos _pos;
exit;

I have added a 'addAction' in my player init line allowing me to reposition myself randomly using the same script.  When I attempt to test the mission, the missing man is successfully placed in one of the four areas.  However, if I attempt to reposition myself, the game immediately crashes.  I managed to get a basic radio DF script working this afternoon but I had a lot of problems with the if/then statement in that script as well, which is why I am presuming that the if/then is where the problem is in the above script.  I have not been able to find very much information on if/then statements in the forums and I would appreciate any assistance offered.

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: if/then script causing CTD
« Reply #1 on: 23 Oct 2008, 10:41:38 »
Heh. I barely recognize the .sqs version of if...then anymore. ? : just isn't as obvious as if (condition) then {action} as in .sqf... ;)

Anyway, I'm not sure I quite got the jist of what you're saying: if you call this script via the action menu (on yourself) it crashes, whereas if you call it otherwise on someone else, it doesn't crash? In that case it might be a problem with _this : _this in cases of, for instance, unitOne exec "script.sqs", refers to unitOne. In the case of an addaction-run script though, _this refers to a whole array of things (such as the caller of the script, the one the action was attached to, the ID number of the action, optional arguments..etc. Check this array out in the entry on addaction). So if you're trying to setpos _this (and array!) someplace, there's a pretty good chance that you'll mess something up, yes :D

I suggest trying to run it via the radio menu instead for testing - it's a lot cleaner generally, and you don't have to worry about the AddAction-specific quirks (believe me, I know - addaction is annoying as hell to work with).

Also if there are any error messages etc., it'd be good to post them.

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline samusi01

  • Members
  • *
Re: if/then script causing CTD
« Reply #2 on: 23 Oct 2008, 22:15:44 »
Wolfrug,

Thanks for the information that you provided.  I did not know that addAction passed an array on to the script.  Having perused the entry on addAction, I believe that I should have added _this select 1 to tell the script that it needed to move me to the new location.  The other two parameters (_obj and _index) that addAction provides don't seem clear to me yet.  More research there I guess.

I modified the if/then to what feels to me like the correct coding (dabbled a bit in Java a while ago).

That said, this is what I have now:
Code: [Select]
if (_start<1) then {goto "P1";}
#P1
_pos = [13452, 15159, 0];
_radius = 300;
_pos = [(_pos select 0)+_radius/2-random _radius,(_pos select 1)+_radius/2-random _radius, _pos select 2];
_this setPos _pos;
exit;

I set it up to be trigger off radio Alpha and I get

'_this |#|setPos _pos;'
Error setpos: Type Array, expected Object

Modifying the code to replace _this with my unit name - SAR01 in this case - results in the script working.  What is the radio alpha passing to the script to generate that code?

Thanks for your time.