Page 1 of 1
How to test if unit has magazine?
Posted: Tue Dec 24, 2019 5:19 am
by morphin
Hi
This code makes error on some scenarios:
local facilitiesToReportOn={}
local s = VP_GetSide({side='playerside'});
for k,v in pairs(s.units) do
local u = ScenEdit_GetUnit({guid=v.guid});
if(u~=nil and u.type ~= 'Group') then
print(#u.magazines)
end
local u=nil;
end
ERROR. Value must not be null
Parametername: collection
->i have found that the error is here
print(#u.magazines)
It seems that some units does not have magazines?
but this code produce the same error:
local facilitiesToReportOn={}
local s = VP_GetSide({side='playerside'});
for k,v in pairs(s.units) do
local u = ScenEdit_GetUnit({guid=v.guid});
if(u~=nil and u.type ~= 'Group') then
if(#u.magazines == nil) then
print(#u)
end
end
local u=nil;
end
Any hints?
RE: How to test if unit has magazine?
Posted: Tue Dec 24, 2019 9:05 pm
by Whicker
have you figured out which unit causes this? try printing the unit name at the same times as the # of magazines.
I just tried your first bit of code on a couple of Chains of War scens and there was no error.
RE: How to test if unit has magazine?
Posted: Tue Dec 24, 2019 10:02 pm
by michaelm75au
Try adding a check for the magazine object itself
if(u~=nil and u.type ~= 'Group' and u.magazines~=nil ) then ...
RE: How to test if unit has magazine?
Posted: Wed Dec 25, 2019 4:53 am
by morphin
Thank's
local facilitiesToReportOn={}
local s = VP_GetSide({side='playerside'});
for k,v in pairs(s.units) do
local u = ScenEdit_GetUnit({guid=v.guid});
if(u~=nil and u.type ~= 'Group' and u.magazines~=nil) then
print(#u)
end
local u=nil;
end
gives the same error:
ERROR: Der Wert darf nicht NULL sein.
Parametername: collection
I will not try to find out the unit
Thakn's
RE: How to test if unit has magazine?
Posted: Wed Dec 25, 2019 5:05 am
by morphin
it is a weapon:
unit {
type = 'Weapon',
subtype = '4003',
name = 'Generic Passive Directional Sonobuoy #11701',
side = 'China',
guid = '898e62a0-e3d3-4910-b355-7d818d0b62b5',
class = 'Generic Passive Directional Sonobuoy',
proficiency = 'Regular',
latitude = '18.5210674341366',
longitude = '113.437795492007',
altitude = '-40',
heading = '273.1217',
speed = '0',
throttle = 'FullStop',
autodetectable = 'False',
mounts = '0',
unitstate = 'Unassigned',
fuelstate = 'None',
weaponstate = 'None',
}
RE: How to test if unit has magazine?
Posted: Wed Dec 25, 2019 5:08 am
by morphin
So i made this workaround and it works[:D]
local facilitiesToReportOn={}
local s = VP_GetSide({side='playerside'});
for k,v in pairs(s.units) do
local u = ScenEdit_GetUnit({guid=v.guid});
if(u~=nil and u.type ~= 'Group' and u.type ~= 'Weapon') then
print(u.magazines)
end
local u=nil;
end
But actually i don't unterstand why the solution from Michael doesn't work with Weapon Type? Anybody can explain?
But thank's for the hints to find a soluation!
RE: How to test if unit has magazine?
Posted: Thu Dec 26, 2019 2:28 am
by michaelm75au
A weapon wouldn't have an object 'magazine', so you wouldn't be able to test it.
When in doubt, use the unit wrapper list to see what is available:
https://www.matrixgames.com/forums/tm.asp?m=4509745#