Most efficient way to check units' positions? [Solved]

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Post Reply
Noobasaurus
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2006
Joined: Tue Aug 17, 2010 5:56 pm

Most efficient way to check units' positions? [Solved]

Post 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.
Hidden/Spoiler:
[code]
unitsAlive = {}

unitSpawn = OnCharacterSpawn(
function(character)
playerInfo = {playerUnit = GetCharacterUnit(character), playerTeam = GetCharacterTeam(character)
}
unitsAlive[character] = playerInfo
end)


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]
Last edited by Noobasaurus on Sat Jul 04, 2015 1:07 am, edited 1 time in total.
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: Most efficient way to check units' positions quickly

Post 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:

Code: Select all

if math.abs(xseeker - xhider) < tagDist ... etc.
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.
Noobasaurus
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2006
Joined: Tue Aug 17, 2010 5:56 pm

Re: Most efficient way to check units' positions quickly

Post by Noobasaurus »

Thanks, I believe that helped, but I'm not completely sure yet. I removed the print messages and that stopped the lag. :P

I was going to say I made it more streamlined but I really didn't do much.

But thanks for your help!
jedimoose32
Field Commander
Field Commander
Posts: 938
Joined: Thu Jan 24, 2008 12:41 am
Projects :: Engineering Degree
Location: The Flatlands of Canada

Re: Most efficient way to check units' positions quickly[Sol

Post by jedimoose32 »

To clarify, did you do what I suggested or only remove the print lines? Just curious.
Noobasaurus
Droid Pilot Assassin
Droid Pilot Assassin
Posts: 2006
Joined: Tue Aug 17, 2010 5:56 pm

Re: Most efficient way to check units' positions quickly[Sol

Post by Noobasaurus »

Both.
User avatar
[RDH]Zerted
Gametoast Staff
Gametoast Staff
Posts: 2982
Joined: Sun Feb 26, 2006 7:36 am
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set
Location: USA
Contact:

Re: Most efficient way to check units' positions quickly[Sol

Post 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.
Post Reply