Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Gather all units of single type in a list?

 
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 >> Gather all units of single type in a list? Page: [1]
Login
Message << Older Topic   Newer Topic >>
Gather all units of single type in a list? - 12/6/2020 3:59:09 AM   
aethertap

 

Posts: 2
Joined: 11/30/2020
Status: offline
Hi all,

Just getting started with Lua scripting and CMO in general. I have experience with Python.

I'm wondering if there's a function or straightforward 'for' loop that will access all units on a side of type 'Surface Ship.' I have a scenario where I've placed a ton of random commercial merchant traffic and I'm looking to go through them and assign home bases and/or random headings. Obviously, the intent is to litter the area with random sea traffic, but I'm not sure that most straightforward way of automating this. Thanks!
Post #: 1
RE: Gather all units of single type in a list? - 12/6/2020 11:02:12 AM   
boogabooga

 

Posts: 457
Joined: 7/18/2018
Status: offline
Yes, that's difficult because generally you need to know a unit's name or guid ahead of time in order to GetUnit.

What I do, following others, is to create units directly in LUA and list them in a table as they are created in order to keep track of them. EX:

quote:

function AddFingerFour(callsign,side,dbid,loadout,latitudeIn,longitudeIn,heading,altitude,proficiency,groupName)
--local circle = World_GetCircleFromPoint({latitude=latitude,longitude=longitude,radius=0.5,numpoints = 72})
local addedAircraftTable = {}

blah blah blah

local unit = ScenEdit_AddUnit({type='Aircraft',side=side,dbid=dbid,name=callsign..' #'..i,loadoutid=loadout,latitude=position.latitude,longitude=position.longitude,heading=heading,altitude=altitude,proficiency=proficiency});
unit.group=groupName
table.insert(addedAircraftTable,unit)

return addedAircraftTable
end



Then the loop to access each unit:

quote:


for key, value in pairs(addedAircraftTable) do

blah blah blah

(in reply to aethertap)
Post #: 2
RE: Gather all units of single type in a list? - 12/6/2020 1:24:18 PM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

I'm wondering if there's a function or straightforward 'for' loop that will access all units on a side of type 'Surface Ship.' ...

---

Yes. Get the side wrapper, grab the unitlist by using unitsBy('Ship') method;

local s = VP_GetSide({side="Red"});
local tbl = s:unitsBy('Ship'); --All surface ships
--local tbl2 = s:unitsBy('Ship','2002'); --example to further constrained to Surface Combatant
--local tbl3 = s:unitsBy('Ship',nil,'2008'); --example to constrain to CVN sub-type's only
local u; --unit wrapper
local retval = false; --return value from pcall
for k,v in pairs(tbl) do
  retval,u = pcall(ScenEdit_GetUnit,{guid=v.guid}); --suppress errors.
  if (retval == true) and u ~=nil then
     --Do Stuff You want with each unit.
     print(string.format("Unitname: %s  Lat: %s Lon: %s",tostring(u.name),tostring(u.latitude),tostring(u.longitude)));
     --
  else
    print('Could not obtain unit. err: ' .. tostring(u)); --u contains error msg if retval is false.
  end
  retval,u = false,nil;
end




< Message edited by KnightHawk75 -- 12/6/2020 1:29:01 PM >

(in reply to boogabooga)
Post #: 3
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Gather all units of single type in a list? 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

2.438