Home   Help Search Login Register  

Author Topic: Stopping the infamous zero divisor error  (Read 2578 times)

0 Members and 1 Guest are viewing this topic.

Offline Tyger

  • Former Staff
  • ****
  • I was at OFPEC when it still had dirt floors...
    • OFPEC
Stopping the infamous zero divisor error
« on: 30 Jul 2007, 16:48:03 »
First off, this script is all General Barron's. I'm just editing it because it's an error in my mission, ultimately, will lower my mission score. So I have but one option. Fix it :D

It's a man MG script - for the exact details, just see the script below. The script works fine, and units will rearm, but occasionally, it will throw an error:

_gunner = _units select _closestUnit |#| Zero divisor

Now, if I assess that right, it's telling me that infact _units select _closestUnit does not exsist. Therefore, I inserted a check immediately before the variable _gunner is initalized with the unit in the array. See below, it's been commented.

The problem is, this still throws an error. Is there any other way that I can stop the zero error? Or ultimately, is there another miniscule error with the script?

Code: [Select]
;-----------------------------------------------------------------------------------------------------------------

-----------------
;manMG2.sqs
;By General Barron
;aw_barron@hotmail.com
;3/29/03

;Edited by Tyger to provide two extra checks agains the "Zero Divisor" error.
;I believe they were cause when the script just entered a loop when the unit it
;was going to give instructions to was killed.

;This script is used to make units man a machine gun when its gunner gets killed.
;The closest unit to the machine gun will go to man it, as long as they are within the
;reach of the trigger. If no units are currently in the area, the next unit to enter the range
; of the trigger will go to man the MG.  If you wish, you can make it so only units of a certain
; side will be affected by this script.

;  It is called as follows:
;[mg,trigger,side(optional)] exec "manMG2.sqs"

;"mg" is the name of the machine gun you want manned.
;"trigger" is the name of a trigger. It must be set to "activation ANYBODY".
;Only units within the area of this trigger will be affected by this script.
;(Make sure it is activated by "anybody", NOT "west", "east", etc. It will ONLY work with "anybody"!!!)

;"side" is optional. Placing WEST (no quotes!) here will allow only west units to be affected
;by this script. Other options are EAST and RESISTANCE. You cannot specify the side "civilian".
;If you leave this blank, any unit, regardless of side will be affected by this script.

;You can end all manMG scripts that are running by setting the variable manMG_end to TRUE
;-----------------------------------------------------------------------------------------------------------------

-----------------

manMG_end = FALSE
;set this variable to TRUE to end the script
_gun = _this select 0
;name of the machine gun to be manned
_trigger = _this select 1
;name of trigger covering the area
_side = _this select 2
;side you want to man the gun with (optional). If nothing is entered, any side can man the gun

?_side == west OR _side == east OR _side == resistance : goto "Top"
_side = civilian
;this makes civilian the default _side (if nothing is entered when calling the script)

  #Top
@NOT alive gunner _gun OR NOT alive _gun OR manMG_end
?NOT alive _gun OR manMG_end : exit
@"vehicle _x == _x" count list _trigger > 0 OR  NOT alive _gun
?NOT alive _gun OR manMG_end : exit
;wait until the gunner is dead, and there are people around who aren't in vehicles

_units = list _trigger
_i = -1
_max = count _units
_minDist = 99999
;set up variables for loop

  #Loop
_i = _i + 1
?_max <= _i : goto "Bottom"
;exit loop when end of list is reached
;Inserted by Tyger as a check for the infamous "Zero Divisor"
if ((_units select _i) != nil) then {_man = _units select _i} else {goto "Top"}
;_man = _units select _i
_dist = _man distance _gun
;find distance between selected man and gun
?_dist >= _minDist OR vehicle _man != _man OR _man == player OR vehicle _man == _gun OR (_side != civilian AND side

_man != _side) : goto "Loop"
;move on to the next man if they are further away from the gun than the closest person, or if they are in a

vehicle, or if it is the player, or if they are on the wrong side (unless _side = civilian)
_closestUnit = _i
_minDist = _dist
;if they pass, then they are the current closest man to the mg, and their element # and distance is saved
goto "Loop"

  #Bottom
;Inserted by Tyger as a check for the infamous "Zero Divisor"
if ((_units select _closestUnit) != nil) then {_gunner = _units select _closestUnit} else {goto "Top"}
;_gunner = _units select _closestUnit
_gunner assignAsGunner _gun
[_gunner] orderGetIn TRUE
;once the loop is finished, the closest man is selected, assigned to the gun, and ordered in
goto "Top"
"People sleep soundly at night only because rough men stand ready to do violence on their behalf." - George Orwell

MSG Mike Everret - We Will Never Forget - '75-'08

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: Stopping the infamous zero divisor error
« Reply #1 on: 31 Jul 2007, 01:14:03 »
Replace the != nil with the old standard check for uninitialised variables:
Code: [Select]
if (format["%1", _units] != "scalar bool array string 0xfcffffef") then {...}
You can not compare something to nil, which is why ArmA gave us the isNil command.


Edit: interesting scripted waypoint alternative here although it will only reman the gun with units from the same group.
« Last Edit: 31 Jul 2007, 01:20:51 by Mr.Peanut »
urp!

Offline schuler

  • Contributing Member
  • **
Re: Stopping the infamous zero divisor error
« Reply #2 on: 31 Jul 2007, 12:05:49 »
this might help too,,, when i shot the M2 gunner, i got a camand to "target m2" but no one was at the M2, i shot it anyways as men where around it,,,, could the M2 be dammaged and therefor not be able to be mounted? or did i shoot the man that was to mount it? and i think the error comes up about 10ses later.
Semper Fi

Offline Tyger

  • Former Staff
  • ****
  • I was at OFPEC when it still had dirt floors...
    • OFPEC
Re: Stopping the infamous zero divisor error
« Reply #3 on: 31 Jul 2007, 16:03:01 »
@MrP

Thanks mate! I was just making a shot in the dark with that one. It didn't return an error which was throwing me off but I suppose the units did not equal the gunner so everything checked out...

@Schuler

No idea mate :P
"People sleep soundly at night only because rough men stand ready to do violence on their behalf." - George Orwell

MSG Mike Everret - We Will Never Forget - '75-'08