OFPEC Forum
Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started 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
-
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.
-
Thanks,
That gave me some hints.
Laggy
-
Maybe a 'complex' but easy to understand example:
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.while {time < 10} do {
if (time > 2) then {
}
//...
sleep 1;
};
-
And now a style tip for better reading of sqf blocks:
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
-
And now a style tip for better reading of sqf blocks:
*scared*
I prefer the C(++) style from Rommel more than the 'pascal' style.
-
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.
-
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
-
Could not agree more, spaces add to file size, and are unnecessary formatting since tabs allow for the same readability, and more productivity.
-
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.
-
Why did you call it "Pascal style"? Pascal doesn't have curly brackets, just "begin" and "end" statements
-
My personal guess is because it's so stretched to the side ;)
-
Pascal style:
if (bla) then
begin
...
end;converted to sqf:
if (bla) then
{
...
};It is just a matter of style, the more important thing is that you increase the indentation after the {.