Jump to content
Sign in to follow this  
Op4 BuhBye

Multiple "If", "Then" statements?

Recommended Posts

Can you use multiple "if", "then" statements in a script?

IE:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">(!(alive w1)) : hint "w1 is dead" : dead = 1

Share this post


Link to post
Share on other sites

Can you explain what you are trying to do? Your example code makes no sense - there is no "if", and two "then"s.

I think switch command may be what you are after, or maybe you just mean using a ; (semi-colon) to seperate statements?

eg:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (abc==xyz) then {player setPos [0,0,0]; player setDamage 1};

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (!(alive w1)) then {hint "w1 is dead"; dead = 1};

See this Wiki article : Control Structures.

Share this post


Link to post
Share on other sites

The actual code from the script is....

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">?(_shooter in list _shooterff) : _mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit] : goto "punishmentff"

Can I use multiple colins in this statement?

Share this post


Link to post
Share on other sites

You can use curled braces around your then code to make it into a "block" (a series of statements to be executed if the condition == true). You can put any number of statements within this block.

This should work, although I'm not 100% sure about goto within a block:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">?(_shooter in list _shooterff) : {_mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit]; goto "punishmentff"}

This will not work:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">?(_shooter in list _shooterff) : _mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit]; goto "punishmentff"

The general case:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (condition) then {statement1; statement2; ...; statementN};

A working example from one of my SQF scripts. Note the formatting would probably not work in an SQS script:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (count _groupTargetPos == 3) then {

_mapCtrl = ((findDisplay 9000) displayCtrl 9052);

_mapCtrl ctrlMapAnimAdd [1, 0.10, getmarkerPos _targetMarkerName];

ctrlMapAnimCommit _mapCtrl;

waitUntil {ctrlMapAnimDone _mapCtrl};

ctrlMapAnimClear _mapCtrl;

};

Apart from easier-to-read formatting, the main advantage of using the SQF syntax is that the semi-colon is always used to separate statements, where-as SQS uses semi-colons sometimes and return/new line at others. I find consistency of syntax makes scripting easier, special cases are a PITA smile_o.gif

I'm just loading ArmA now to make sure everything I've suggested is correct for SQS, I've been working on textures not scripts lately.

Share this post


Link to post
Share on other sites

In that statement wouldn't I want to say...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">?(((_shooter in list _shooterff) : (_mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit]) : goto "punishmentff")))

To keep the goto statement within that string and not just send anyone to "punishmentff"? Or will the curled braces do the same thing?

Share this post


Link to post
Share on other sites

No, you can only have one "then" (or : (colon)) for one "if". Brackets () are not used to group statements, only values (as used in conditions, maths functions, etc).

Actually, now I've tested it, you will definitely need {} braces around your then statement.

working example test.sqs:

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

if (_value == "one") then {hint format["player is %1",name player]; unit1 setVelocity [0,0,10]; goto "somewhere"};

~1

player sideChat "finished normal"

exit

#somewhere

player sideChat "finish somewhere"

NOT WORKING example test.sqs:

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

if (_value == "one") then hint format["player is %1",name player]; unit1 setVelocity [0,0,10]; goto "somewhere";

~1

player sideChat "finished normal"

exit

#somewhere

player sideChat "finish somewhere"

In this 2nd broken example, every case ends up going to "somewhere".

Share this post


Link to post
Share on other sites

So would an easy fix be....

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">?(_shooter in list _shooterff) : goto "punishmentff"

#punishmentff

_mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit]

Will this work?

Share this post


Link to post
Share on other sites

Hehe, yes, that's a good way around it in this particular case, but sometime you will need to use more than one then statement.

The general syntax is pretty easy, indeed you should always be using braces even if you are only using one statement:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE"> if (condition) then {statement}

Share this post


Link to post
Share on other sites

Thanx fasad, I seem to be blacklisted in here. You and Mr Peanut are the only ones who answer my questions and I appreciate it. thumbs-up.gif

I need to learn SQF!!!!!

Share this post


Link to post
Share on other sites

If I understand correctly you simply need if then else etc...

Works for SQF and SQS:

if(condition)then{ bla bla bla bla } else { bla bla bla bla2 };

Thats the same as what you wanted:

? (condition) : bla bla bla bla : bla bla bla bla2;

And yes, forget about SQS dude wink_o.gif

Quote[/b] ]?(_shooter in list _shooterff) : _mesg = format["%1 will be dealt with for shooting Team Mate %2.",name _shooter,name _hit]; goto "punishmentff"
Ehrr, as far as I can see there's nothing wrong with this code dude, should work fine.

Share this post


Link to post
Share on other sites

Well... I had the whole script up here and not 1 person answered it, and I was looking at it last night and saw I had like 6 lines of code with 2 colins in each line.

So I started to wonder if that was why its not working.

Share this post


Link to post
Share on other sites

Hi,

Just started learning SQF scripting (never done any sqs) and have gotten stuck on my first proper one and wondered if anyone had the time to put me back on track.

I am trying to make a random waypoint for a sector control mission, similar to the SC map that came with Arma (that one is in sqs though, with alot of gotos) for when there is not enough humans.  The main idea is, that its an 8 vs 8 map with AI  (none in a group), who don't always go into the same spots (it's a tight map).

After I figure this out, I hope to work torwards getting them to push together forward, if they detect that they have more present in the area than the opponents.  

This is what I have got so far, with my errors included in the comments.

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

/*As I understand it, Ai are server side so random should have no problems here?*/

_x=ceil (random 3);

if (_x==1) then {

        _west domove getmarkerpos "m1"}

         else

        {

         if (_x==2) then { _west domove getmarkerpos "m2"}

        };

         if (x==3) then { _west domove getmarkerpos "m3"};

/*Problem now is, I get the hint as soon as he starts on his way to the first marker.  I'm guessing it has something to do with the unitready bit, but can't figure it out.*/

Waituntil {unitready _west;hint "made it"};

if (!alive unit) exitWith {};

//will make the sleep random next, if I can.

sleep 10;

if (_x==1) then {

        _west domove getmarkerpos "m4"}

         else

        {

         if (_x==2) then { _west domove getmarkerpos "m5"}

/*And so on until he reaches the last ones before a flag, in which he executes another sqf for the same type of thing.*/

The main problem being the waituntil bit.

I have probably gone about things the wrong way, for instance I think, switch maybe what I should be using instead of the if's, but I couldn't figure out how to get it working with the random number.

Thanks for any help, should anyone have the time.

Share this post


Link to post
Share on other sites

you should put an if(not(local server))exitWith{};

(I haven't got the isServer thing to work when I preview it and when Im a local server host, because it'll kick me from executing the script because I am then the client....)

This because you don't want the clients to be running this because they want to spend their CPU on other things biggrin_o.gif

Anyways.

The switch-thingy would probably look something like.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_x=ceil (random 3);

switch(_x) do{

case 1:{

_west domove getmarkerpos "m1";

};

case 2:{

_west domove getmarkerpos "m2";

};

case 3:{

_west domove getmarkerpos "m3";

};

};

Then I think you've got the waitUntil backwards.

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

sleep 0.5 //Sleep here for not clogging down the server

//You must not have any; after the last line as it counts as an return value.

(unitready _west)

};

hint "ready xD!!";

Hope this helps

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  

×