How do I locate all objects witin a certain radius of my ai

I need a way to locate all objects of a certain type that are within a set radius of my ai.
e.g. I want to create a list of all trees within 100 pixels of my ai. Note that there can be >1000 trees in the game and I think that iterating over all objects will be very time consuming. And there will be >100 ai.


Can you explain more exactly what you want to do? There is no reason I can think of you would need to rapidly iterate over so many object’s positions.

Hi, I’m testing it with some objects, I tested the speed of doing a double iteration with a function condition (checking square distance = faster) vs the distance condition (it checks distance (sqrt) but it seems that doesn’t need explicit iterations)

The distance condition is a lot faster (0.28 - 0.30 ms) than a double iteration with square distance condition (5.80 - 6.00 ms)… :neutral_face:

If there were a square distance condition instead just an object method, maybe your events could be even faster :frowning:

I would like to have a AI “woodcutter” that cuts the nearest tree and than returns to his home.
His home can be placed at a random (user selected) location.
There are many trees in a level.
I’m only interested in the trees close to the building.

Check this :smiley: :
TreeChop.7z (31.9 KB)
Of course what you are looking for is in the first control event, where the Sir Woodcutter select the nearest tree (note that the selected Tree is the nearest to the Man, not the House, but of course you can just switch the object) :slight_smile:

That sounds like it.
At the risk of sounding dumb: how do I do this?

  • How does Sir Woodcutter find the nearest tree (the AI should do this by itself)
    And which “fisrt control event” should I use (what is it called or where can I find it)?

The events are documented… but they are folded inside the groups (I assume you know how to unfold them, and you have the latest version of GD wich support groups), In the second group, the first event find the nearest Tree :wink:

In general, to find the nearest object, you need something like this (image courtesy of victor):

In my example, instead link objects, I save the Tree ID, to know the specific Tree to cut :slight_smile:

Hi Lizard,
Thanks for the explanation!
In this case you are still iterating over all soldiers (trees) right?
Which is what I wanted to prevent.
Because this could end up pretty time consuming.
I’ll give it a try end see where it ends when I have hundreds of soldiers finding their enemies all at the “same” time.

Because my trees are all static (they don’t move) it could be an idea to go over them all once when a hut is placed. Next put all trees within a certain distance in a list and let the woodcutter go over the list one by one.
(I’m going to find out how to do that next :smiley: )

Thanks a lot.