Home   Help Search Login Register  

Author Topic: SQF structure question  (Read 2012 times)

0 Members and 1 Guest are viewing this topic.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
SQF structure question
« on: 25 Jan 2009, 19:44:05 »
Hi,

I read Cheetahs good SQF tutorial, but an important questionmark still remains.

In many advanced SQF scripts (way over my level) I see many different WHILE  and IF and DO loops merged together into one big complex script.

What I don't understand is how these complicated structures work, meaning when does a loop switch to the next loop with new conditions, when are they skipped, when are they ALL running.

I also don't understand the efficient way to exit a script when let's say the unit running the script dies.

I hope someone has the patience to explain this, even if it is "simple" to you.

Laggy
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: SQF structure question
« Reply #1 on: 25 Jan 2009, 20:05:53 »
Do you have some example of these to work with? General speaking whiles, ifs, etc are not exactly just merged together but contained, that is a block may contain another blocks, and a block is whatever starts with { and ends with }.

And beware with these blocks, because they define scopes for local variables. If a local variable has not been defined in a higher level block and it is used in an inner level block, this variable will be visible for that block, but will not be visible outside of that block. So, using private command to declare all the local variables that you want to be "usable" anywhere within your script is a good practice.

Offline laggy

  • Members
  • *
  • "Behold a pale horse"
Re: SQF structure question
« Reply #2 on: 25 Jan 2009, 23:47:32 »
Thanks,

That gave me some hints.

Laggy
And I looked and beheld a pale horse and his name that sat on him was Death and Hell followed with him.

Offline Rommel92

  • Members
  • *
Re: SQF structure question
« Reply #3 on: 31 Jan 2009, 23:13:34 »
Maybe a 'complex' but easy to understand example:
Code: [Select]
while {time < 10} do {
if (time > 2) then {
} else {
while {time < 2} do {
{
_i = _x
} foreach [0,1,2,3];
};
};
sleep 1;
};
Explanation (by statement number):

1. Time == 0 at the beginning of a mission; so the while loop is 'true' as time(0) is less than 10.
2. At this point; time < 2, as time == 0; so the if statement is 'false'; it will turn to the 'else' statement.
3. The while {time < 2} do statement will now loop for 2 seconds straight constantly completing the for each statement.
4. Sleep 1 second (time will now be > 2 and _i = 3)
5. The code will now loop this part of the statement for the next 8 seconds.
Code: [Select]
while {time < 10} do {
if (time > 2) then {
}
//...
sleep 1;
};

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: SQF structure question
« Reply #4 on: 31 Jan 2009, 23:24:26 »
And now a style tip for better reading of sqf blocks:
Code: [Select]
while {time < 10} do
{
   if (time > 2) then
   {
   }
   else
   {
      while {time < 2} do
      {
         {
            _i = _x
         } foreach [0,1,2,3];
      };
   };
   sleep 1;
};

Opening and closing {} at the same level.
What an extremely weird example BTW  :P

Offline i0n0s

  • Former Staff
  • ****
Re: SQF structure question
« Reply #5 on: 01 Feb 2009, 00:22:12 »
And now a style tip for better reading of sqf blocks:
*scared*
I prefer the C(++) style from Rommel more than the 'pascal' style.

Offline Rommel92

  • Members
  • *
Re: SQF structure question
« Reply #6 on: 01 Feb 2009, 00:24:53 »
Haha, I must admit I used the pascal format up until recently, I never understood why people did the 'C++' format; but now it just appeals to me for readability, compression and precision in working through errors in the code.

And yes mando it was the most bizarre example I could come up with whilst staying simple, it was just to give an idea of control structures within each other... if that even makes sense.
« Last Edit: 01 Feb 2009, 20:42:23 by bedges »

Offline Xeno

  • Members
  • *
Re: SQF structure question
« Reply #7 on: 01 Feb 2009, 11:44:33 »
Completely agreed. I've never used that "pascal" style format (@Mando, use tabs, makes things much easier and even more readable than adding space chars or converting tabs to space chars).

Xeno
« Last Edit: 01 Feb 2009, 20:42:34 by bedges »

Offline Rommel92

  • Members
  • *
Re: SQF structure question
« Reply #8 on: 01 Feb 2009, 12:04:13 »
Could not agree more, spaces add to file size, and are unnecessary formatting since tabs allow for the same readability, and more productivity.
« Last Edit: 01 Feb 2009, 20:42:47 by bedges »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: SQF structure question
« Reply #9 on: 01 Feb 2009, 12:04:39 »
Well, a brief explanation:
If you use plain notepad (no editing aids like colouring, etc) like me, then:
1 - The {} at the same column helps A LOT trying to find missing ones.
2 - The 3 space instead of tab avoids having lines too displaced to the right, which avoid many cases of a screen having single code lines occupying more than one displayed line.

Offline Ext3rmin4tor

  • Members
  • *
Re: SQF structure question
« Reply #10 on: 01 Feb 2009, 12:11:07 »
Why did you call it "Pascal style"? Pascal doesn't have curly brackets, just "begin" and "end" statements
How can you shoot women and children?
Easy! Ya' just don't lead'em so much! Ain't war hell?!!

Offline Deadfast

  • Members
  • *
Re: SQF structure question
« Reply #11 on: 01 Feb 2009, 12:58:22 »
My personal guess is because it's so stretched to the side ;)

Offline i0n0s

  • Former Staff
  • ****
Re: SQF structure question
« Reply #12 on: 01 Feb 2009, 13:02:14 »
Pascal style:
Code: [Select]
if (bla) then
begin
  ...
end;
converted to sqf:
Code: [Select]
if (bla) then
{
  ...
};
It is just a matter of style, the more important thing is that you increase the indentation after the {.