Home   Help Search Login Register  

Author Topic: Tutorial: Basics of SQF (ACCEPTED)  (Read 15091 times)

0 Members and 1 Guest are viewing this topic.

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Tutorial: Basics of SQF
« Reply #15 on: 17 Mar 2007, 19:01:11 »
alive is a command which returns one of two values.

Either true (1) or false (0).

So either fred is alive or he is not alive.

alive is therefore not a variable with a value so nothing can be equal to it, it returns a value depending on the result of the check on whether something is alive or not.


Planck
I know a little about a lot, and a lot about a little.

Offline geezerjo

  • Members
  • *
Re: Tutorial: Basics of SQF
« Reply #16 on: 17 Mar 2007, 20:26:18 »
Your right, mayby i should have written my example like this ...

if !(alive _gunarray [ 0]) && !(alive _gunarray [1]) then
{
enemy_cleared=1;
};


Offline Cheetah

  • Former Staff
  • ****
Re: Tutorial: Basics of SQF
« Reply #17 on: 18 Mar 2007, 10:25:03 »
Sorry, I haven't been too clear on that example. It was more to show how to convert a script from SQS to SQF, it's not meant to be a logical one. Might have to explain what the enemy_cleared variable does.

In short: the variable is set to 1 in a trigger (note that a boolean type would've been better), so outside of the script.
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline Tom_Anger

  • Members
  • *
    • Shadow Company Elite (SCE_FATMAN)
Re: Tutorial: Basics of SQF
« Reply #18 on: 23 Mar 2007, 06:44:45 »
Cheetah - excellent work bro.  I have no OFP or ArmA pre-experience and  come from a large family of programming language from basic to fortran to pascal to C+ and work for a software company using our own language (over 11 years now - work 3rd shift and love it).  This .sqs, .sqf logic has some similarities so I am able to put your notes into perspective.  Understanding the looping process, variables & how they are assigned, incrementede, etc helps.

Scripts using your tut can help a mission maker understand alot for folks like me who have little to no knowledge of the language.  I see alot of .sqs scripts out there in the community so it seems to me that OFP folks took what they had in OFP and used them to make ArmA missions.  Having said that - I am left to believe that some .sqs scripts may not work, but most probably do.

Being able  to write .sqf, .sqs, or understand it helps.  Knowing the proper way to setup and call them from the mission editor goes a long way.  I stopped half way from reading your tut and wanted to just let you know this is a tremedous help for me.  I am a gatekeeper for my companie's knowledge base system and I write articles in the format for "dummies" for the lack of a better term.  The little things we (you) do now is much appreciated.  Thanks.
« Last Edit: 23 Mar 2007, 07:23:00 by Tom_Anger »

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re: Tutorial: Basics of SQF
« Reply #19 on: 10 Sep 2007, 12:40:31 »
Just something that may need clarified, if you want an unconditional exit in a function will ...

Code: [Select]
if (true) ExitWith {}
Work, or does ExitWith NEED an action? 
Great Job on the tut Cheetah, really helped me avoid a load of trial and error work  :P

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Tutorial: Basics of SQF
« Reply #20 on: 10 Sep 2007, 12:51:14 »
exitWith exits the execution of a context, which doesnt mean it will exit a script. A context is defined, for example, by while loops, for loops, if blocks, etc. To simplify, if you have code between {}, then you have a context.

Code: [Select]
while {true} do
{
...
   if (!alive player) exitWith {}
...
};
// Previous exitWith will jump here (out of the while context).

// This exitWith will effectively end the script as it is in the top most context level.
if (!alive player) exitWith {}

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re: Tutorial: Basics of SQF
« Reply #21 on: 10 Sep 2007, 12:58:32 »
Absolute legend, thanks Mandoble.  This would be a worthwile inclusion in the comref since it is a definite difference to the 'exit' command in OFP, what do you think?

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Tutorial: Basics of SQF
« Reply #22 on: 10 Sep 2007, 13:11:04 »
The key point here is to understand that SQF is focused to structured programming, which also means you should not exit loops directly but using the general loop condition (whiles and fors). If you follow some quite basic structured programming rules, you will never need to use exitWith.

More info about exitWith here.

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Tutorial: Basics of SQF
« Reply #23 on: 10 Sep 2007, 13:20:12 »
Extra info above added to comref.


Planck
I know a little about a lot, and a lot about a little.

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re: Tutorial: Basics of SQF
« Reply #24 on: 10 Sep 2007, 13:24:31 »
I have a slight grievance with the view that unconditional exits from scripts are unneccssary, if there is no other way to prematurly stop the execution of a function in ArmA then this would be a good addition to the comref.

Sometimes complex functions or functions executed multiple times simultaneously require premature cessation to save processor power and make the game a bit more playable.  I know I had to use this alot in OFP and in my experience of programming having a 'halt execution of script' command is very useful.

I understand many programmers berk at having an exit command but I think the view is a little eliteist as these commands (however inefficent) do make programming easier, and that the whole point of a higher programming language is it not?

EDIT: Thanks Planck, little hints like this are what make the comref a lifesaver for part-timers like myself  :D

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Tutorial: Basics of SQF
« Reply #25 on: 10 Sep 2007, 13:42:10 »
Well, if you need many potential exit points in a SQF script with has many blocks and blocks inside blocks and you dont want to mess with exitWith from every block level you may do the following:

Code: [Select]
// CustomExecute.sqf
_params = _this select 0;
_script_name = _this select 1;

my_condition_to_exit_var = false;
_script_handler = [_params, "my_condition_to_exit_var"] execVM _script_name;

waitUntil {scriptDone _script_handler || my_condition_to_exit_var};
if (my_condition_to_exit_var && !(scriptDone _script_handler)) then
{
   terminate _script_handler;
};

To force the termination at any point within your script you first get its params and the name of the variable to flag the immediate termination:
Code: [Select]
_normal_params = _this select 0;
_termination_var_name = _this select 1;

// Now, to terminate at any point the script
call compile {format["%1=true",_termination_var_name]};

This way (call compile {format["%1=true",_termination_var_name]};) you would have the very same functionality as will exit command for SQS scripts.