Page 1 of 1

How to get all unit of a side?

Posted: Sat Feb 25, 2023 6:25 am
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?

Re: How to get all unit of a side?

Posted: Sat Feb 25, 2023 8:10 am
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.