Home   Help Search Login Register  

Author Topic: simple .SQF loop script  (Read 1878 times)

0 Members and 1 Guest are viewing this topic.

Offline Delta

  • Members
  • *
  • Shazayme, Babye !
simple .SQF loop script
« on: 24 Nov 2007, 21:44:31 »
Hi,

   I'm ashamed. :confused:
I'm stuck on a simple .SQF loop script guys.

I need to add an action to the player whenever he has a weapon.
If he drops the weapon the action must then dissapear as well.
If he later picks up the weapon,then the option must reappear also. :scratch:

I have a single run version working,but as I say it needs to be running all the time the player is alive (thus convertion from .sqs to .sqf is needed)

Code: [Select]
while {alive player} DO
{
if (player hasweapon "M16") then
  {
   MNU = player addaction ["Attach Bayonette","bayonette.sqf"];
  }
  else
  {
   player removeaction MNU
  };
};

I get an } related error, and if I add the 'sleep' command I get one about that too.

I'm going crazy trying to get the desired result in .sqf format!

Can anyone help me out?
« Last Edit: 24 Nov 2007, 22:29:23 by hoz »

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: simple .SQF loop script
« Reply #1 on: 24 Nov 2007, 22:30:14 »
I switched your code sample too CODE. :P 
I also spaced out your routine so it was easier to read.


I think your missing a ;

Code: [Select]
   player removeaction MNU;
It really helps to craft your code so you can see errors like this. I miss a ; all the time. :D
« Last Edit: 24 Nov 2007, 22:33:19 by hoz »
Xbox Rocks

Offline Delta

  • Members
  • *
  • Shazayme, Babye !
Re: simple .SQF loop script
« Reply #2 on: 24 Nov 2007, 23:15:29 »
Thanks Hoz,

      I still seem to have the same problems unfortunately.

Code: [Select]
while {alive player} DO
{
if (player hasweapon "M16") then
  {
   MNU = player addaction ["Attach Bayonette","bayonette.sqf"];
  }
  else
  {
   player removeaction MNU;
  };
sleep 0.5;
};


Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: simple .SQF loop script
« Reply #3 on: 24 Nov 2007, 23:42:28 »
I don't think there's such a weapon as "M16" in ArmA, the proper name for the old M16A2 is, simply "M16A2". For the newer M16A4, it's "M16A4" :D

Otherwise, I don't really see anything wrong with it. :-/ Sure you're exec-vm'ing it and such properly, and that the addaction's script exists?

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Delta

  • Members
  • *
  • Shazayme, Babye !
Re: simple .SQF loop script
« Reply #4 on: 25 Nov 2007, 00:12:46 »
Hi,cheers wolfrug. :scratch:


 
Quote
Otherwise, I don't really see anything wrong with it. :-/ Sure you're exec-vm'ing it and such properly, and that the addaction's script exists?

Wolfrug out.

Its the exec vm  bit I messed up.

Sheesh! :confused:


Offline Goullou

  • Members
  • *
Re: simple .SQF loop script
« Reply #5 on: 25 Nov 2007, 19:31:11 »
Hi Ladies and Gentlemen,

Yes that's right "M16" is not an ArmA weapon but OFP.

You must check your execution command of the script. You can't launch a .sqf script like a .sqs by a simple exec.
Something like player exec myscript.sqf makes ArmA to execute it like a .sqs then all specific .sqf commands and syntax will return errors.
A good execution will be
script = [ ] execVM "myscript.sqf"
where arguments will be passed through the [] if needed.
or
whatever = [ ] execVM "myscript.sqf"
firstnameofyourgirlfriend = [ ] execVM "myscript.sqf"
   :-*


Otherwise try this:
Code: [Select]
private ["_compWeap","_loop","_MNU"];
//make list of bayonette compatible weapons
_compWeap = ["M16A2","M16A2GL","M16A4","M16A4_ACG","M16A4_ACG_GL","M16A4GL","M4","M4A1GL","M4A1","M4AIM","M4GL"];
//now the loop
for [{_loop=0},{_loop<1 or alive player},{_loop=_loop}] do
{
if ((primaryweapon player) in _compWeap and (vehicle player)==player) then
}
    _MNU = player addaction ["Attach Bayonette","bayonette.sqf"];
  }
  else
  {
player removeaction _MNU;
};
sleep 0.5;
};

I've changed the loop "while" by a loop "for" because loops "while" are limited to 10,000 rounds until the loop exit by itself. In your case with sleep 0.5 sec makes 5,000 sec (~ 1h30). I replaced MNU by _MNU because here's no reason to create a global variable.
I put all M16 and M4 weapon versions in the _compWeap array but i'm not sure if the versions having grenade launcher allow a bayonette down the muzzle in reality. Could be dangerous ...  :scratch:
PrimaryWeapon doesn't return the current active weapon (in hands) but what's loaded in 1st slot. Then if your soldier is not supposed to attach a bayonette on a handgun may be you should start your bayonette.sqf by:
player selectWeapon (primaryweapon player);

That's just couple of ideas ...  ;)
« Last Edit: 25 Nov 2007, 20:24:49 by Goullou »
Si vis pacem, para bellum.