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

Get help using Construct 2

Post » Sat Feb 18, 2017 2:04 pm

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:

Code: Select all
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)

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.
Last edited by ErekT on Sat Feb 18, 2017 4:18 pm, edited 1 time in total.
B
42
S
16
G
6
Posts: 544
Reputation: 7,701

Post » Sat Feb 18, 2017 2:48 pm

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
"The intent is to provide subscribers with a sense of pride and accomplishment for unlocking different features...."
B
42
S
18
G
32
Posts: 818
Reputation: 20,779

Post » Sat Feb 18, 2017 3:34 pm

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).
Learning other engines, but C2 still rocks. Image Image Image Image
B
80
S
22
G
95
Posts: 2,418
Reputation: 57,673

Post » Sat Feb 18, 2017 4:18 pm

Thanks guys :)

I implemented Colludium's solution and it works great.
B
42
S
16
G
6
Posts: 544
Reputation: 7,701

Post » Sat Feb 18, 2017 5:44 pm

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

Sorry for joining the party late, but this can be done with three Picks.
ImageImageImage
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

Post » Sat Feb 18, 2017 5:47 pm

@blackhornet - I like it + very elegant!
Learning other engines, but C2 still rocks. Image Image Image Image
B
80
S
22
G
95
Posts: 2,418
Reputation: 57,673

Post » Sat Feb 18, 2017 6:01 pm

Just had a look. Sweet! Thanks, now I can avoid the extra loop :)
B
42
S
16
G
6
Posts: 544
Reputation: 7,701

Post » Sun Feb 19, 2017 4:09 am

blackhornet wrote: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 :D
B
54
S
33
G
95
Posts: 435
Reputation: 53,546


Return to How do I....?

Who is online

Users browsing this forum: Kyatric and 3 guests