I have some experience that when a goto is executed the program pointer goes back to the top of the script and then searches forward until it finds the lable.
Very interesting, and I'll definately take note of that. From now on, if I have any loops, they will go at the top of the script!
The beauty of this is, the function, which is basically a linear script is already in memory, it doesnt require the hard drive to be accessed or data written across to ram, its much more instant and therefore a lot less cpu intensive
This _might_ be a fundamental difference between sqs and functions. Maybe, perhaps, and well worth checking.
Err... yes and no. A "function" is just a STRING, which is executed as code via the "call" command.
Standard practice is to save these strings into a variable, with something like
my_func = preprocessfile "blah.sqf" or
my_func = loadfile "blah.sqf". The difference between "preprocess" and "load" is that all the comments are taken out of preprocessed files (I believe--although it would be simple to check).
Since this variable is saved, it would make sense that OFP could access it faster, since it doesn't have to load up the file each time it is run. However, I doubt that OFP actually RUNS the code in any different way than normal script code (except that functions can't have delays or 'goto's of course).
So Terox's statement is somewhat correct. The .sqf file itself is nothing special. It is the act of storing these functions into a variable that makes them different than scripts. I had to point this out because look at Terox's example code:
?!(local server):exit
~ 3 + random 1
#INIT
_a = 0
#START
~1
if(tx_StopSqs)then{goto "END"}
_a = _a + 1
if((_a == 3)&&(({alive _x}count tx_destroy)==0))then {[b]call loadfile "tx_sys\server\CheckB.sqf"[/b]}
if(_a == 4)then {[b]call loadfile "tx_sys\server\CheckA.sqf"[/b]}
if ((_a == 5) && (count tx_damcon >0))then{{_x setdammage 0} foreach tx_damcon}
if(_a >=5)then{_a = 0}
goto "START"
#END
;; does some boring end of mission stuff then exits
exit
Notice that he LOADS the function every time he calls it. This is essentially the same as loading a script each time it is run, like OFP does. If he wanted "a linear script already in memory", then he should have loaded the file ONCE, and saved it into a variable (local or global).
But if he were to do that, then I think there would be some performance increase--at least in very fast looping scripts that repeatedly execute another script (or function).
Another great thing about functions is that line breaks are indicated by semicolons; instead of the 'return' key, so you can lay out long strings, if/then, or do/while conditions in a neat way.
EDIT
I think you guys already mentioned this, but I'm kinda dense
. Anyways, if a "goto" causes the script to be read again from the start, then that might explain why an @condition usually runs faster than an very short "goto" loop.
The @condition is prolly checked every clock cycle or so--while the "goto" statement causes the script to be searched thru until the label as mentioned. Unless the label is near the top of the script, then there might be quite a bit of code for OFP to search thru--every 0.5 seconds or so, depending on the delay.
So which is faster? Searching thru X lines of code to find a label, then checking a condition, every 0.01+ seconds? Or checking a condition every 0.001 seconds? I imagine the answer is going to be a combination between how much code is above the label, and how complex the condition being checked is. With that in mind, it seems likely that an @might be faster than a ~0.5 delay, if it is down under a bit of code.
Good stuff to keep in mind, instead of just blindly making loops everywhere, in fear of using an @condition.
Oh yeah, and I have more evidence to support this (labels being searched for from the top): if you ask a script to "goto" a non-existant label, the script will exit instead of giving an error or continuing on--indicating that it just kept searching until the end of the file, as opposed to 'preprocessing' the labels on first execution.