How to test if unit has magazine?

All discussions & material related to Command's Lua interface

Moderators: michaelm75au, angster, RoryAndersonCDT, MOD_Command

Post Reply
morphin
Posts: 755
Joined: Fri Apr 26, 2002 6:51 pm
Location: Switzerland

How to test if unit has magazine?

Post 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?

Whicker
Posts: 665
Joined: Tue Jun 19, 2018 9:54 pm

RE: How to test if unit has magazine?

Post 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.
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: How to test if unit has magazine?

Post by michaelm75au »

Try adding a check for the magazine object itself
if(u~=nil and u.type ~= 'Group' and u.magazines~=nil ) then ...
Michael
morphin
Posts: 755
Joined: Fri Apr 26, 2002 6:51 pm
Location: Switzerland

RE: How to test if unit has magazine?

Post 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

morphin
Posts: 755
Joined: Fri Apr 26, 2002 6:51 pm
Location: Switzerland

RE: How to test if unit has magazine?

Post 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',
}
morphin
Posts: 755
Joined: Fri Apr 26, 2002 6:51 pm
Location: Switzerland

RE: How to test if unit has magazine?

Post 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!
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: How to test if unit has magazine?

Post 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#
Michael
Post Reply

Return to “Lua Legion”