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 » 16 Oct 2013, 09:24

Hi all!

I known this function Spring.GetUnitNearestEnemy but if i need get all enemy units in specific range. is it possible?
0 x

User avatar
Silentwings
Moderator
Posts: 3570
Joined: 25 Oct 2008, 00:23

Re: About function in lua GetUnitNearestEnemy

Post by Silentwings » 16 Oct 2013, 09:48

Yes, use a Spring.GetUnitsIn**** call and then iterate through the unit list it returns to find enemy units.
0 x

User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6088
Joined: 29 Apr 2005, 01:14

Re: About function in lua GetUnitNearestEnemy

Post by FLOZi » 16 Oct 2013, 18:47

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
0 x

User avatar
BrainDamage
Lobby Developer
Posts: 1164
Joined: 25 Sep 2006, 13:56

Re: About function in lua GetUnitNearestEnemy

Post by BrainDamage » 16 Oct 2013, 19:02

Code: Select all

Spring.GetUnitsInSphere(x,y,z,radius,Spring.ENEMY_UNITS)
also, this should be moved to lua forum
0 x

User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6088
Joined: 29 Apr 2005, 01:14

Re: About function in lua GetUnitNearestEnemy

Post by FLOZi » 16 Oct 2013, 19:33

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:
0 x

User avatar
Silentwings
Moderator
Posts: 3570
Joined: 25 Oct 2008, 00:23

Re: About function in lua GetUnitNearestEnemy

Post by Silentwings » 16 Oct 2013, 20:23

oh me too :p
0 x

Post Reply

Return to “Lua Scripts”