About function in lua GetUnitNearestEnemy

About function in lua GetUnitNearestEnemy

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

About function in lua GetUnitNearestEnemy

Post by Broker »

Hi all!

I known this function Spring.GetUnitNearestEnemy but if i need get all enemy units in specific range. is it possible?
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: About function in lua GetUnitNearestEnemy

Post by Silentwings »

Yes, use a Spring.GetUnitsIn**** call and then iterate through the unit list it returns to find enemy units.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: About function in lua GetUnitNearestEnemy

Post by FLOZi »

Code: Select all

local x, y, z = Spring.GetUnitPosition(unitID)
local teamID = Spring.GetUnitTeam(unitID) -- if you don't know it already
local unitsInRange = Spring.GetUnitsInCylinder(x,y,z, RANGE) 
local enemyInRange = {}
for _, unitInRangeID in pairs(unitsInRange) do
  local unitTeam = Spring.GetUnitTeam
  if not Spring.AreTeamsAllied(unitID, unitInRangeID) then
    table.insert(enemyInRange, unitInRangeID)
  end
end
Just a e.g. it isn't the cleanest possible
User avatar
BrainDamage
Lobby Developer
Posts: 1164
Joined: 25 Sep 2006, 13:56

Re: About function in lua GetUnitNearestEnemy

Post by BrainDamage »

Code: Select all

Spring.GetUnitsInSphere(x,y,z,radius,Spring.ENEMY_UNITS)
also, this should be moved to lua forum
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: About function in lua GetUnitNearestEnemy

Post by FLOZi »

BrainDamage wrote:

Code: Select all

Spring.GetUnitsInSphere(x,y,z,radius,Spring.ENEMY_UNITS)
also, this should be moved to lua forum
Much better, totally forgot Spring.ENEMY_UNITS even existed. :oops:
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: About function in lua GetUnitNearestEnemy

Post by Silentwings »

oh me too :p
Post Reply

Return to “Lua Scripts”