Defining closest contact to unit
Posted: Fri Jul 16, 2021 10:27 am
Apologies if this has been answered elsewhere. I'm trying to define a function that returns the closest contact to a selected unit. In some instances I may want the controlled (Player or AI) unit to *not* engage the closest hostile, for example if they are tasked to a HVAA rundown. I can get to the point where I return a table of contacts with associated ranges for a specific unit ... but I've run into a brick wall trying to get to the next step!
There's probably a few dead ends in the code, I've borrowed liberally from other posts on this forum and am clunking along in Lua.
Code: Select all
function closest_contact_to(name)
local close_c = false
local unit = ScenEdit_GetUnit({name=name, guid=guid})
local side = ScenEdit_PlayerSide()
local contacts = ScenEdit_GetContacts(unit.side)
local myTempTable = {}
if (contacts ~=nil) then
for k,v in pairs(contacts) do
table.insert(myTempTable, {name=v.name, guid=v.guid})
end
end
contacts = nil;Code: Select all
if (#myTempTable >0) then
local min_range = 99999
local con;
for k,v in pairs(myTempTable) do
con = VP_GetUnit({guid=v.guid});
if ((con ~=nil) and (con.type == 'Aircraft')) then
range = Tool_Range(unit.guid, v.guid)
print(string.format( 'Range from %s to unit %s is %.2f nm', tostring(v.name), tostring(unit.name), tostring(range))); --test by printing to console
end
-- something needs to go here...!
--
con = nil;
end
myTempTable = nil
end
return close_c
endThere's probably a few dead ends in the code, I've borrowed liberally from other posts on this forum and am clunking along in Lua.