enumerate units

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
pclaurent
Posts: 53
Joined: Fri May 17, 2019 5:29 pm

enumerate units

Post by pclaurent »

Simple question:
Is there a way to get a list of all existing units of a given side using a lua script?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: enumerate units

Post by KnightHawk75 »

If you want them not filtered by type at all...

local s = VP_GetSide({Side="myside"})
for k,v in pairs(s.units) do --units holds a table of all unit names and guids.
print('name: ' .. v.name .. ' guid: ' .. tostring(v.guid))
end

pclaurent
Posts: 53
Joined: Fri May 17, 2019 5:29 pm

RE: enumerate units

Post by pclaurent »

Wow, works great. Filtering by type should be a piece of cake. Thanks!
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: enumerate units

Post by KnightHawk75 »

Keep in mind you don't have to do some of the filtering yourself anymore in 1146+
You can use enhancements to s:unitsBy() filtering mechanisms for type and then subtype or category with-in that type to get back a prefiltered table. Super handy and faster than old way of sub filtering manually.

ie
side:unitsBy('Ship')
side:unitsBy('Ship',2002)
side:unitsBy('Ship', 2002, 3003)
side:unitsBy('Ship',,3002)
etc

pclaurent
Posts: 53
Joined: Fri May 17, 2019 5:29 pm

RE: enumerate units

Post by pclaurent »

got it - works fine. Tx
Post Reply

Return to “Lua Legion”