GUID for Grouped Units

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
tmoilanen
Posts: 74
Joined: Wed Oct 19, 2011 4:28 pm

GUID for Grouped Units

Post by tmoilanen »

Is there anything that differentiates the GUID of a group (say a flight assigned to patrol) from an individual unit.

I'm working on a script that queries the units assigned to a mission, and if they have been airborne for longer than 6 hours I will order them to RTB.

The script throws an error (ERROR: Object reference not set to an instance of an object), and I'm fairly certain it is coming from a group GUID.

Any help would be appreciated. My code is below:

local myside = 'United States'
local mymission = 'CAP - Eareckson'

local a = ScenEdit_GetMission(myside,mymission)
print(a)
local u = a.unitlist
print(u)
local cnt_u = (#u)
print(cnt_u)

for i = 1,cnt_u
do
local myguid = u
print(myguid)

--get unit information
local myunit = ScenEdit_GetUnit({guid=myguid})
print(myunit)
local mytype = myunit.type
print(mytype)

--get airborne time
local abtime = myunit.airbornetime
print(abtime)
if abtime ~= nil then
local h = string.sub(abtime,1,1)
print(h)
h = h * 1
print(h)
--if unit has been airborne for 6 hours RTB
if h == 6 then ScenEdit_SetUnit({guid=myguid,RTB=true}) end
end
end
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: GUID for Grouped Units

Post by KnightHawk75 »

Since you're already getting each member unit why not just skip when unit.type == "Group"?

Reworked with that and I think ?probably? more resilient parsing of the "hours".

Code: Select all

 local function handleUnitMember(theguid)
   if theguid == nil then print("handleUnitMember(): theguid parameter missing"); return; end
   local retval,u = pcall(ScenEdit_GetUnit,{guid=theguid})
   if (retval ~=nil and u ~=nil) and retval ==true then --we have obtained a valid wrapper and no error message.
     if (u.type ~= "Group") then
       --process single unit.
       local retval2 = string.match(u.airbornetime,"(%d*)%shr") --do we even have xx hr if so grab the digits before the space.
       if retval2 ~=nil then --capture successful
         if tonumber(retval2) >= 6 then u:RTB(true); return true; end
       else
         print("handleUnitMember(): debug: Skipping unit hasn't been airborne an hour yet.");
       end
     end
   else
     print("handleUnitMember(): Error could not obtain unit for guid " .. tostring(theguid) .."  msg:" .. tostring(u));
   end
   return false; --skipped or problem.
 end

Code: Select all

local function processMission(theside,themission)
   if theside == nil or themission ==nil then print("processMission(): Error one or more invalid parameters."); return;end
   local retval,m = pcall(ScenEdit_GetMission,theside,themission)
   if (retval ~=nil and m ~=nil) and retval == true then 
     for _,v in pairs(m.unitlist) do
       handleUnitMember(v)
     end
   else
     print(string.format("processMission(): Could not obtain mission with side %s and missionname %s",tostring(theside),tostring(themission)))
   end
 end
 
processMission("United States","CAP - Eareckson")




tmoilanen
Posts: 74
Joined: Wed Oct 19, 2011 4:28 pm

RE: GUID for Grouped Units

Post by tmoilanen »

Thanks, KnightHawk - I see what you're trying to do with the error handling, but I still can't get it to work. If the GUID belongs to a group, it is throwing the "Object Reference Not Set to an Object" error, and never interrogating the unit type to see if it belongs to a group.

Any thoughts on how I can get around this?

local myside = 'United States'
local mymission = 'CAP - Eareckson'

local m = ScenEdit_GetMission(myside,mymission)
print(m)

local units = m.unitlist
print(units)

local myguid = units[1]
print(myguid)

local function handleUnitMember()
if myguid == nil then print("handleUnitMember(): missing GUID"); return; end
local retval,myunit = pcall(ScenEdit_GetUnit,{guid=myguid})
if (retval ~= nil and myunit ==nil) and retval ~= true then print("Error"); return; end
if (retval ~=nil and myunit ~=nil) and retval ==true then
print(myunit)
end
end

handleUnitMember()


tmoilanen
Posts: 74
Joined: Wed Oct 19, 2011 4:28 pm

RE: GUID for Grouped Units

Post by tmoilanen »

save file attached.
Attachments
LUA_TestScenario.zip
(13.71 KiB) Downloaded 2 times
KnightHawk75
Posts: 1556
Joined: Thu Nov 15, 2018 7:24 pm

RE: GUID for Grouped Units

Post by KnightHawk75 »

I get no such errors when testing with the code posted in #2 _untouched_, applied to your save. See the special action in the save, execute it whenever you want, especially after 6hr, I get no such errors when using it and when run after 6hr of airtime the units RTB as expected.

The code you posted in #3 is not going to work or do anything. You're calling a function passing it no parameters, and using a variable inside the function that doesn't exist 'myguid' in that context\scope, it will do nothing and fail. For educational purposes if you wanted it to work (not sure why you're reinventing a wheel already provided), you would for instance feed 'myguid' as a parameter to your function.... as you can see is done in post #2.

Code: Select all

 local function handleUnitMember(paramGuid)
   if paramGuid == nil then print("handleUnitMember(): missing GUID"); return; end
   local retval,myunit = pcall(ScenEdit_GetUnit,{guid=paramGuid})
   if (retval ~=nil and myunit ~=nil) and retval ==true then
     print(myunit);
   else
     print("Error");
   end
 end
 local myside = 'United States'
 local mymission = 'CAP - Eareckson'
 local m = ScenEdit_GetMission(myside,mymission)
 print(m)
 local units = m.unitlist
 print(units)
 local myguid = units[1]
 print(myguid) 
 handleUnitMember(myguid) 
 

Btw forum tip, if you want the forum post to maintain code indents etc, use the code tags around your code. You will have to use multiple code tags to split it up though if it's longer than than about 15-20 lines though or else it can get all mangled for some reason.
Attachments
LUA_GroupG..Scenario.zip
(18.58 KiB) Downloaded 4 times
Post Reply

Return to “Lua Legion”