Testing for nearest object

How do you test for the nearest object x, to specific object, y when there are multiple instances of each object x and y? GD seems to be getting confused about specifically which object I am referring to.

You have many instances of ObjectY and many instances of ObjectX… For each ObjectY, ¿do you have to find the nearest ObjectX?

First: you need a loop “For each ObjectY”, sub-events will be executed for each ObjectY (of course :slight_smile: )
Second: you need a loop “For each ObjectX” as sub-event of the first loop
GD will run it (two ObjectY and two ObjectX):
…first ObjectY:
…first ObjectX: do something
…second ObjectX: do the same thing
…second ObjectY:
…first ObjectX: do something
…second ObjectX: do the same thing

Now you need a “Distance” variable to check each ObjectY-ObjectX distance:
If the distance with the next OjectX < variable “Ditance”, that ObjectX is nearest to ObjectY ==> variable “Distance” = this new distance.

Finally, to know who is the ObjectX between other ObjectX, check if the variable “Distance” = distance ObjectX-ObjectY (variable “Distance” = the smallest distance between an ObjectX and ObjectY)
Now you can link the ObjectY and ObjectX with “Link Extension”.

Pseudocode:

Condition to start checking dinstances: .........For each "ObjectY" repeat: ................Do variable(Distance) = 10000 (10000 = minimum distance) ................For each "ObjectX" repeat: ........................If distance between ObjectY and ObjectX < variable(Distance): ................................Do variable(Distance) = distance between ObjectX and ObjetcY ................Distance between ObjectX and ObjectY = variable(Distance): ........................Link ObjectY and ObjectX

This pseudocode is a copy of an 4ian’s explanation made in the forum some time ago (I hope he doesn’t get angry :smiling_imp: ), him code is very useful and simple: [url]Find object nearest to a point]

Do not hesitate to ask any quetion (I haven’t any PC with GD right now, if you need I can make a little example.gdg then :wink: )

Extremely helpful! I see I was failing because I assumed

was the other way around! >.<

Thank you Lizard! :slight_smile:

Double post but nvm: Does this mean event order is like this?

[code]These are done in order 1 - 3?
Event 1
Event 2
Event 3

These are done in order Event 1 - Sub event 1 - Sub event 2 - Event 2 ?

Event 1
Sub event 1
Sub event 2
Event 2

These are done in order Event 1 - Sub event 1 - Sub event 2 - Sub event 3 - Event 2?

Event 1
Sub event 1
Sub event 2
Sub event 3
Event 2 [/code]

Yep, sub events are run just after their parent event, if the conditions of the parent event were fulfilled. :slight_smile:

Mmmph… maybe we should make better explanations about events, sub-events, loops and GD logic in general… and add them on the wiki, of course.

I can’t, I mean: I could make examples with nice words, fine explanations and examples… but in Spanish, my English isn’t good, nobody would understand me :imp:

So a for each object loop sub event runs like an nested array loop?

For each object x 
    For each object y 
        For each object z

This would run for:
First x object → First y object → Every object z
First x object → Second y object → Every object z
…First x object → Last y object → Every object z
Second x object → First y object → Every object z
etc…?

@Lizard - If you can write the article I could easily make the English more understandable (if it even needs to be changed!). I have no troubles understanding anything you wrote on these forums before. It would be a very useful article to have!

Yes exactly.
How did you understand it before? ( It’s interesting because I’ve never imagined that anyone could get it wrong :slight_smile: ).

Maybe I should add a little paragraph about subevents on this page: wiki.compilgames.net/doku.php/en … s_concepts
Or Lizard, if you want to write an article or tutorial, feel free to do so: Notify us when it is done, and we’ll correct any mistake :slight_smile:

I initially thought it would do it sequentially like this:

event for each object x //Do everything in this loop sub event for each object y //Then do everything in this loop sub event for each object z //Then do everything in this loop

I’m honestly not sure why I thought it would work differently than standard C++… :astonished:

Ok, I’ll do that, give me some days :wink: