How you Find the nearest object to another object?

  • Multiple instances of same kinds, no for each loops? I'm trying to use built in looping but it seems impossible.

    note: I am using choosing by UID, but to even choose by UID you must loop through instances, hence the problem as it stops -short.

  • You could do it like this. First you pick the object and save it's x, y and uid to some variables. Next you use an inverted "pick by uid" to pick all the instances except the original instance. The you can use the "pick closest" condition as usual.

    global number x=0

    global number y=0

    global number obj=-1

    pick sprite at random

    set x to sprite.x

    set y to sprite.y

    set obj to sprite.uid

    [inverted] pick sprite instance by uid obj

    sprite: pick closest to (x,y)

    do something

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • thanks ill give it a shot hope I understand this right.

    Do you know of LoS objects are more resource intensive than simple for each loops?

  • They do different things so I'm unsure of the comparison.

  • Ah, anyway I just remember that your way of doing this is actually a global for each loop and one Ive already tested before, I was trying to give a bunch of objects a brain object and test each Unique (If UIDObject == UIDObjectBrain :: Do something) ID exists per brain do the search but I still need the for each loop to find the nearest object target to each one.

    It seems it is inescapable that to loop the objects you'll either do a global for each loop or a regular one. I'll just deal. Thanks again for making me realize this.

  • In that case the events could look like this. There's no avoiding the "for each", and even if you could it would still be done internally. For example the "pick closest" condition will actually loop through the objects to find the closest.

    global number x=0

    global number y=0

    global number obj=-1

    For each sprite

    set x to sprite.x

    set y to sprite.y

    set obj to sprite.uid

    --- pick all sprite

    --- [inverted] pick sprite instance by uid obj

    --- sprite: pick closest to (x,y)

    --- > do something

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)