Jump to content
Sign in to follow this  
Czarny

Problem with "if" loop

Recommended Posts

I've wrote this script (this is lap counter in "race mission"):

_el=0

_wl=0

#Update

? (e1==1) &&(e2==1)&&(e3==1)&&(e4==1)&&(e5==1) &&(e6==1)&&(e7==1) &&(e8==1) &&(e9==1) &&(e10==1) &&(e11==1) &&(e12==1):

{

                _el=_el+1

_e1=0

_e2=0

_e3=0

_e4=0

_e5=0

_e6=0

_e7=0

_e8=0

_e9=0

_e10=0

_e11=0

_e12=0

                }

? (_w1==1) &&(_w2==1)&&(_w3==1)&&(_w4==1)&&(_w5==1) &&(_w6==1)&&(_w7==1) &&(_w8==1) &&(_w9==1) &&(_w10==1) &&(_w11==1) &&(_w12==1) :

{

                _wl=_wl+1

_w1=0

_w2=0

_w3=0

_w4=0

_w5=0

_w6=0

_w7=0

_w8=0

_w9=0

_w10=0

_w11=0

_w12=0

               }

? (_el>4)||(_wl>4) :

goto "End"

? (_wl<5) && (_el<5) :

Titletext [Format ["west: %1 lap - east %2 lap", _wl, _el],"plain down"]

goto "Update"

#End

? (_wl>4) && (_el<5) :

Titletext ["player WEST win","plain"]

? (_el<5) && (_wl>4) ;

Titletext ["player EAST win","plain"]

exit

e1....ex and w1...wx are a checkpoints, el and ew is number of lap each of two players, there are 5 laps to go

My problem is I don't know construction of "if" loop. It seems in this script language this one is little bit different than in c++.

My ignorance couses that foregoing script stright away generate "player EAST win". Why? Where is error?

Share this post


Link to post
Share on other sites

It looks like you are using an SQS script.

In SQS, anything between brackets must be on the same line.

This will cause an error:

<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;

          statement3;

}

You should write it like this:

<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; statement3;}

If you want to use multiline, your only option is to use LoadFile of PreProcessFile.

In your SQS:

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

MyResult = [arg1, arg2, (...etc. etc.)] call MyFunction

In your libMyFunction.SQF file:

<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;

          statement3;

          RESULT=statement;

};

RESULT;

Also note the difference in syntax. SQS uses semicolons only between brackets, SQF has a more C++ like syntax.

In the last example, the "RESULT;" works like the following C++ statement: "return RESULT;"

You can't use "GOTO" in SQF files. Infinite loops like this will also cause problems:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">while "TRUE" do { statement };

(Who the **** had that marvelous idea of using quotes around a condition?!?)

Share this post


Link to post
Share on other sites
Quote[/b] ]Who the **** had that marvelous idea of using quotes around a condition

It was me. It is caused byt the fact while condition needs to evaluated repeatedly - and it therefore needs to be passed not as an already evaluated value, but as an expression. Maybe you will like this (valid) design:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">while {true} do { statement };

Share this post


Link to post
Share on other sites
It was me. (...)

You gotta love these guys. Best customer-interaction ever.

biggrin_o.gif

Thanks for the explanation Suma.

Share this post


Link to post
Share on other sites

Script after corrects still dosn't work but i have next questions:

1. Is such construction allowed:

? (condition): { something; something; ? (condition): something;}

I mean are nested conditions' loops allowed?

2. I have two variables x and y.

_x==1 - is this comapre value x with 1?

y==1 - is this compare adress y with 1?

3. I have in trigger expression x=true. Is:

if(x) then .....

is correct notation? I mean is "true" frase equal to "1" or something like this?

I have propossition: maybe someone from BIS should to release some publication with correct notations?

According to conditions in brackets i know this is standard notation in c++.

Share this post


Link to post
Share on other sites

this is my script after rebuilt:

_el=0

_wl=0

#update

? (e1) &&(e2)&&(e3)&&(e4)&&(e5)&&(e6)&&(e7)&&

amp;(e8)&&(e9) &&(e10) &&(e11) &&(e12) : {_wl=_wl+1; goto "xxx";}

? (w1) &&(w2)&&(w3)&&(w4)&&(w5)&&(w6)&&(w7)&&

amp;(w8)&&(w9) &&(w10) &&(w11) &&(w12) : {_wl=_wl+1; goto "xxx";}

goto "update"

#xxx

? (_wl<5) && (_el<5) : Titletext [Format ["west: %1 lap - east %2 lap", _wl, _el],"plain down"];

? (_el>4)||(_wl>4) : goto "end"

w1=false

w2=false

w3=false

w4=false

w5=false

w6=false

w7=false

w8=false

w9=false

w10=false

w11=false

w12=false

goto "update"

#end

? (_wl>4) && (_el<5) : Titletext ["player WEST win","plain"]

? (_el<5) && (_wl>4): Titletext ["player EAST win","plain"]

exit

And of course it dosn't work. Uhhh...

Share this post


Link to post
Share on other sites
Quote[/b] ]I have propossition: maybe someone from BIS should to release some publication with correct notations?

What about this one?

Quote[/b] ]? (condition): { something; something; ? (condition): something;}

No, this is not possible: ? is a sqs element, and as such, it can appear only on the line beginning. You could try:

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

if (condition) then { something; something; if (condition) then {something};}

Or:

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

? (condition) : something; something; if (condition) then {something}

Share this post


Link to post
Share on other sites

That's because there are a whole mess of syntax errors in the script. For exmaple, this section....

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">? (e1) &&(e2)&&(e3)&&(e4)&&(e5)&&(e6)&&(e7)&&(e8)&&(e9) &&(e10) &&(e11) &&(e12) : {_wl=_wl+1; goto "xxx";}

...should look like this...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">? e1 && e2 && e3 && e4 && e5 && e6 && e7 && amp && e8 && e9 && e10 && e11 && e12: _wl=_wl+1; goto "xxx";

You cannot put line breaks, and I think (not totally sure) you need spaces between "&&". note that I removed the brackets. They are not really needed in this case, but they can be used if you want them. ALso this part in your script...

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

Didn't make sense to me. I assumed "amp" was another boolean condition and wrote it as so in the corrected version.

Also note this section...

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

{_wl=_wl+1; goto "xxx";}

Was changed to this...

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

_wl=_wl+1; goto "xxx";

Note that "?" statement is not the same as an "if" statement. With the "?" statement the command at the end cannot be put in quotes. Only with the "if" statement do you put the resulting command in quotes.

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

if (_y>1) then {_y=0}

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

? _y>1:_y=0

Also...with standard flashpoint scripts, there is no need to terminate a line with a semicolon, as flashpoint reads standard scripts line by line anyway. I've noticed that many people with prior programming knowledge do this out of habit. I came into flashpoint with asolutely no prior programming experience, and since learning to script in OFP, I've learned other languages suchs as VBSCript and php. Only afte learning a couple of 'real' programming languages did I realize the little idiosyncrasies of OFP scripting, and why they sometimes drove many programmers nuts.

Edit: Oops - fixed some syntax errors of my own! :P

Share this post


Link to post
Share on other sites
Script after corrects still dosn't work but i have next questions:

1. Is such construction allowed:

? (condition): { something; something; ? (condition): something;}

I mean are nested conditions' loops allowed?

2. I have two variables x and y.

_x==1 - is this comapre value x with 1?

y==1 - is this compare adress y with 1?

3. I have in trigger expression x=true. Is:

if(x) then .....

is correct notation? I mean is "true" frase equal to "1" or something like this?

I have propossition: maybe someone from BIS should to release some publication with correct notations?

According to conditions in brackets i know this is standard notation in c++.

1) You just forgot a bracket. I would advise to use the if-then form, like this:

if (condition) then { statement; statement; if (condition2) then { statement; }; }

Makes code easier to read, and BIS will probably prefer the if-then format too.

2) Correct.

(1==0) will result in false

(1==1) will result in true

3) Correct, incorrect.

_x is a private variable (think of it as a normal C++ variable). It can only be accessed by the script that is using it. If you use an underscore as the first character of the variablename, it will make the variable private.

x is a global variable. It will be accessible by all scripts in the game.

x is not the same as _x !!!

_x and x are two different variables!!!

Use the same rule as you use for C++: use global variables only if you must.

3) You should use brackets after the if-then statement

x=true

if (x) then { statement }

But there's no typecasting as in C/C++

(1==true) will result in false

("true"==true) will result in false

(1=="true") will result in false

The only sort of typecast that you can do is by using the format command. It will create a string.

Example:

format ["%1 - %2 - %1", 1, "text"]

Result is "1 - text - 1"

Official command reference:

http://www.flashpoint1985.com/docs/comref_102002/comref.html

OFPEC command reference:

http://www.ofpec.com/editors/comref.php

edit: so 2 people beat me to the post... tounge_o.gif

Share this post


Link to post
Share on other sites

OK. My script is now working!!! This is universal lap counter (witch 12 checkpoint):

_el=0

_wl=0

#update

if (e1&&e2&&e3&&e4&&e5&&e6&&e7&&e8&am

p;&e9&&e10&&e11&&e12) then {_el=_el+1; goto "loop_east";}

if (w1&&w2&&w3&&w4&&w5&&w6&&w7&&w8&am

p;&w9&&w10&&w11&&w12) then {_wl=_wl+1; goto "loop_west";}

goto "update"

#loop_west

if (_wl<5 && _el<5) then {titletext [Format ["west: %1 lap - east %2 lap", _wl, _el],"plain down"] }

if (_el>4 || _wl>4) then {goto "end"}

w1=false

w2=false

w3=false

w4=false

w5=false

w6=false

w7=false

w8=false

w9=false

w10=false

w11=false

w12=false

goto "update"

#loop_east

if (_wl<5 && _el<5) then {titletext [Format ["west: %1 lap - east %2 lap", _wl, _el],"plain down"] }

if (_el>4 || _wl>4) then {goto "end"}

e1=false

e2=false

e3=false

e4=false

e5=false

e6=false

e7=false

e8=false

e9=false

e10=false

e11=false

e12=false

goto "update"

#end

if (_wl>4) && (_el<5) then Titletext ["player WEST win","plain"]

if (_el<5) && (_wl>4) then Titletext ["player EAST win","plain"]

exit

Thanks all people for help (many thanks for Matthijs).

Suma:

1. if (condition) then { something; something; if (condition)then {something};} dosn't work.

2. I must to say that: this script language is weird (stupid?) and unintuitive. I hope You coded OFP in some more friendly one.

3. Thank You for link, but this is not enough i think.

Geetings.

Share this post


Link to post
Share on other sites
1. if (condition) then { something; something; if (condition)then {something};} dosn't work.

You can nest if, the, else statements..

Here is an example...

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (!alive soldier1) then {hint "soldier1 died"} else {if (!alive soldier2) then {hint "soldier2 died"} else {if (!alive soldier3) then {hint "soldier3 died"} else {if (!alive soldier4) then {hint "soldier4 died"}}}}

Edit[/b[ Oh I see you're not trying to do what I posted.

YOu probably can't put an "if" statement in the same line as an old style OFP scripting command. "If then" and "while do" statements are handled differently from other commands in flashpoint.

There is a syntax error there...

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

..thre needs to be a space after the second (condition)

Share this post


Link to post
Share on other sites
(...)

2. I must to say that: this script language is weird (stupid?) and unintuitive. I hope You coded OFP in some more friendly one.

3. Thank You for link, but this is not enough i think.

Geetings.

The SQS form is not perfect, but that doesn't make the scripting language crappy. If you want more structur, try creating SQF scripts. Even though it's fairly simple, it's also very powerful and forgiving.

Don't crap on this game, it's the best ever made.

3. Seems like it is perfectly suited for the rest of the community. What could you want more? The OFP source code?

(Oooh.... Suma... could we?)

Share this post


Link to post
Share on other sites

I am no expert but it does look like at the end, you have two tests.

The first test is:

if (_wl>4) && (_el<5) then Titletext ["player WEST win","plain"]

This says if WEST laps is GREATER than 4 and EAST laps is LESS than 5.

The second test is:

if (_el<5) && (_wl>4) then Titletext ["player EAST win","plain"]

This says if WEST laps is GREATER than 4 and EAST laps is LESS than 5.

Catch that? Should be like this  

(_wl>4) && (_el<5)

(_el>4) && (_wl<5)

Not sure if that will help your problem, but it certainly isn't going to work right like it is.

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  

×