Home   Help Search Login Register  

Author Topic: Making a loop in SQF  (Read 3821 times)

0 Members and 1 Guest are viewing this topic.

Offline Kimmeh

  • Members
  • *
Making a loop in SQF
« on: 27 Jun 2009, 18:40:29 »
Hey there ladies and gentlemen,

Firstly, hi! I'm kind of new here. I did read around here for a long time but I felt a bit intimidated by the complexity of some things and didn't dare to post because of that. >.<

Anyway, as to my question, which is probably very simple/obvious but trust me I have tried really hard to find an answer, looked through other people's SQF scripts, tutorials etc, but yeah... here goes.

I am trying to make a tiny little script to keep the artillery in the secops manager available, even when allready used. Now the radio trigger works of course but it's not very neat coz all players would see it. I'd prefer that the player would automatically get a new arty strike available. This is for multiplayer by the way, allthough I'm not sure that matters much in this case.

So yeah I find .sqs a little easier for some odd reason, but in this case I had to use a SQF command namely "waitUntil {_battery getVariable "ARTY_SPLASH"};" which resulted in an error when I used it in sqs, I think.

This is what I'm trying to do(BABUSHKA is the name of my arty module, FO is the forward observer-player):

#start;
waitUntil {BABUSHKA getVariable "ARTY_SPLASH"};
sleep 15 (so it has time to actually say 'splash out' in response to the artillery operator)
[["artillery_barrage"], FO, [[BABUSHKA, [1,2,3,4,5,6,7,8,9]]]] call BIS_SOM_addSupportRequestFunc;
goto "start";

Now before you facepalm, I _do_ realise goto isn't used in sqf, and that's where my problem lies. Now I'd only be able to re-add the artillery strike once, while I want to make it indefinately available.

Can anyone help me convert this to SQF, or find a better (probably way obvious) solution to what I'm trying to achieve?

Thanks a lot in advance for taking your time to read this and your patience.

Kimmy

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Making a loop in SQF
« Reply #1 on: 27 Jun 2009, 19:52:53 »
Code: [Select]
while { true } do
{
  waitUntil {BABUSHKA getVariable "ARTY_SPLASH"};
  sleep 15; // so it has time to actually say 'splash out' in response to the artillery operator.
  [["artillery_barrage"], FO, [[BABUSHKA, [1,2,3,4,5,6,7,8,9]]]] call BIS_SOM_addSupportRequestFunc;
};
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Kimmeh

  • Members
  • *
Re: Making a loop in SQF
« Reply #2 on: 27 Jun 2009, 20:59:09 »
Works like a charm, thank you so very much!  :good:

I'll use that for loops in the future, I'll beat SQF one day. :)

Kim

Offline Spooner

  • Members
  • *
  • Mostly useless
    • Community Base Addons
Re: Making a loop in SQF
« Reply #3 on: 27 Jun 2009, 22:28:32 »
Well, don't make the mistake that a lot of people do when moving from SQS to SQF, which is to use while for absolutely every loop. Get used to using for and forEach and you'll find your code a lot more readable than it could be just relying on while:
Code: [Select]
for "_i" from 1 to 10 do
{
   // Do this 10 times, where _i is set to 1, 2, 3, ..., 10
};


for "_i" from 0  to 10 step 2 do
{
  // _i is 0, 2, 4, 6, 8, 10.
};

// Kill everyone in evilNed's group.
{ _x setDamage 1 } forEach (units group evilNed);
[Arma 2] CBA: Community Base Addons
[Arma 1] SPON Core (including links to my other scripts)

Offline Kimmeh

  • Members
  • *
Re: Making a loop in SQF
« Reply #4 on: 27 Jun 2009, 23:39:21 »
Woah that one made me go cross-eyed. The _i and _x stuff is confusing the bleep out of me, and I've no idea what the random numbers are for.

Anyway, back to topic at hand, I found a problem. Once the Splash event occured, the script keeps giving you an arty strike every after 15 "sleep" seconds indefinately. Once the splash event occured I guess it doesn't do the Waitunil part anymore... any idea's?

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: Making a loop in SQF
« Reply #5 on: 28 Jun 2009, 02:12:39 »
I'll try to clear it up for ya.

Note I do not know how much you know,
so I will try and go straight from the ground up

Code: [Select]
while { true } do

- this means the script will loop as long as true is true (will happen no matter what). 
However, anything after this loop won't be executed, (because it's stuck in this loop.)

{
  waitUntil {BABUSHKA getVariable "ARTY_SPLASH"};

- waits for "arty_splash" to be true. Your problem is that it is still true when this loop occurs.
That gives you your arty strike after every 15 seconds.

sleep 15; // so it has time to actually say 'splash out' in response to the artillery operator.

This has it wait for 15 seconds. (I know, obvious. :P)

[["artillery_barrage"], FO, [[BABUSHKA, [1,2,3,4,5,6,7,8,9]]]] call BIS_SOM_addSupportRequestFunc;

This gives you the artillery strike.
To fix your 15 second problem you have to set the arty_splash variable to False, like so:

BABUSHKA setVariable ["Arty_Splash", false].
};

Dunno if that would allow you to get only one strike, though.


Below is Spooner's help for loops
Code: [Select]
for "_i" from 1 to 10 do
{

   Okay what happens here is this,
   The above sets the variable _i to 1.
   Then it runs the script in the curly braces. {}
   However, it executes the code inside the braces again adding 1 to the value of _i.
   So the second time through, _i is now 2.
   It continues this pattern until _i becomes 10.
   Hence the statement below.

   // Do this 10 times, where _i is set to 1, 2, 3, ..., 10
};


for "_i" from 0  to 10 step 2 do
{

   All right Kim, same principle, just tweaked a little.
   in this case _i starts from zero (0).
   and the step value configures how much to add, in this case two
   so _i is 0 the first run through, _i is 2 the second time, but _i is 4 the third time.
   In other words:
   Loop script with variable _i starting at value 0 and ending at value 10, adding 2 each time it loops.
   Hence, again, the statement below.

  // _i is 0, 2, 4, 6, 8, 10.
};

I like this for making 360 degree circles,
though it uses a slightly different loop:
Code: [Select]

_i=0;
while {_i < 360} do
   {
   code here;
   _i = _i + ((random 5)+1);
   };


The following concerns an array or group.
something in the format [element-1,element-2,element-3,...,element-n];
note that an array may have no values [], or only one value [value], or any number of values.
positions are arrays,
and the Foreach commands help a lot with arrays.

Code: [Select]
// Kill everyone in evilNed's group.


how the above works is as so-

say evilNed's group is three people as follows:

evilNed himself,
dirtyDan,
angryPaul.

the command "units group evilNed" would give us an array of

[evilNed, dirtyDan, angryPaul];

the command "{ _x setDamage 1 } forEach (units group evilNed);"
is a very condensed way of typing the following
evilNed setDamage 1;
dirtyDan setDamage 1;
angryPaul setDamage 1;

_x represents an element of the array carrying out the code "for each" element  in the array typed.
The script in the braces {} is looped for the number of elements substituting _x for that element.

In other words- "setDamage 1" for each of the elements in the array (the units in evilNed's group).


I know, It's long. :P

Hope that helped clear some stuff up about .sqf for you.

And DON'T FORGET to end each complete line with a semicolon;

Sqs let's you get away with that. Sqf doesn't.

Luke
Pesky Human!!
Wort Wort Wort.

Offline Kimmeh

  • Members
  • *
Re: Making a loop in SQF
« Reply #6 on: 28 Jun 2009, 13:52:15 »
Hey once you explained it like that I actually understood it, that's great stuff!

I'm gonna try out the thing with setting "splash" to false, see what that does. :) Oh and I'm gonna dig through that sqf tutorial again.

Thanks!

Edit: Just tried the splash-false setting, it worked like a charm, continuous arty use is flawless now thanks to you guys. I'd like to write a little tutorial thingy for newbs like me how to set up a continously available artillery in a mission, any idea to who I should send it, or is there a forum area for it?
« Last Edit: 28 Jun 2009, 14:06:56 by Kimmeh »

Offline Luke

  • Members
  • *
  • Thank God! The OFPEC Staff are here!
Re: Making a loop in SQF
« Reply #7 on: 28 Jun 2009, 23:44:46 »
There's a tutorial section in the editor's depot.

There's none for arma 2, but there are for arma itself.

Dunno how to submit it, though.  :dunno:

Try sending a PM to Mandoble as he's the depot admin.

Hope that helps!

Luke
Pesky Human!!
Wort Wort Wort.