scaredy Posted Wednesday at 12:08 AM Posted Wednesday at 12:08 AM (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 Wednesday at 12:09 AM by scaredy clarification
Kanelbolle Posted Wednesday at 08:53 AM Posted Wednesday at 08:53 AM 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. WARGAMES Servers: https://forum.dcs.world/topic/301046-wargames-server-pvp-and-pve-campaign-servers/ Scripts: RGC & SPGG https://github.com/AGluttonForPunishment/
Kanelbolle Posted Wednesday at 09:29 AM Posted Wednesday at 09:29 AM (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 Wednesday at 06:18 PM by Kanelbolle Removed some powershell syntax :D and missing end's - Added .miz WARGAMES Servers: https://forum.dcs.world/topic/301046-wargames-server-pvp-and-pve-campaign-servers/ Scripts: RGC & SPGG https://github.com/AGluttonForPunishment/
cfrag Posted 15 hours ago Posted 15 hours ago 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.
Recommended Posts