OFPEC Forum

Editors Depot - Mission Editing and Scripting => Arma2 - Editing/Scripting General => Topic started by: Dajan on 16 Jan 2010, 19:38:25

Title: To sqs or sqf, that is the question...
Post by: Dajan on 16 Jan 2010, 19:38:25
In OFP I didn't script anything - it was a complex world I didn't think I'd every want to touch;

In Arma I learnt to write very basic scripts for the thing I needed the most - custom loadouts. Back then I kept several dozen of my most used loadouts in .sqs format in a .pbo in my addons folder, updated regularly when I found new mods or wanted to create a rag-tag army of misfits.

In Amra II I'm told my L33T scripting skills are now obsolete and the .sqf is in town to beat on me...

So the question:-

As loadouts are likely to stay my most used scripts do I need to learn to use .sqf files or can I stick to the aging, fragile beast that is the .sqs?
If the New-Shiny-FormatĀ© is the way then could anybody see their way to maybe providing me with a commented example of how, with instructions on how to implement it?

TIA
Title: Re: To sqs or sqf, that is the question...
Post by: i0n0s on 17 Jan 2010, 01:01:57
Which question?
You're don't code for the original OFP, so you use SQF.

For implementation you simple use execVM instead exec and you should take a look at this guide:
http://community.bistudio.com/wiki/SQS_to_SQF_conversion (http://community.bistudio.com/wiki/SQS_to_SQF_conversion)
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 17 Jan 2010, 15:27:41
Ah, right.. Sounds like it's back to school for me  :confused:
 Thanks i0n0s, will have a read

:edit:

okay.. busily getting my head round that..
when I was using the old .sqs method I would create a script like:-
Code: [Select]
;alter loadout

_who = _this
removeallweapons _who
;unit is now empty

_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
_who addmagazine "magazine_name"
;primary/secondary weapon 12 slots (use throw for magazine_names)

_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
_who addmagazine "pistol_magazine_name"
;pistol 8 slots

_who addweapon "binocular"
_who addweapon "nvgoggles"
;misc 2 slots

_who addweapon "secondary_weapon_name
_who addweapon "pistol_name"
_who addweapon "throw_for_grenades"
_who addweapon "primary_weapon_name"
;Weapon slots slots

_who selectweapon "primary_weapon_or_muzzle_name"

exit
Do I need to alter this over much? I can't see anything that makes it look like I do except changing how bits are commented out..?
Title: Re: To sqs or sqf, that is the question...
Post by: i0n0s on 17 Jan 2010, 16:22:22
The comment-style and the EOL parenthesis:
http://pastebin.jonasscholz.de/pastebin.php?diff=493
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 17 Jan 2010, 17:23:53
Thanks!
Does it not need exit on the end anymore?
Implementing at mission start is:
Code: [Select]
nul0 = [unitname] execVM "myscripts\scriptname.sqf"in the init of each soldier I want to rearm yes?
Sorry for the barrage of probably frequently-asked and newb-level questions, it's just any documentation is written by those who understand for those who understand a little less rather than those who really, honestly don't have much of a clue  :-[
Title: Re: To sqs or sqf, that is the question...
Post by: i0n0s on 17 Jan 2010, 17:51:08
Exit is not needed any more.

And you need to use:
Code: [Select]
nul0 = unitname execVM "myscripts\scriptname.sqf"
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 17 Jan 2010, 17:56:20
Ta i0n0s
Title: Re: To sqs or sqf, that is the question...
Post by: Gielovic on 19 Jan 2010, 21:24:33
I see the solution is already presented.

One small remark for you in the feature on your last question.

Sometime you scripts getting executed (called) like this:
Code: [Select]
variable = nameofsomeunit execVM "script.sqf"and sometimes you see
Code: [Select]
variable = [nameofsomeunit] execVM "script.sqf
This is due to the way the _this variable in the script is used.
In your case you use in the first line of (real) code :
Code: [Select]
_who = _thiswhich means you give the value of the phrase before 'execVM' to the variable _who. In this case, this will be some unit.

You could also pass a list of values to the script, a so called 'array'. An array is denoted by the square brackets with comma seperated values in between.
if your first line of code would be
Code: [Select]
_who = _this select 0 you should have passed  an array to the script ([nameofsomeunit]), and the first element (with index 0), would be assigned to the variable _who.
More on the select statement can be found in the Biki (http://community.bistudio.com/wiki/select)

This way you can make scripts to which you can pass multiple units, or other elements like static objects, tanks, groups of units, etc...

A nice tutorial (http://www.ofpec.com/ed_depot/index.php?action=details&id=390&game=ArmA)
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 20 Jan 2010, 01:18:33
I could just do this in a trigger to run for multiple units?! Wow! Maybe I should have learned this stuff before  :)
Unfortunately that tutorial wasn't available when I tried just now, but thank you - really - for the help. I'll see if I can get it all into my head  :clap:
Title: Re: To sqs or sqf, that is the question...
Post by: Planck on 20 Jan 2010, 01:41:57
The tutorial above has been available since 2007, how could you miss it?


Planck
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 20 Jan 2010, 11:53:02
As I only started trying to script a year ago and never tried .sqf until everyone started saying .sqs was obsolete for Arma II. Some of us panic when trying to learn a new language without a teacher there to hold our hands you see.. :confused:
Title: Re: To sqs or sqf, that is the question...
Post by: Zipper5 on 20 Jan 2010, 14:58:22
I wouldn't say there are amazing benefits to using SQF over SQS besides better, cleaner formatting and marginally faster processing. However, performance in ArmA II is a major issue for many people, so I took it upon myself to learn how to write clean SQF scripts for my ArmA II missions. I found it as hard as you did at first, since I knew much more about SQS, but now, I'm just as good with SQF as I was with SQS. Just takes practice.

I'd say use whichever you're most comfortable with using. Rarely do people unpbo missions and look at them themselves, and when they do, they rarely go "ZOMFG SQS SCRIPTING!!111one", lol. I wouldn't say the benefits of SQF scripting are enough to make SQS obsolete.
Title: Re: To sqs or sqf, that is the question...
Post by: h- on 20 Jan 2010, 15:53:41
Quote
marginally faster processing
IMHO it's much more than marginally faster.
In my experience sqf beats sqs in performance 100-0, but of course it depends on what you script. With some simple things you won't see the difference.

I had coded mind boggling amounts of sqs code in OFP and when I initially started "Armatizing" them especially scripts with heavy looping caused significantly lower FPS hit when converted to sqf.
Where sqs would cause huge, even eye detectable FPS drop the same thing in sqf wouldn't cause any, not even when measuring with Fraps..
Title: Re: To sqs or sqf, that is the question...
Post by: DeanosBeano on 20 Jan 2010, 16:41:03

 for me its a simple equation :).
 
if my script runs once and has no loops or ifs ands or maybe and it works straight from ofp-arma then i leave it.
 I have hundreds if not thousands of scripts from ofp and to be honest if it aint broke there is little point fixing it

 The pros of using SQF are speed and consistency and   that you will require it at some point and so why not learn it.

 The pros of sqs are , if you have one that works and isnt causing lag and it fires even during periods of bad FPS , then slap it in there why waste time :.

Sqs will never die, it will merely be converted by others and called there own.
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 20 Jan 2010, 19:46:15
Thanks for the reassurance guys, I love you all  ;)
Title: Re: To sqs or sqf, that is the question...
Post by: nominesine on 21 Jan 2010, 18:19:27
...never tried .sqf until everyone started saying .sqs was obsolete for Arma II...

My output has admittedly been very low since the release of ArmA2. But I still write scripts every now and then, and I still use sqs from time to time. That way I can use script snippets from the early OFP-days, with a simple cut and paste.
Title: Re: To sqs or sqf, that is the question...
Post by: Gielovic on 23 Jan 2010, 15:55:09
marginally faster processing.

If I remember correct, SQF was first only used for creating new functions and wasn't at all useful as a replacement for SQS because the game was suspended  until the function you created finished.

I think this is still the case if you compile the sqf-script as a function and then call it. With execVM the script is interleaved with the main program thread and it runs like sqs does (next to the 'game' itself).

Correct me if I'm wrong.

Of course SQF is much more convenient as it looks more like modern day programming languages, with control flow structures like while and for loops.
Also consider Dijkstra's statement: 'goto's are harmful!'  -exactly the statements which are used for looping in SQS- and the conclusion is inevitable:  use SQF!

Edsger Dijkstra (dutch) (http://nl.wikipedia.org/wiki/Edsger_Dijkstra)
Edsger Dijkstra (english) (http://en.wikipedia.org/wiki/Edsger_W._Dijkstra)
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 05 Feb 2010, 22:00:49
Sorry to reopen this but I tried experimenting earlier with some of what has been said here, specifically advise from Gielovic:
Quote
You could also pass a list of values to the script, a so called 'array'. An array is denoted by the square brackets with comma seperated values in between.
if your first line of code would be

Code: [Select]
_who = _this select 0 you should have passed  an array to the script ([nameofsomeunit]), and the first element (with index 0), would be assigned to the variable _who.
More on the select statement can be found in the Biki

This way you can make scripts to which you can pass multiple units, or other elements like static objects, tanks, groups of units, etc...
I took this to mean that i could, say rearm a group of units via a simple .sqf by using a trigger with the following it its on activation field:
Code: [Select]
nul0 = [tom,dick,harry] execVM "myscripts\newloadout.sqfhowever even with the suggested change to the first line of code this only rearm the first unit (i.e. tom)
Did I misunderstand something in Gielovic's advice or am I missing a vital step somewhere?
Title: Re: To sqs or sqf, that is the question...
Post by: DeanosBeano on 05 Feb 2010, 22:20:41

maybe you need in script another 2 x who

you send to script [tom,dick,harry]

script
reads

tom =  _this select 0
dick =    _this select 1
harry  = _this select 2

so
_who = _this select 0 // this means tom  and so later in script if you only say

_who addmagazine _x   // then only tom will add the magazine


you will need _who1 =_this select 1  and _who2 =_this select 2   

and then _who2 addmagazine _x     and _who3 addmagazine _x    etc.

 i must say here tho its already becoming too much  and you need to read about array and or Foreach.

i am busy at moment hope this helps a little and some other can contribute to finish.


Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 05 Feb 2010, 22:26:29
Ah thanks DeanosBeano, think I did the classic jumping feet first without checking the water! I'll get back on the BIKI and see if I can work it out  :-[
Title: Re: To sqs or sqf, that is the question...
Post by: Gielovic on 05 Feb 2010, 22:44:56
passing an array of values gives you the possibility to use more than one input parameter.
I think its good practice to always use an array.
What your want to do actually is rewriting the script so that not a unitname has to be passed but an array (list) of units is passed to which the script is applied iteratively.

This can be done by creating a newscript say applytoall

which does the following
Code: [Select]
inputlist = _this select 0 //The first element passed has to be an array
// So call this function by [[element1,element2,..,elementn]] execVM ...

//This executes your original script for each individual unit.
{nul = [_x] execVM yoursoriginalscript.sqf } forEach inputlist


These scripts can be merged very easily but it wouldn't be a bad idea to seperate them.
Perhaps you can compile your original script and make it an actual function that can be called using the call (http://community.bistudio.com/wiki/call_body)  operator.
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 05 Feb 2010, 23:04:35
Head... starting.. to... spin...
Thanks gielovic, will look into it!
Does anyone know of any missions out there where someone used .sqf and arrays in the fashion I'm talking about - I might be able to get my head round this faster if I can examine something that works and do some re-egineering of it?
Title: Re: To sqs or sqf, that is the question...
Post by: laggy on 06 Feb 2010, 03:04:08
Hi Dajan,

To sqs or sqf? I find there is a lot of "snobbyism" here... my personal opinion.

Arma2 still supports .sqs which tells you it is not obsolete yet.

People in general advocate .sqf even when it isn't needed, that's my view. I guess some of us are just interested in making our missions/scripts to work, some people want a very clean code as a first priority.

When I design missions, I just want stuff to work the way I intended, and often .sqs is the simplest (old fashioned way) to do that. .sqs is my spontaneous way of scripting (since OFP days) and only if it's really necessary I will convert it into .sqf.

For example: If the script has to be run very often during game, I try to use .sqf because it supposedly demands less resources from your pc, but a script that only runs once (like camera/intro) runs just as well (or better) in .sqs.

Anyway, that's my view (but I'm still an old time OFP scripter)

Laggy
Title: Re: To sqs or sqf, that is the question...
Post by: zonker3210 on 06 Feb 2010, 03:12:37
I'm not sure if this is the sort of example for which you're seeking but...

In some missions I've made, I like to add IEDs via Jeevz' IED script. (Note that it's in SQS format! ;) ) Anyhow, I didn't like having specific IED locations...I wanted them to be random so that players couldn't be sure whether or not it was safe to walk near that car...that garbage can...that pile of trash near the road. So I wrote a script to randomize the locations of the IEDs. To use it, you simply place a series of empty objects on the map, name them (i.e., "ied1", "ied2", "ied3", etc.) and then add the following line to the mission's INIT.SQF file...

Code: [Select]
[
  [
    [ied1, "Large", 15],
    [ied2, "Huge", 30],
    [ied3, "Large", 15]
  ]
] execVM "IED_Randomizer.sqf";

Essentially what this does is it identifies possible IED locations in the map (ied1 thru ied3) and specifies both the size of each IED (Small, Medium, Large, or Huge) and the distance at which it will be triggered. For example, if ied1 is made active, then it will be a Large IED triggered when someone comes within 15 meters of its location. Whether or not a particular location is made into an actual IED is based on the script equivalent of flipping a coin ("round(random 1)").

Well, I'm sure you get the idea so here's the randomization script itself (see below). Note that the input parameters are in nested array form and I can add more possible IED's simply by adding more entries to the parameter array.

Hope this helps,
 -z

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
IED_Randomizer.sqf
Code: [Select]
// ----------------------------------------------------
//
//  IED_Randomizer.sqf by zonker3210
//  created: 10-Jun-2009
//
//  Intended for use with the IED.SQS script made
//    by Jeevz. The IED.SQS script may be found here:
//
//    http://www.armaholic.com/page.php?id=504
//
//  The script below allows for somewhat random IEDs
//    from a pool of potential IEDs. Should be
//    referenced in your INIT.SQF file like so...
//
//    [
//      [
//        [ied1, "Large", 15],
//        [ied2, "Huge", 30],
//        [ied3, "Large", 15],
//        [ied4, "Small", 5],
//        [ied5, "Medium", 15]
//      ]
//    ] execVM "IED_Randomizer.sqf";
//
// ----------------------------------------------------

if !(isServer) exitWith{};

private ["_trigger", "_activateIED", "_iedArray"];
_activateIED = 0;
_iedArray = _this select 0;

if (isNil "Ied_Setup_Completed") then
{
  //loop thru each item in the array of potential IEDs...
  {
    // get a pseudo-random number; 0=false, 1=true
    _activateIED = round(random 1);
   
    if (_activateIED > 0) then
    {
      // ------- debugging statement -------
      //hint format["%1 is active", _x];
      // -----------------------------------
     
      // Create a trigger at the location of the potential IED. When activated,
      //  the trigger will call the IED.SQS script using the specified IED object
      //  and blast size.
      _trigger = createTrigger["EmptyDetector", (getPos (_x select 0))];
      _trigger setTriggerArea[(_x select 2),(_x select 2),0,false];
      _trigger setTriggerActivation["WEST","PRESENT",false];
      _trigger setTriggerStatements["(thisList select 0) isKindOf 'Man'", format["[%1, ""%2""] exec ""ied.sqs""", (_x select 0), (_x select 1)], ""];
    };

    sleep .5;
  } foreach _iedArray;
 
  // set the nil variable with a default value for server and both JIP & 'join at mission start'
  Ied_Setup_Completed = true;
  publicVariable "Ied_Setup_Completed";
};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 06 Feb 2010, 20:18:13
Hmm.. thanks zonker. I'll have a toy about. I don't suppose you've published any of these missions have you? A little hands-on can solve some of my more pointless questions
Title: Re: To sqs or sqf, that is the question...
Post by: zonker3210 on 07 Feb 2010, 01:30:22
The only one I released to the public was Afghan Village Raid (http://www.ofpec.com/forum/index.php?topic=33552.0) but that was an Arma mission and it requires four addons. If you can wait a day, I'll put something together for Arma2....just a quick example using Jeevz' IED script and the randomizer script mentioned above.

Another (and possibly more helpful) example of passing arrays as parameters in script calls is shk's Taskmaster script (http://forums.bistudio.com/showthread.php?t=86206). Depending on how comfortable you are with scripting, it might be a bit tough to follow all the way through, though.

I'll try to get a basic example together by tomorrow.

UPDATE: sample attached
Title: Re: To sqs or sqf, that is the question...
Post by: Dajan on 07 Feb 2010, 13:56:28
@zonker
You just became my own personal god. Thanks for taking the time to do this, I'll give it a spin asap! :good:
Title: Re: To sqs or sqf, that is the question...
Post by: Gielovic on 07 Feb 2010, 23:34:47
some on-topic then..

I guess some of us are just interested in making our missions/scripts to work, some people want a very clean code as a first priority.

Sure it needs to work, and that is why you shouldn't use SQS :P. It is not a coincidence that for example BASIC evolved into a form which does not have goto statements.

Programs (and ofp scripts) are way easier to understand if they use standard control flow structures like while loops and if statements. It is much easier to make a mistake when using goto's.

I do agree with you that SQS suffices if you are making cutscenes and you just needs to execute a list of camera actions. But for the actual scripting I would prefer using SQF,