Most efficient way to check units' positions? [Solved]
Posted: Fri Jul 03, 2015 4:21 pm
by Noobasaurus
I'm trying to check all of the units' positions on the battlefield every half second or so and then compare them to each other. If units of different teams are close enough to each other, stuff happens. I finally got some code working, but the game froze when it ran because of how many calculations were being done. So I'll post the code here and hopefully someone can help me improve it.
OnTimerElapse(
function(timer)
print("checker")
for i, v in pairs(unitsAlive) do
print("i ", i)
if GetCharacterTeam(i) == 1 then
seeker = GetCharacterUnit(i) --seeker
local x, y, z = GetWorldPosition(seeker)
local seekerpos = {x, y, z}
for a, b in pairs(unitsAlive) do
print("a ", a)
if GetCharacterTeam(a) == 2 then
hider = GetCharacterUnit(a) --hider
local x, y, z = GetWorldPosition(hider)
local hiderpos = {x, y, z}
local tagDist = 3
if math.abs(seekerpos[1] - hiderpos[1]) < tagDist --my fav code ever
and math.abs(seekerpos[2] - hiderpos[2]) < tagDist
and math.abs(seekerpos[3] - hiderpos[3]) < tagDist then
--hider has been tagged
print("Close enough, do Tag")
Tag(a)
end
end
end
end
end
SetTimerValue(checker, 0.5)
StartTimer(checker)
end,
checker
)[/code]
Re: Most efficient way to check units' positions quickly
Posted: Fri Jul 03, 2015 11:40 pm
by jedimoose32
It's strange that those calculations are causing a freeze for you. I wrote an almost identical section of code for Assassination and it ran with no problems. The main difference is that I didn't store the x,y,z variables in a table - instead what I would do in your case is local xseeker, yseeker, zseeker = GetWorkdPosition(seeker), and then later, local xhider, yhider, zhider = GetWorldPosition(hider). Then when you want to check the distances, just write:
I'm not a Lua-guru but my understanding is that local variables are held in memory for the duration of the function or loop in which they are created, so your first x,y,z trio (for seeker) might be overridden by your second x,y,z trio (hider) since the hider set is in an if-then nested inside the same if-then where the seeker ones are defined. That may be part of (or all of) the problem.
Re: Most efficient way to check units' positions quickly
Posted: Sat Jul 04, 2015 1:06 am
by Noobasaurus
Thanks, I believe that helped, but I'm not completely sure yet. I removed the print messages and that stopped the lag.
I was going to say I made it more streamlined but I really didn't do much.
But thanks for your help!
Re: Most efficient way to check units' positions quickly[Sol
Posted: Sat Jul 04, 2015 1:31 am
by jedimoose32
To clarify, did you do what I suggested or only remove the print lines? Just curious.
Re: Most efficient way to check units' positions quickly[Sol
Posted: Sat Jul 04, 2015 1:51 am
by Noobasaurus
Both.
Re: Most efficient way to check units' positions quickly[Sol
Posted: Mon Jul 13, 2015 12:44 am
by [RDH]Zerted
There are fancy algorithms that couple potentially make this type of code a lot faster. Basically you store units by positions instead of in a list, then you only need to check units stored near each other instead of all units. However, if your unit counts are low enough than it might not be worth the effort to code them.
seekerpos vs hiderpos was fine. x, y, and z were changed, but seekerpos didn't contain x, y, and z. It contained whatever x, y, and z were pointing at when seekerpos was created. Changing x, y, or z wouldn't change seekerpos just as changing seekerpos wouldn't change x, y, or z. There's a difference between a variable name and it's value. Think of the names as pointing to values in memory. Doing something like variable1 = varible2 means variable1 is pointing to the same thing as variable2. Changing what variable2 points to don't change the old value, so variable1 is still correctly pointing at it. Use your fingers as variable names. If two fingers are pointing at the same thing and you move one of them, the other finger is still pointing at the original thing. You don't point at your other finger and say: hey look at what this finger is pointing at but instead point directly at the thing that's interesting. If all that was too confusing, just ignore it and name everything differently.