Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

GUID for Grouped Units

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> GUID for Grouped Units Page: [1]
Login
Message << Older Topic   Newer Topic >>
GUID for Grouped Units - 2/17/2021 7:51:30 PM   
tmoilanen

 

Posts: 72
Joined: 10/19/2011
Status: offline
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
Post #: 1
RE: GUID for Grouped Units - 2/17/2021 10:03:31 PM   
KnightHawk75

 

Posts: 1406
Joined: 11/15/2018
Status: offline
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".
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
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")






< Message edited by KnightHawk75 -- 2/17/2021 10:12:43 PM >

(in reply to tmoilanen)
Post #: 2
RE: GUID for Grouped Units - 2/18/2021 3:04:01 PM   
tmoilanen

 

Posts: 72
Joined: 10/19/2011
Status: offline
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()



(in reply to KnightHawk75)
Post #: 3
RE: GUID for Grouped Units - 2/18/2021 3:05:08 PM   
tmoilanen

 

Posts: 72
Joined: 10/19/2011
Status: offline
save file attached.

Attachment (1)

(in reply to tmoilanen)
Post #: 4
RE: GUID for Grouped Units - 2/19/2021 2:20:40 AM   
KnightHawk75

 

Posts: 1406
Joined: 11/15/2018
Status: offline
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.

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.


Attachment (1)

< Message edited by KnightHawk75 -- 2/19/2021 2:27:09 AM >

(in reply to tmoilanen)
Post #: 5
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> GUID for Grouped Units Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.324