Page 1 of 1
Testing to See if Loadout is Set
Posted: Wed Sep 18, 2019 12:56 pm
by SeaQueen
In order to make an improved version of my aircraft maintenance script, I'm experimenting with using the function
to test whether or not the loadout has been set or not. This should hopefully allow me to retain loadouts between save games without the risk of resetting unintentionally. I've run into a little problem, though. Instead of returning nil, 0, 99999 or some other useful value when the aircraft's loadout isn't set, I get an error message.
Has anyone encountered a similar problem? Is there a work around or solution?
RE: Testing to See if Loadout is Set
Posted: Thu Sep 19, 2019 5:42 am
by michaelm75au
example of script or test save?
RE: Testing to See if Loadout is Set
Posted: Thu Sep 19, 2019 11:38 am
by KnightHawk75
SeaQueen,
I presume you mean if you call GetLoadout({UnitName='somebasedaircraft1',LoadoutID=0}) on an aircraft who's loadout is unassigned? Doing it in console regularly will throw a object ref not set normally.
The trick\workaround is to wrap the GetLoadout call around setting the Tool_EmulateNoConsole() setting to true, as when it's true the error is suppressed and the return value will be nil and check-able, as it does when run under an event or special action context as well, just not in the console.
From some fragment I had related to this:
Code: Select all
-- Returns true on having loadout, false on nil or error, prints debug info
local function HasLoadout(u)
local ltext= 'empty';
local retval= false;
if u ~= nil then
print('unit: ' .. tostring(u.name));
Tool_EmulateNoConsole(true);
local l = ScenEdit_GetLoadout({UnitName=u.name,LoadoutID=0}); --request current assigned loadout
Tool_EmulateNoConsole(false);
if l ~=nil then
ltext= tostring(l.dbid);
retval= true;
else
ltext= 'No Loadout';
end
else
print('Invalid Unit');
end
print('LoadoutID: ' .. ltext);
ltext = nil;
return retval;
end
local unit1 = ScenEdit_GetUnit({name='Raptor99', guid='d20fea11-f966-4d7d-a0c5-9c8dafa02db0'}); --in the air with loadout
local unit2 = ScenEdit_GetUnit({name='TestAircraft #1'}); --at base with no loadout assigned
HasLoadout(unit1);
HasLoadout(unit2);
--Result with EmulateNoConsole disabled
--unit: Raptor99
--LoadoutID: 4523
--unit: TestAircraft #1
--ERROR: Object reference not set to an instance of an object.
--Result with above code (using EmulateNoConsole set to true temporarily).
--unit: Raptor99
--LoadoutID: 4523
--unit: TestAircraft #1
--LoadoutID: No Loadout
I agree it would be an improvement if nil were returned in both cases.
RE: Testing to See if Loadout is Set
Posted: Thu Sep 19, 2019 1:33 pm
by SeaQueen
Perfect!