OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: laggy on 25 Jan 2009, 19:44:05

Title: SQF structure question
Post by: laggy 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
Title: Re: SQF structure question
Post by: Mandoble 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.
Title: Re: SQF structure question
Post by: laggy on 25 Jan 2009, 23:47:32
Thanks,

That gave me some hints.

Laggy
Title: Re: SQF structure question
Post by: Rommel92 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;
};
Title: Re: SQF structure question
Post by: Mandoble 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
Title: Re: SQF structure question
Post by: i0n0s 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.
Title: Re: SQF structure question
Post by: Rommel92 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.
Title: Re: SQF structure question
Post by: Xeno 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
Title: Re: SQF structure question
Post by: Rommel92 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.
Title: Re: SQF structure question
Post by: Mandoble 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.
Title: Re: SQF structure question
Post by: Ext3rmin4tor 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
Title: Re: SQF structure question
Post by: Deadfast on 01 Feb 2009, 12:58:22
My personal guess is because it's so stretched to the side ;)
Title: Re: SQF structure question
Post by: i0n0s 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 {.