Jump to content

Sign in to follow this  
fer

SQFs - Exemplar mission?

Recommended Posts

Although there has been a lot of talk about how missions for ArmA should use SQFs in preference to SQSs, unPBOing the missions that shipped with the product doesn't exactly reveal a set of missions brimming with new SQF goodness... whistle.gif

Is there an exemplar mission which shows those of us who rely on our half-baked understanding of how to build missions with whoary old SQSs how we can migrate to the shiny new architecture of SQF-led mission construction? huh.gif

- Fer <TZW> inlove.gif

Share this post


Link to post
Share on other sites

I am not really sure what to think of this either.

atm I think the terminology that is used is just confusing. Because you still got a lot of scripts ingame. But I think what is new is that they want you to use "new" (most of it was in OFP:R already) Control Flow Syntax instead of the old ?() : etc stuff. I think most of what they call function is not really a function but a script with the sqf syntax. At least for me a function is what you use with the call command and it should halt the program execution until it's done.

Share this post


Link to post
Share on other sites

Exactly my thoughts. Even in the Biki, the statements are using "scripts" and "functions" without really caring what they talk about, all the while stating some definite truth about "functions" that can't use wait command like sleep, and so on... Really confusing.

How I see it, and how I would describe it :

There are 2 types of codes in OFP/ArmA engine : "scripts" and "functions".

Scripts are asynchronous, if I may say so, in that the engine does not wait for the preceding command to be finished to process the next command. In scripts, you can use "goto" command, and commands to pause the script (sleep, @)

Functions are (if I understood well) synchronous, each command is executed sequentially, but you cannot insert "pauses". Functions return a value to the calling process.

Functions use a specific syntax, with if..then...else..., while...do, things like that.

Scripts CAN USE 2 different syntaxes in ArmA. Only 1 of the 2 is valid in OFP.

So, there's the old way to write scripts, the OFP-way, with ?(...):..., ~X, etc.. And there is the new way, valid in ArmA, which is the same way than functions are written (if..then..else, sleep, etc...)

You call functions, in OFP and ArmA, with the command "call"

You call scripts in OFP with the command "exec" (and ofc the old syntax)

You call scripts in ArmA either with the commands "spawn" (which takes compiled code as argument) or "execVM" (whoch takes a file name as argument) when you write your script with the new syntax, and you call scripts in ArmA with the command "exec" when you wrote it with the old OFP-style syntax.

And all this is not really clear in the Biki atm sad_o.gif

So unclear in fact that I may be completely wrong :P

Share this post


Link to post
Share on other sites

When alot of that stuff was written, we only had beta type comref from BI to work from. Now that many people have the game the articles should improve with discussions like this.

Share this post


Link to post
Share on other sites

The way it worked in OFP was that SQS scripts (that one started by means of "exec") had a set of instructions, that were all executed in order, and a sleep command (~float) as well as a wait until command (@condition) (or was that a wait while?) During which the script commands given would be showing their effects.

The SQF files were first run through a function preprocessfile, which returned a string. This 'code string' could then be called from other code, SQS script or trigger, by the call command. See http://community.bistudio.com/wiki/call_(OFP)

It ran through the instructions in the string from beginning to end, there being no concept of stopping anywhere to continue later.

Armed assault, however...

http://community.bistudio.com/wiki/Code_strings essentially says that 'text' and 'code' strings are now no longer freely exchangable.

Compile transforms a text string into a 'code string'. This will probably do some basic syntax checking too. What is loaded with loadfile and preprocessfile it probably just text in this context.

compilation can provide a number of advantages, including optimizations, and

perhaps also

Early error detection (at least for syntax errors)

Code strings can be loaded once into a global value and called whenever it's needed, at little computational cost.

The "call" function seems to remain the same as ever, except for the requirement to compile text strings into code strings.

The wiki gives no information about "exec" in armed assault. However, it presumably works still, causing one to run a sqs script like before. SQS scripts have been declared deprecated though, so it's possible this command will disappear eventually. A totally wild guess of mine is that the classic sqs execution actually does the compile command on each line every time it executes... which would make it unnecessarily expensive in cpu cycles.

There is a new "execvm" command. However, if I read it correctly,

something execvm "filename"

is equivalent to

something call compile loadfile "filename"

Now on to my suggested howto.

Create each script in the sqf format, with the sqf extension.

In an init script (I don't know if init.sqf will work, or if you have to create a trigger that runs it) run the following command for each file:

healplayer = compile loadfile "healplayer.sqf";

arty_fire = compile loadfile "arty/fire.sqf";

and then, whenever you need to run any of them,

call healplayer;

instead of the old exec "healplayer.sqs" as you would had it been an sqs.

You have to make sure not to overwrite the function with something else, though.

There's one question left which I am not able to answer - what if the scripter wanted that to run concurrently with the calling script?

This is only a guess of mine, but if the called script uses Sleep then control is passed back to the caller.

Someone please test that and report back.

However, considering my amount of practical experience with arma scripting, the above might conceivably be up to 99% bullshit.

Share this post


Link to post
Share on other sites

So, in the nicest possible way, nobody is really in a position to give a definitive response to my original request, eh? LOL - well, guess it's back to the old reverse-engineering process. Assuming there are mission PBOs that actually use the new approach, that is wink_o.gif

Share this post


Link to post
Share on other sites

Well I can at least give you a basic idea, from the differences I found since I start testing out the new scripts:

Exec:

The reason you will still see exec used quite a lot in the new Arma campaign is. You cant use execVM or Spawn directly from the init field of a unit in the editor. But you can from within a script launched with exec. So if you have a few scripts that need to be launched from an objects init filed, it's probably better to use exec.

If you want to use execVM or Spawn then you can do it like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Call {[AIR01,TANK02] ExecVM "Script.sqs"}

When using loops with execVM or Spawn you can't use the the labels for the goto command.

The first loop I set up, looked something like this:

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

_Exit=False;

While {!_Exit} Do

       {

       Player SideChat Format ["%1 %2",_Aircraft,Time];

       Sleep 0.1;

       };

This will loop indefinitely, I can see _Exit becoming a standard condition in most of my scripts. As ending nested loops, will be a little more complicated now. If you still want to continue executing the script.

But there are so many new commands for looping code in Arma. There may be better ways.

Exit:

It looks like you can't use the exit command with scripts launched by execVM or Spawn. You have to use terminate. It still works the same way with exec scripts.

When I'm sure of the results, I will try and add anything worthwhile to the wiki.

Share this post


Link to post
Share on other sites
  (UNN @ Dec. 10 2006,11:55) said:
You cant use execVM or Spawn directly from the init field of a unit in the editor.

correction, you can run execVM from an init field or a trigger etc. Didn't try it with spawn yet.

if you assign the return value (of type script) to a variable then it will work.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">foo = execVM "test.sqs";

EDIT:

However what I'd like to see is that there would be some discussion about setting a clear terminology for dealing with all the new things. I don't have a big problem finding out if and how stuff works myself but the Biki is pretty confusing. Especially the term "function" has been used too generally IMHO. What we do need is to define what is a function, what is a script. And to define how to deal with the differences between OFP and Arma. Once we have a clear terminology I think the Biki will become much more informative.

Share this post


Link to post
Share on other sites

I see thanks, that explains the error message.

I was aware there are some cases where Exec has to be used:

  [b said:
Quote[/b] ]There are even instances in the game where both new and old syntax is accepted, and old syntax is used for sqs extenstion (like init.sqs, init.sqf). --Suma 15:28, 27 September 2006 (CEST)

Thought that might have been one of them.

Share this post


Link to post
Share on other sites
  (Donnervogel @ Dec. 10 2006,14:12) said:
What we do need is to define what is a function, what is a script.

The difference is the same as it was in OFP. A function is executed all at once. The game will freeze until the function has returned. This is why you cannot use sleep in functions as it would freeze the game and thus be quite pointless. Functions are executed with the call command.

Scripts are run with exec command (Old style SQS scripts that are deprecated) or with execVM/spawn (New style SQF scripts). Scripts execute in parallel, you can have a script in an infinite loop and it will be executing parallel to the game itself, and parallel to other scripts.

The only difference between ArmA and OFP is that in ArmA you should be using the execVM command instead of exec command. The SQF scripts executed with execVM/spawn use the same syntax as functions did in OFP. So instead of using gotos to control the program flow you use if/for/switch/while/etc.

SQF scripts executed with execVM/spawn also are faster to execute than SQS scripts which is propably the main reason why they should be used.

In OFP you had:

SQS scripts - executed with exec command

SQF Functions - executed with call command

In ArmA you have:

SQS scripts - executed with exec command, this is deprecated and should not be used

SQF scripts - executed with execVM or spawn commands

SQF Functions - executed with call command

Share this post


Link to post
Share on other sites

Yes I think the definitions by Kegetys are great. If people agree with it it would be great if we could use this terminology on the Biki. Right now there are articles witch are confusing because it is a bit unclear what they mean with functions. Some used function as a synonym for an sqf file when I had a look a two days ago. However we need to make a clear distinction between SQF scripts and SQF functions. So it would be great if we could agree on a precise terminology.

Share this post


Link to post
Share on other sites

Someone also should write 'explain-this-to-a-5-year-old' formatted examples of the new flow control things, practical examples actually tested in game..

I have huge trouble trying to understand how these new things work since I have very litle experiance in any 'real' coding language..

The Biki examples are non-existent, or if there are examples they are written too technical; lot's of why instead of how (usually even why in some 'real' code), and have no use for average dumb ass like me...

  [b said:
Quote[/b] ]This will loop indefinitely

Until it reaches 10 000 loops and stops...

Only thing different with while/do in OFP is that this time you don't get any error messages, it just stops..

Share this post


Link to post
Share on other sites
  [b said:
Quote[/b] ]_Exit=False;

While {!_Exit} Do

{

Player SideChat Format ["%1 %2",_Aircraft,Time];

Sleep 0.1;

};

What? No break command? Or for that sake, continue?

  [b said:
Quote[/b] ]The difference is the same as it was in OFP. A function is executed all at once. The game will freeze until the function has returned. This is why you cannot use sleep in functions as it would freeze the game and thus be quite pointless. Functions are executed with the call command.

Sounds good, except... what do we use for both? If something in the biki can refer to either of them...

  [b said:
Quote[/b] ]When using loops with execVM or Spawn you can't use the the labels for the goto command.

Good riddance. yay.giftounge2.gif

Share this post


Link to post
Share on other sites

I agree with the OP, an example mission with some well commented SQF scripts showing how the basic structure and processing flow works would be ace wink_o.gif

Share this post


Link to post
Share on other sites
  [b said:
Quote[/b] ]The difference is the same as it was in OFP. A function is executed all at once. The game will freeze until the function has returned. This is why you cannot use sleep in functions as it would freeze the game and thus be quite pointless. Functions are executed with the call command.

I just realized you can use sleep with the call command, this works:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">Call {Player SideChat Format ["1 %1",Time]; Sleep 5; Player SideChat Format ["2 %1",Time]}

Don't know if this is down misreading the Wiki or a bug?

Edit: Doh, got my replies mixed up with another thread.

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  

×