Home   Help Search Login Register  

Author Topic: [SOLVED]Checking Ammo with 2 mags?  (Read 2500 times)

0 Members and 1 Guest are viewing this topic.

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
[SOLVED]Checking Ammo with 2 mags?
« on: 16 Feb 2012, 06:13:54 »
I recently found the command for ammo, to see how many rounds are left in the magazine, and tested it in a mission. This is a long post, and my question is at the bottom.

4 scripts, and 3 units.

Units: East soldier (eastm), 2 west soldiers (cap1, cap2)
eastm being player, and cap1, and cap2 being targets.

Scripts (Dont ask why my script names are what they are, I have no idea, they're totally random in my head):
init.sqs
hower.sqs
checker.sqs
checker2.sqs

init.sqs
Code: [Select]
eastm_wep = 1

eastm exec "checker.sqs"
eastm exec "checker2.sqs"
eastm exec "hower.sqs"

removeallweapons eastm
eastm addmagazine "ak47"
eastm addweapon "ak47"
eastm addmagazine "ak47"

eastm dowatch cap1
eastm dofire cap1

hower.sqs
Code: [Select]
#return
?(eastm ammo "ak47" < 1) : goto "move"
goto "return"
#move
eastm_wep = eastm_wep - 1

checker.sqs
Code: [Select]
#return
?(alive cap1) : goto "return"
eastm dowatch cap2
eastm dofire cap2

exit

checker2.sqs
Code: [Select]
#return
?(eastm_wep < 1) : goto "move"
goto "return"
#move
removeallweapons eastm
eastm setpos getpos outof
_msg=format["%1.... you are out of ammo. Bye...... Weapon Status: %2", name eastm, eastm_wep]
?(eastm == player) : hint _msg
~5
eastm setdammage 1.0
exit

Now basically when they eastm runs out of ammo in his first magazine, checker2.sqs carries through, to kill eastm. If there was only one magazine, this would work perfectly. However, there are two magazines. Is there a way to check how many magazines he has? or something to make it so that he doesn't die until he's actually out of ammo, and not just through his first magazine?

Thanks!

-Mad Pup  :D
« Last Edit: 11 Mar 2012, 04:30:58 by Mad Pup »
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re: Checking Ammo with 2 mags?
« Reply #1 on: 24 Feb 2012, 00:09:15 »
Mad Pup,

You can simplify the whole thing to two scripts (init.sqs and checker.sqs). Also, if checker.sqs is more general it will work with any unit and any two targets and any weapon/ammo type you give him. Here I use the magazines command to determine when he is out of magazines. Also, note that I added a short delay in the loop. It is good practice to put some kind of delay in repeating loops to help reduce lag. For simple missions with just one or two scripts running it is not much of an issue, but if you make more complex missions where many scripts might be running at the same time, loops with no delay can cause lag.

init.sqs:

Code: [Select]
removeallweapons eastm
eastm addmagazine "ak47"
eastm addmagazine "ak47"
eastm addweapon "ak47"

[eastm, cap1, cap2] exec "checker.sqs"
;this will execute the script checker.sqs sending eastm, cap1 and cap2 as parameters to that script

Checker.sqs:

Code: [Select]
_man = _this select 0
_targ1 = _this select 1
_targ2 = _this select 2
;these above lines draw out the parameters sent to the script and sets local variables representing each parameter

_targ = _targ1
;this line sets the local variable _targ to be equal to _targ1

#resetTarg
;this is a label, labels can be used in scripts with the goto command to make the script jump to new locations or repeat loops

_man doWatch _targ
_man doFire _targ
;the _man (in this case eastm) will watch the _targ and fire at the _targ (the _targ starts as cap1 but changes later to cap2)

#loop

? ! alive _targ && _targ == _targ1 : _targ = _targ2; goto "resetTarg"
;this conditional will make the script jump to reset _targ to become _targ2 (cap2) when _targ1 (cap1) is dead

~ .01
? count magazines _man > 0 : goto "loop"
;this loop repeats until the man is out of ammo

? _man == player : hint format ["%1.... you are out of ammo. Bye......", name _man]
;send hint when human player is out of ammo

exit

The reason one makes a script with local variable is so the script is more general. If you only used global variables, then you would have to make a new script for every group of different units running the script. Using a local variable script, one script can work for many units with different names, even several units can run the same script at the same time and the multiple cases of the script running will not have any effect on each other, because the local variable stay local to the script. So, this one script can check the ammo status of eastm, eastm1, eastm2, eastm3 etc. And each of these units can be shooting at cap1, cap2, cap3, cap4, etc.

As you play around with scripting you will learn ways to make them even more general. You can even send parameters to scripts that are unique commands that the script can execute using the call command. Using arrays in scripts allow even greater flexibility. Keep trying new things and examine other scripts to see what others have done (download stuff from the editors depot and examine the scripts that are used in those resources. You can learn from those and sometimes even figure out better ways of scripting things than the original author.


« Last Edit: 29 Feb 2012, 19:12:28 by Raptorsaurus »

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
Re: Checking Ammo with 2 mags?
« Reply #2 on: 25 Feb 2012, 15:56:07 »
Quote
Code: [Select]
_man = _this select 0
_targ1 = _this select 1
_targ2 = _this select 2

_targ = _targ1

Thanks for the response! But....
What exactly does this do? I'm a basic scriptor, and don't know much.

-Mad Pup
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad

Re: Checking Ammo with 2 mags?
« Reply #3 on: 25 Feb 2012, 16:42:53 »
Code: [Select]
_man = _this select 0
_targ1 = _this select 1
_targ2 = _this select 2

are referred to

Code: [Select]
[eastm, cap1, cap2] exec "checker.sqs
so _man refers to first element of the [array] which is easttm;
_targ1 refers to second element of the [array] which is cap1;
_targ2 refers to third element of the [array] which is cap2;

and

Code: [Select]
_targ = _targ1
just "rename" _targ1 variable in _targ
? (this == thinkable) : this = scriptable

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
Re: Checking Ammo with 2 mags?
« Reply #4 on: 25 Feb 2012, 22:52:45 »
So when you say
Quote
Code: [Select]
_man = _this select 0
that selects eastm?

it's the
Code: [Select]
_this select 0
_this select 1
_this select 2
part that confuses me. I guess I'm not sure what it does.
Sorry for being complicated.

-Mad Pup
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad

Offline Gruntage

  • Missions Depot
  • Administrator
  • *****
  • How do I get outta this chickensh*t outfit?
Re: Checking Ammo with 2 mags?
« Reply #5 on: 25 Feb 2012, 23:38:19 »
Code: [Select]
_man = _this select 0
_veh = _this select 1
_logic = _this select 2

Basically what these commands do is select global variables from within the mission itself. A script that uses these parameters is called by:

Code: [Select]
[firstvar, secondvar, thirdvar] exec "script.sqs"
You would replace 'firstvar' etc with names of units or any other global variable.

The 'firstvar' is 'select 0', the 'secondvar' is 'select 1' etc. You can use local variables as well, like:

Code: [Select]
[_var1, _var2, _var3]  exec "script.sqs"
But these variables have to come from within a script rather than within the mission itself.

In the case of your script, '_man = _this select 0' would select the unit you want the script applied to, as it is the first element as Doktor says.

Hope this helps

Gruntage
« Last Edit: 25 Feb 2012, 23:39:56 by Gruntage »
"But one thing I can tell you from not just OFP but life in general:  criticism is directly proportional to quality. The more criticism a mission receives, the better the outcome" - macguba

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
Re: Checking Ammo with 2 mags?
« Reply #6 on: 26 Feb 2012, 00:02:26 »
This makes more sense. Thanks Gruntage.  :good:

-Mad Pup
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad

Re: Checking Ammo with 2 mags?
« Reply #7 on: 26 Feb 2012, 09:56:38 »
Yes, Gruntage explained it better than me! Sorry for my rushed reply! :D
? (this == thinkable) : this = scriptable

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re: Checking Ammo with 2 mags?
« Reply #8 on: 26 Feb 2012, 20:28:46 »
Mad Pup,

Although Doktor Headshot and Gruntage gave a good explaination I will elaborate further.

OFP lets you pass parameters to scripts using the _this command. If you want to send the parameters to the script you put them in an array when you call the script.

Code: [Select]
[eastm, cap1, cap2] exec "checker.sqs

In this case I'm sending your global variables eastm, cap1 and cap2 to the script "checker.sqs"

When the script runs, the array of variables [eastm, cap1, cap2] are stored in a local command array variable called "_this". In OFP scripting, variables with an underline in front are local variable (meaning only the script "knows" what is stored in them, other scripts and the general game engine have no connection to the values of local variables. There are some local variables that are built in to the OFP scripting that act like commands. These include: _this and _time. In the script, the _this command always contains the array of parameters that were sent to the script. To separate out each element from the array one uses the "select" command. "_this select 0" is the first element (OFP indexes arrays starting at zero), "_this select 1" is the second element, "_this select 2" is the third element, etc.

I will make a commented version of the script and send that a little later that explains each command line.
« Last Edit: 26 Feb 2012, 20:31:23 by Raptorsaurus »

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
Re: Checking Ammo with 2 mags?
« Reply #9 on: 27 Feb 2012, 21:55:25 »
Alright Raptorsaurus. Thanks!

-Mad Pup
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad

Offline Raptorsaurus

  • Editors Depot Staff
  • *****
Re: Checking Ammo with 2 mags?
« Reply #10 on: 29 Feb 2012, 19:14:03 »
I added comments to my first post, go ahead and check those.

Offline Mad Pup

  • One Who Asks A Lot.
  • Members
  • *
  • OFPEC Is Awesome
Re: Checking Ammo with 2 mags?
« Reply #11 on: 01 Mar 2012, 01:08:49 »
I think I'm going to let people test this mission. I think it's a pretty good idea. Theres an addon that I found for it. Not sure how popular it is.

-Mad Pup
"The object of war is not to die for your country, but to make the other poor bastard die for his" - General Patton
[SH]MadPup
[SH]Squad