[Solvd]How do I pick nearest instance that's not instance X?

0 favourites
  • 8 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • I have myself a bit of a problem. I want to pick the nearest instance to another instance, both of object 'Chr' and tried doing it like this:

    Local_X            (reference x-coordinate for instance A, passed to function)
    Local_Y            (reference y-coordinate for instance A, passed to function)
    
    Chr | Pick nearest to (Local_X, Local_Y)
    [/code:153lcdn6]
    But as I found out, instance A gets picked every time of course because obviously that's the instance closest to my reference x/y values. I'd like to exclude this instance from the picking somehow. Something like 'Pick nearest to (Local_X, Local_Y) that is not instance A'.
    
    Is that possible or do I need to do a manual bubblesort or something? Really hope I can avoid that.
  • Just create and instance variable.

    On start of layout set VarInstanceVar to self.IID

    On the same condition you want to detect nearest instance, put everything in a subevent with the condition:

    VarInstanceVar =/= X

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • How I would tackle this depends a bit on how you want to implement this condition in your game. I would do the following to loop through the instances of object Object, comparing the distance to each with a max search distance:

    var MyUID = Player.uid [the uid of the object you wish to ignore from this search]

    var OtherUID = -1

    var ObjectiveX, var ObjectiveY [set these to the x and y coordinates of the center of your search]

    var LocalDistance = 10000 [where this value is the max range of your search]

    C: For Each Object

    C: Object.uid != MyUID

    var tempDistance = distance(Object.x, Object.y, ObjectiveX, ObjectiveY)

    C: If tempDistance < LocalDistance

    -- A OtherUID = Object.uid

    -- A LocalDistance = tempDistance

    If OtherUID > -1 then that is the UID of the closest Object within the search range you specified (-1 = none found). You could also filter the objects for Is On Screen, or you could use a large invisible collision object to reduce the number of Object instances you loop through by first testing for Is Overlapping (only if your layout is huge with many Object instances, otherwise I wouldn't bother).

  • Thanks guys

    I implemented Colludium's solution and it works great.

  • http://www.blackhornettechnologies.com/Construct2Stuff/PickNearest_BHT.capx

    Sorry for joining the party late, but this can be done with three Picks.

  • blackhornet - I like it + very elegant!

  • Just had a look. Sweet! Thanks, now I can avoid the extra loop

  • http://www.blackhornettechnologies.com/Construct2Stuff/PickNearest_BHT.capx

    Sorry for joining the party late, but this can be done with three Picks.

    blackhornet

    Nice one, Thank you

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