How to get all unit of a side?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
yiyuezhuo
Posts: 5
Joined: Thu Feb 18, 2010 1:29 am

How to get all unit of a side?

Post by yiyuezhuo »

For example, I want to create a table that maps their initial name to their Guid once the scenario is loaded, and do something for every unit per second. However, I don't find a function to do the trick :( . Is it possible? Or maybe I must list all units manually?
User avatar
blu3s
Posts: 1400
Joined: Fri Jul 08, 2022 9:45 am

Re: How to get all unit of a side?

Post by blu3s »

You can acces all units from a side invoking the function VP_GetSide() retrieving a CMO__SIDE object and accesing their field .units. ex:

Code: Select all

local units = VP_GetSide({side='MySide'}).units
As you can read in the documentation the CMO__SIDE object has besides the units field, two methods that serve to return a table of units. The unitsBy method in which you can apply a filter to return only the ships, for example, ex

Code: Select all

local myside = VP_GetSide({side='MySide'})
local ship_units = myside:unitsBy('Ship')
Ant the method unitsInArea that retrieve all the units in a existing area. The parameter Area its a table with the names of the reference points from the area you want to get the units.

Code: Select all

local myArea = {'RP-1','RP-2','RP-3','RP-4'}
local mySide = VP_GetSide({side='MySide'})
local units_in_myArea = mySide:unitsInArea(myArea)
yiyuezhuo wrote: Sat Feb 25, 2023 6:25 am maps their initial name to their Guid once the scenario is loaded, and do something for every unit per second
Mmm I don't know if I'm understanding you well, but in order to perform actions with lua, this already happens, that is to say, you can access the units only through its GUID, in fact, it is preferable to do it always through this one since it is unique. If it is so that you have a reference of which GUID has a unit by name in order to make it easier to program the acitions, nice.
Post Reply

Return to “Lua Legion”