Home   Help Search Login Register  

Author Topic: Rearming HMG  (Read 1956 times)

0 Members and 1 Guest are viewing this topic.

Offline SaS TrooP

  • Members
  • *
  • n00b always & everywhere
Rearming HMG
« on: 24 Dec 2009, 12:48:31 »
OK, another problem. Can someone explain me how exactly while loop works?
Here's the loop:

Code: [Select]
while {alive deszeka1} do
{
if (someammo deszeka1) then
{
hint "test";
}
else
{
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
);
);

The game should rearm DShK always when its out of ammo. How to modify that script and start it so it works?
Notice that there will be more scripts like that (10 or more HMGs to rearm), so script shouldnt be resource-eating.

Offline savedbygrace

  • Intel Depot
  • Administrator
  • *****
  • Be swift to hear...slow to speak...slow to wrath.
Re: Rearming HMG
« Reply #1 on: 25 Dec 2009, 07:28:34 »
I hope I am not overstepping my bounds here as I am most certainly no SQF expert but your final two closing brackets were parenthesis and not the needed curly braces. I haven't tested it though.
You may be able to use several of these while loops in the same script and call the script in the mission init, trigger OnActivation or from another script by using something like this....
call {[stuff] execVM "script.sqf"}

Code: [Select]
while {alive deszeka1} do
{
if not (someammo deszeka1) then
{
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
};

};
« Last Edit: 25 Dec 2009, 07:54:58 by savedbygrace »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Rearming HMG
« Reply #2 on: 25 Dec 2009, 09:28:19 »
One thing to remember is to add a pause in the loop, otherwise you may bring the game to halt (although Arma2 seems to handle this a bit better than the previous games)..
Also, SBG's example has slight syntax error
Code: [Select]
while {alive deszeka1} do
{
if (!(someammo deszeka1)) then
{
deszeka1 addmagazine "50Rnd_127x107_DSHKM";
};
  sleep 1;
};
sleep 1 makes the loop pause for one second on each iteration (and ! means not)..


If you want it to be more "dynamic", as in that you don't have to manually edit the magazine name and have several versions of the same script (if you want to use this for other types of vehicles too) you can try something like the code below. It will reload the vehicle (with single weapon type) to it's original full ammo once it's out of ammo, no need to do anything else than execute the script from the unit's init field.
Code: [Select]
private["_magType","_magCount"];

// get the type of magazine the vehicle uses
_magType = (magazines _this) select 0;

//count the amount of magazines
_magCount = (count (magazines _this))-1;

//loop as long as the vehicle is alive
while {alive _this}
 do {
//check if vehicle doesn't have ammo...
if (!(someAmmo _this)) then {
//.. and if so add as many magazines as the vehicle originally had
for [{_i=0},{_i<=_magCount},{_i=_i+1}] do {
_this addMagazine _magType;
};
};
//pause for a sec
sleep 1;
    };
Name the script to something, for example rearm.sqf and execute it from the desired unit's init with the following; void = this execVM "rearm.sqf"
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline SaS TrooP

  • Members
  • *
  • n00b always & everywhere
Re: Rearming HMG
« Reply #3 on: 25 Dec 2009, 13:24:09 »
Thank to both of you. I decided to choose h-'s option because its less resource eating. Of course both versions are woking property BUt I have 2 questions:

1. That's a pain of all humanist-type brain. YOu understand all in the script, but you wouldn't write it alone :P
BUt there's one line which I cant understand:
Code: [Select]
//count the amount of magazines
_magCount = (count (magazines _this))-1;

Game counts the magazines of tthe object who executes the script. OK. But why there is -1 in the end? What's the usage of it?

2. I asked how to execute that script, because I cant understand the difference between execVM and preprocessFile. Can you explain me this?
« Last Edit: 25 Dec 2009, 14:03:10 by SaS TrooP »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Rearming HMG
« Reply #4 on: 25 Dec 2009, 18:56:04 »
Quote
But why there is -1 in the end?
It's kinda a habit of mine.
I usually use the count result with selecting elements from an array and array element indexes beging from 0 and the count begins from 1 so the count will then always go one further than the array element indexes, and will then cause error to pop up.

If you change the _i=0 to _i=1 you wouldn't need that -1..

Quote
because I cant understand the difference between execVM and preprocessFile
execVM is used to execute sqf script, preprocessFile is used to preprocess a function.
Function is still the same as it was in OFP:R, sqf script is basically new version of the old sqs; it allows the same formatting as in the functions and is more efficient.
For some reason execVM requires a named scripthandle when used in the editor (triggers, waypoints, init fields, etc), in that example of mine I use the name void (void = this execVM "rearm.sqf")..

Check out this tutorial about the basic sqf scripting, much better than me trying to explain it :P .
It's for Arma 1 but all of it is still relevant in Arma 2.
Only one thing has changed and that is that first you have to declare (give some value to) all variables you will be using in the script.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline SaS TrooP

  • Members
  • *
  • n00b always & everywhere
Re: Rearming HMG
« Reply #5 on: 25 Dec 2009, 20:04:18 »
OK, now I understand. But one more question: Maybe it's a bit too much for me. but I make RPG-style mission. Well, actually I have quite much done (like HMGs that will never be out of ammo, so human bastions can be defended for ever :P)
You can buy weapons. But its good to sell it from time to time. Best idea is to set weapon prices in one script and then take a percent while selling it. That was possible for me with SPON Money, that are currently unavailable for ArmA 2. SO I am using good old OFP script in .sqs to operate it.

Returning to the heart, I want to add possibility to sell your CURRENT main weapon. All weapons (no matter if AKS-47 or KSVK) have to be sold for 300 money. But this is connected with global variable in .sqs scripts, so that (at least for me) have to be in .sqs syntax, too.
I tried to rework your rearm scipt where DShK's magazines are being set without naming it:
Code: [Select]
_magType = (magazines _this) select 0;That seems not to be working.
How do I set a boolean called _gun, where _gun is a weapon that is being currently held by unit called "ja" (I would rather avoid using "player" in this stage), and then this _gun is being removed? I can handle with the rest.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Rearming HMG
« Reply #6 on: 26 Dec 2009, 07:22:52 »
Try the commands currentWeapon and removeWeapon.

Although I don't really get what you mean with "boolean called _gun", boolean means a variable with true/false value..
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline SaS TrooP

  • Members
  • *
  • n00b always & everywhere
Re: Rearming HMG
« Reply #7 on: 26 Dec 2009, 15:08:41 »
Hmmm, really good point!
This how it looks:

Code: [Select]
playercounter=playercounter
_gun = currentweapon ja;
~1
ja removeweapon _gun
playercounter=playercounter+300
hint format ["You ssold your current weapon and now have %1 money", playercounter]

Exit

And that works. But here's small problem. You are selling weapon even if you dont have a weapon -.-
How to make a - how to call it - "Negative" version. I want to have it some how like this:

Code: [Select]
playercounter=playercounter

; And here I need an condition that go to "noweapon" when player has now weapon.

_gun = currentweapon ja;
~1
ja removeweapon _gun
playercounter=playercounter+300
hint format ["You ssold your current weapon and now have %1 money", playercounter]

Exit

#noweapon
hint "You have no weapon to sell!";
Exit

How to construct the command that will go to #noweapon if unit called "ja" has NO weapon in the hands (the current weapon)?
So, lets say, player is out of weapons completely and he want to sell a gun anyway. I need a command that will prevent it and will ski the script to #noweapon. How to to this? That have to be .sqs syntax (I think)

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Rearming HMG
« Reply #8 on: 27 Dec 2009, 10:36:58 »
Quote
That have to be .sqs syntax (I think)
Nothing has to be in sqs format.

How/where is that script executed from?
Is that an action, or what?
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline SaS TrooP

  • Members
  • *
  • n00b always & everywhere
Re: Rearming HMG
« Reply #9 on: 27 Dec 2009, 13:51:39 »
Well, I have actually decided to cut it off. But, for you:
Its being executed from .sqs script as an action:

Code: [Select]
action300=trader1 addaction ["Sell your current main weapon (300)","WeaponSell.sqs"];
And now "WeaponSell.sqs" is this script in my post above. But, as I said, you can sell weapon even if you dont have it -.-
And I don't know how to fix it (check if player has a weapon). In SPON Money you can add value of the weapon (its prize) in a label, and then script executes, let's say a percent from weapon's original price. But SPON Money are not available for ArmA 2 and I al using quite old script. And this old script works property, so I am not going to changed it.