OFPEC Forum

Editors Depot - Mission Editing and Scripting => ArmA - Editing/Scripting General => Topic started by: Rellikki on 25 Jul 2007, 23:44:52

Title: SOLVED: Check for free magazine slots
Post by: Rellikki on 25 Jul 2007, 23:44:52
Is there a way to check if the player has any free magazine slots? :dunno:
Title: Re: Check for free magazine slots
Post by: LCD on 25 Jul 2007, 23:53:29
u cud use da magazines (http://www.ofpec.com/COMREF/index.php?action=list&game=All&letter=m#magazines) command 2 check how meny magazines da player got... now as most units have da same num of magz u cud calculate it from here...

LCD OUT
Title: Re: Check for free magazine slots
Post by: Rellikki on 26 Jul 2007, 00:42:04
Thanks, I made this script using that command:

Quote
_mags = count magazines player
?_mags <= 10 : goto "true"
goto "end"

#true
Hint "Player has 10 or less magazines"
Exit

#end
exit

However it gets complicated because I'm supposed to check if the player has two free magazine slots for one satchel charge, and that script also counts the pistol magazines with it, and eg. MG magazine takes two slots but it's still counted as one. :confused: If someone has any ideas, I'd be really thankful.
Title: Re: Check for free magazine slots
Post by: Tyger on 26 Jul 2007, 00:52:55
This could be a long script but basically I know the gist of it.

magazines myUnit can also return an array of the magazines a unit has. Therefore, you could compare each item in the array to see what type of magazines they are, and based off of that, subtract points from a counter.

For instance, first check weather the unit has a pistol. If the unit has a pistol, set the counter to 14, else set it to 10. Then see if any of the magazines the player has are LAW rockets, MG magazines etc. If they are any of the magazines that normally take up 2 slots, then remove two slots from your counter. If you have ended up with 2 slots left (i.e. your counter is 2) you have room.

Does that make sense? Hope so...
Title: Re: Check for free magazine slots
Post by: Tyger on 26 Jul 2007, 01:45:45
Something similar to this. Haven't time to check it but that should work:
Code: [Select]
;magazinesTest.sqs

;Execute this script with
;unitName exec "magazinesTest.sqs"

_unit = _this

;All magazines that take up a single slot
_singleSlotArray = ["20Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_StanagSD","30Rnd_556x45_G36","30Rnd_9x19_MP5SD","30Rnd_9x19_MP5","5Rnd_762x51_M24","10Rnd_127x99_m107","30Rnd_545x39_AK","30Rnd_545x39_AKSD","10Rnd_762x54_SVD","5Rnd_127x108_KSVK","SmokeShell","SmokeShellRed","SmokeShellGreen","HandGrenade","HandGrenadeTimed","FlareWhite_M203","FlareGreen_M203","FlareRed_M203","FlareYellow_M203","1Rnd_HE_M203","FlareWhite_GP25","FlareGreen_GP25","FlareRed_GP25","FlareYellow_GP25","1Rnd_HE_GP25"]

;All magazines that take up double slots
_doubleSlotArray = ["200Rnd_556x45_M249","20Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_StanagSD","100Rnd_762x51_M240","M136","JAVELIN","STINGER","100Rnd_762x54_PK","PG7V","PG7VR","STRELA","PipeBomb","TimeBomb","Mine","MineE"]

;Count the number of magazines
_numMags = count magazines _unit

;Magazine array
_magArray = magazines _unit

;If the unit has a pistol, check for pistol mags. Otherwise, they can't have pistol mags.
if ((_unit hasWeapon "M9") || (_unit hasWeapon "M9SD") || (_unit hasWeapon "Makarov") || (_unit hasWeapon "MakarovSD")) then {_magazineCounter = 14} else {_magazineCounter = 10}

_i = 0
#magsLoop
if ((_i - 1) > _numMags) then {goto "ExecuteAction"}
_thisMag = _magArray select _i
if (_thisMag in _singleSlotArray) then {_magazineCounter = _magazineCounter - 1}
if (_thisMag in _doubleSlotArray) then {_magazineCounter = _magazineCounter - 2}
_i = _i + 1
goto "magsLoop"

#ExecuteAction
;Your code to do whatever goes here
if (_magazineCounter > 1) then {_unit addMagazine "Pipebomb"}

exit
Title: Re: Check for free magazine slots
Post by: Rellikki on 26 Jul 2007, 03:22:24
Thanks for the time you put in that script, but it gives me an error though...

Quote
Error hasweapon: Type Array, expected Object
Title: Re: Check for free magazine slots
Post by: h- on 26 Jul 2007, 08:33:45
How did you execute the script?
With unitname exec "blah" or [unitname] exec "blah"?
Should be executed with the first option, otherwise the variable _unit is an array and will not work with the hasWeapon command..

@Tyger
Please do not double post.
If you have something to add to your post after a short period of time modify it instead of replying to yourself :cop:
Title: Re: Check for free magazine slots
Post by: Rellikki on 26 Jul 2007, 12:08:49
Oh, oops. I thought I did execute it that way. Must have been too tired or something... :-[
I get another error though:

Quote
_thisMag = _magArray select _i
Error Zero Divisor
Title: Re: Check for free magazine slots
Post by: Tyger on 26 Jul 2007, 19:28:21
@ h- sorry mate. Shoulda known better... :P

I get that still with General Barron's Man MG script. I dunno what that means, because _magArray select 0 should be the first element of the array...

Did the unit have magazines? :D Just kidding mate... I'd need someone to translate what Zero Divisor means before I could fix it.

EDIT
Okay, i did a search on Zero Divisor and I might have a fix for it. Apparently, it might be that the unit is not completely initialized when the script is run, therefore the magazines array is null at the time. So, try executing the script from a trigger in the mission. That might work. I'll keep on looking mate.

EDIT II
Also, below
Code: [Select]
;Magazine array
_magArray = magazines _unit
add the following:
Code: [Select]
if (_magArray == nil) then
{
    hint "Array is empty"
    exit
}
I think that should tell you weather or not the array is empty.
Title: Re: Check for free magazine slots
Post by: Rellikki on 26 Jul 2007, 23:35:07
Quote
Okay, i did a search on Zero Divisor and I might have a fix for it. Apparently, it might be that the unit is not completely initialized when the script is run, therefore the magazines array is null at the time. So, try executing the script from a trigger in the mission. That might work. I'll keep on looking mate.
Already had it that way.

Quote
Also, below
Code: [Select]
;Magazine array
_magArray = magazines _unit
add the following:
Code: [Select]
if (_magArray == nil) then
{
    hint "Array is empty"
    exit
}
I think that should tell you weather or not the array is empty.
It does give me the hint, but now I get another error. :blink:

Quote
'if (magArray |#|== nil) then '
entry, Display (dialog), Control
Error ==: Type Array, expected Number, String, Object, Side, Group, Text, Config
This seems to be very complicated..
Title: Re: Check for free magazine slots
Post by: Tyger on 27 Jul 2007, 01:49:50
Hmm. That means that an array cannot be equal to type nil. Needless to say I would remove the check from the script.


EDIT
Well, i've been playing around with the damn thing and apparently it's something to do with the _i and the magazines array.

I tested the script using hints and the _magArray variable WAS set.
Also, _magArray select 0 does have a value when using hints to confirm.
Also, with a standard soldier
Code: [Select]
if ((magazines player select 0) in [20Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_StanagSD"]) then {hint "Amen."} works. Therefore it's not the in syntax.

I'm bugger out of ideas. Other than maybe testing every single bleeding magazine spot individually.


EDIT II

I think I've solved it.
Code: [Select]
; This file is open source. Please give credit where due.
; Script file for Armed Assault
; Created by: Tyger

;magazinesTest.sqs

;Execute this script with
;unitName exec "magazinesTest.sqs"

_unit = _this

;All magazines that take up a single slot
_singleSlotArray = ["20Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_StanagSD","30Rnd_556x45_G36","30Rnd_9x19_MP5SD","30Rnd_9x19_MP5","5Rnd_762x51_M24","10Rnd_127x99_m107","30Rnd_545x39_AK","30Rnd_545x39_AKSD","10Rnd_762x54_SVD","5Rnd_127x108_KSVK","SmokeShell","SmokeShellRed","SmokeShellGreen","HandGrenade","HandGrenadeTimed","FlareWhite_M203","FlareGreen_M203","FlareRed_M203","FlareYellow_M203","1Rnd_HE_M203","FlareWhite_GP25","FlareGreen_GP25","FlareRed_GP25","FlareYellow_GP25","1Rnd_HE_GP25"]
;_singleSlotArray = ["M16","M4","AK47","AK74","BizonMag","SteyrMag","FALMag","Flare","FlareGreen","FlareRed","FlareYellow","HKG3Mag","HuntingRifleMag","HK","KozliceBall","KozliceShell","GlockMag","GlockSMag","CZ75Mag","IngramMag","M21","RevolverMag","SkorpionMag","SVDDragunov","TokarevMag","UZIMag","BerettaMag","HandGrenade"]

;All magazines that take up double slots
_doubleSlotArray = ["200Rnd_556x45_M249","20Rnd_556x45_Stanag","30Rnd_556x45_Stanag","30Rnd_556x45_StanagSD","100Rnd_762x51_M240","M136","JAVELIN","STINGER","100Rnd_762x54_PK","PG7V","PG7VR","STRELA","PipeBomb","TimeBomb","Mine","MineE"]
;_doubleSlotArray = ["GrenadeLauncher","LAWLauncher","RPGLauncher","Mortar","PK","M60"]

;All magazines that take up five slots
;_largeSlotArray = []
;_largeSlotArray = ["MM1Magazine","6G30Magazine","9K32Launcher","AALauncher","AT4Launcher","CarlGustavLauncher","LaserDesignator"]

;Count the number of magazines
_numMags = count (magazines _unit)

;Magazine array
_magArray = magazines _unit

;Default number of magazines
_magazineCounter = 10

;If the unit has a pistol, check for pistol mags. Otherwise, they can't have pistol mags.
;Insert ArmA here
;if ((_unit hasWeapon "Revolver") || (_unit hasWeapon "Skorpion") || (_unit hasWeapon "Tokarev") || (_unit hasWeapon "Ingram") || (_unit hasWeapon "GlockS") || (_unit hasWeapon "Glock") || (_unit hasWeapon "CZ75") || (_unit hasWeapon "Beretta")) then {_magazineCounter = 14}
if ((_unit hasWeapon "M9") || (_unit hasWeapon "M9SD") || (_unit hasWeapon "Makarov") || (_unit hasWeapon "MakarovSD")) then {_magazineCounter = 14}

;magsLoop counter
_counter = 0

#magsLoop
if (_magArray select _counter == nil) then {goto "ExecuteAction"}
_thisMag = _magArray select _counter
if (_thisMag in _singleSlotArray) then {_magazineCounter = _magazineCounter - 1}
if (_thisMag in _doubleSlotArray) then {_magazineCounter = _magazineCounter - 2}
;if (_thisMag in _largeSlotArray) then {_magazineCounter = _magazineCounter - 6}
_counter = _counter + 1
if (_counter == _numMags) then {goto "ExecuteAction"}
goto "magsLoop"

#ExecuteAction
;Your code to do whatever goes here
hintC format["You have %1 magazine slots open.",_magazineCounter]

exit

I'm rather confused at the moment why we were getting an error with the other script. As far as I'm aware, the script should have ended early because of my accidental subtraction of one when comparing the number to the maximum value. But anyway, I added a test to see if the magazine select was equal to nil before you set _thisMag equal to it. That fixed the problem by moving on if the mag was nil.

That should work. And please pardon that I decided to also convert it into use for OFP :).

Funny how sometimes a simple check is the error that causes the whole thing to be problematic.
Title: Re: Check for free magazine slots
Post by: Rellikki on 27 Jul 2007, 11:08:41
Works perfectly, thanks a lot. I had to change some numbers though, because in ArmA there are 12 rifle magazine slots + 8 pistol magazine slots. Also,  after some tests, I found out that the pistol magazines aren't counted to the _magazineCounter so it doesn't need to be increased on that line where it checks if the unit has a pistol. :)