----------------------------------------------------------------------------------------------------------------------------- -- SafeGetUnit ----------------------------------------------------------------------------------------------------------------------------- -- Description: -- Safely get the unit, meaning it wil return the unit, or nil, depending upon unit existance without throwing an error. -- This function wraps ScenEdit_GetUnit in a pcall and traps the error, if one is thrown by Command. -- Use this where you normally have ScenEdit_GetUnit (with the same parameters) -- If all three parameters give, guid will be used. -- -- Parameters: -- UnitSelector table must have either guid, or name and side defined to work. -- UnitSelector.guid string string containing the target unit's unique guid (unique to this game instance) -- UnitSelector.side string string containing the target unit's side -- UnitSelector.name string string containing the target unit's name -- -- Return: the unit wrapper loaded with the unit data, or nil on any error. -- --------------------------------------------------------------------------------------------------------------------------- SafeGetUnit = function(unitSelector) if unitSelector == nil or type(unitSelector) ~= 'table' then return nil end local side_name = unitSelector.side local unit_name = unitSelector.name local unit_guid = unitSelector.guid local retVal = nil local e , p if (unitguid ~= nil and type(guid == 'string')) then --potentially good guid e, p = pcall(ScenEdit_GetUnit,{side='test', name='test_unit'}) if e==true and p~= nil then retVal = p end elseif (side_name ~= nil and type(side_name == 'string')) and (unit_name ~= nil and type(unit_name == 'string')) then --potenitally good side and unit values e, p = pcall(ScenEdit_GetUnit,{side='test', name='test_unit'}) if e==true and p~= nil then retVal = p end end return retVal end ----------------------------------------------------------------------------------------------------------------------------- -- SafeGetUnit Test Code ----------------------------------------------------------------------------------------------------------------------------- local function RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, successExpected) ScenEdit_AddSide({name=mySide}) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) local u = SafeGetUnit({side=testSide, name=testUnit}) if successExpected == true then if (u ~= nil) and (u.name == myUnit) and (u.side == mySide) then print 'PASSED' else print('FAILED') end else if (u == nil) then print 'PASSED' else print('FAILED') end end ScenEdit_RemoveSide({name=mySide}) end --test 1 - good side, good unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = 'test' local testUnit = 'test_unit' RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, true) end --test 2 - good side, bad unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = 'test' local testUnit = 'asdf' RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, false) end --test 3 - bad side, good unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = 'asdf' local testUnit = 'test_unit' RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, false) end --test 4 - bad side, bad unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = 'asdf' local testUnit = 'asdf' RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, false) end --test 5 - good side, nil unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = 'test' local testUnit = nil RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, false) end --test 6 - nil side, good unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = nil local testUnit = 'test_unit' RunSafeGetUnitTest(mySide, myUnit, testSide, testUnit, false) end --test 7 - nil side, nil unit do local mySide = 'test' local myUnit = 'test_unit' local testSide = nil local testUnit = nil RunSafeGetUnitTest_1(mySide, myUnit, testSide, testUnit, false) end --test 8 - good guid (only) do local mySide = 'test' local myUnit = 'test_unit' ScenEdit_AddSide({name=mySide}) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) tUnit = ScenEdit_GetUnit({side=mySide, name=myUnit}) local testGuid = tUnit.guid local result = SafeGetUnit({guid=testGuid}) if (result ~= nil) and (result.name == myUnit) and (result.side == mySide) then print 'PASSED' else print('FAILED') end ScenEdit_RemoveSide({name=mySide}) end --test 9 - bad guid (only) do local mySide = 'test' local myUnit = 'test_unit' ScenEdit_AddSide({name=mySide}) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) tUnit = ScenEdit_GetUnit({side=mySide, name=myUnit}) local result = SafeGetUnit({guid='asdf'}) if (result == nil) then print 'PASSED' else print('FAILED') end ScenEdit_RemoveSide({name=mySide}) end --test 10 - good guid, good side, good unit do local mySide = 'test' local myUnit = 'test_unit' ScenEdit_AddSide({name=mySide}) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) tUnit = ScenEdit_GetUnit({side=mySide, name=myUnit}) local testGuid = tUnit.guid local result = SafeGetUnit({side=mySide, name=myUnit, guid=testGuid}) if (result ~= nil) and (result.name == myUnit) and (result.side == mySide) then print 'PASSED' else print('FAILED') end ScenEdit_RemoveSide({name=mySide}) end --test 11 - good guid, good side, good unit, but wrong side and unit do local mySide = 'test' local myUnit1 = 'test_unit1' local myUnit2 = 'test_unit2' ScenEdit_AddSide({name=mySide}) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit1, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) ScenEdit_AddUnit({side=mySide, type='Facility', name=myUnit2, dbid=2784, latitude=19.004100276608, longitude=110.24596193077, }) tUnit = ScenEdit_GetUnit({side=mySide, name=myUnit1}) local testGuid = tUnit.guid local result = SafeGetUnit({side=mySide, name=myUnit2, guid=testGuid}) if (result ~= nil) and (result.name == myUnit1) and (result.side == mySide) then print 'PASSED' else print('FAILED') end ScenEdit_RemoveSide({name=mySide}) end