Hidden/Spoiler:
Most efficient way to check units' positions? [Solved]
Moderator: Moderators
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Most efficient way to check units' positions? [Solved]
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.
Last edited by Noobasaurus on Sat Jul 04, 2015 1:07 am, edited 1 time in total.
-
jedimoose32
- 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
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.
Code: Select all
if math.abs(xseeker - xhider) < tagDist ... etc.
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
Re: Most efficient way to check units' positions quickly
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!
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

- 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
To clarify, did you do what I suggested or only remove the print lines? Just curious.
-
Noobasaurus
- Droid Pilot Assassin

- Posts: 2006
- Joined: Tue Aug 17, 2010 5:56 pm
- [RDH]Zerted
- Gametoast Staff

- Posts: 2982
- Joined: Sun Feb 26, 2006 7:36 am
- Projects :: Bos Wars AI - a RTS game
- xbox live or psn: No gamertag set
- Location: USA
- Contact:
Re: Most efficient way to check units' positions quickly[Sol
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.
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.
