Jump to content

Recommended Posts

Posted (edited)

How do I obtain the unit object if I know its unitID? I cannot find a Unit.GetById anywhere, and don't believe it exists.

 

For example, if I know a unit's name, I can get the unit object simply using Unit.getByName('unit name'), but how do I do that when I only know the unitID?

Edited by scaredy
clarification
  • scaredy changed the title to Unit.GetByID?
Posted
8 hours ago, scaredy said:

How do I obtain the unit object if I know its unitID? I cannot find a Unit.GetById anywhere, and don't believe it exists.

 

For example, if I know a unit's name, I can get the unit object simply using Unit.getByName('unit name'), but how do I do that when I only know the unitID?

Shot answer:

 

You can do this by getting all groups in a coalition with  coalition.getGroups

and getting all the id's of all the units in all the groups with Group.getUnits

then make a Database with all the id's.

Posted (edited)
unitsDB = unitsDB or {}
 
function updateUnitDB()

    -- Remove the old data in $unitsDB
    unitsDB = nil
    unitsDB = unitsDB or {}

    -----------------------------------
    -- Get Red Coalition Forces	-------------------
	-----------------------------------
	for i, gp in pairs(coalition.getGroups(1)) do 

		local _GpName = Group.getName(gp)
		
		if _GpName ~= nil then
		
			if (Group.getByName(_GpName) and Group.getByName(_GpName):getSize() > 0) and (Group.isExist(gp) == true) then
					
				for index, _unit in pairs(gp:getUnits()) do

                        _unitId = _unit:getID()
                        _unitName = _unit:getName()
                        unitsDB[tonumber(_unitId)] = {["name"] =  _unitName, ["groupname"] =  _GpName,}
                        env.info('Adding to database- Id: ' .. _unitId .. ' - Name: ' .. _unitName)
                        trigger.action.outText('Adding to database- Id: ' .. _unitId .. ' - Name: ' .. _unitName, 10)
                end  -- for index, _unit in pairs(gp:getUnits()) do
		
			end -- if (Group.getByName(_GpName) and Group.getByName(_GpName):getSize() > 0) and (Group.isExist(gp) == true) then
		
		
		end -- if _GpName =~ nil then
		
	end -- for i, gp in pairs(coalition.getGroups(1)) do

       -----------------------------------
    -- Get Blue Coalition Forces	-------------------
	-----------------------------------
	for i, gp in pairs(coalition.getGroups(2)) do 

		local _GpName = Group.getName(gp)
		
		if _GpName ~= nil then
		
			if (Group.getByName(_GpName) and Group.getByName(_GpName):getSize() > 0) and (Group.isExist(gp) == true) then
					
				for index, _unit in pairs(gp:getUnits()) do

                        _unitId = _unit:getID()
                        _unitName = _unit:getName()
                        unitsDB[tonumber(_unitId)] = {["name"] =  _unitName, ["groupname"] =  _GpName,}
                        env.info('Adding to database- Id: ' .. _unitId .. ' - Name: ' .. _unitName)
                        trigger.action.outText('Adding to database- Id: ' .. _unitId .. ' - Name: ' .. _unitName, 10)

                end  -- for index, _unit in pairs(gp:getUnits()) do
		
			end -- if (Group.getByName(_GpName) and Group.getByName(_GpName):getSize() > 0) and (Group.isExist(gp) == true) then
		
		
		end -- if _GpName =~ nil then
		
		
	end -- for i, gp in pairs(coalition.getGroups(2)) do

	
end -- function updateUnitDB()

function getUnitByid(id)
     local _id = tonumber(id)
	-- for index, _u in pairs(unitsDB) do 
		-- trigger.action.outText('From database index : ' .. index .. ' - Unit Name: ' .. unitsDB[index].name, 10)
  	-- end
		
	if unitsDB[_id] ~= nil then
		trigger.action.outText('From database- Name - '.. unitsDB[_id].name, 10)
		local _unit = Unit.getByName(unitsDB[_id].name)
		if _unit then
			return  _unit
		else
			return false
		end
	end
	
	return false

		   
end

 

Added a working .miz test file.

getUnitId.miz

Edited by Kanelbolle
Removed some powershell syntax :D and missing end's - Added .miz
Posted
On 5/7/2025 at 11:29 AM, Kanelbolle said:
function updateUnitDB()

A really nice utility. Be sure that you amend this by e.g. patching coalition.addGroup() to include all new, dynamically spawned units if your mission supports dynamic spawns, else the new units will not be included in getByID.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...