[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
39
S
16
G
6
Posts: 542
Reputation: 7,617

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
Image Image Image
B
31
S
11
G
27
Posts: 748
Reputation: 17,093

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).
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Sat Feb 18, 2017 4:18 pm

Thanks guys :)

I implemented Colludium's solution and it works great.
B
39
S
16
G
6
Posts: 542
Reputation: 7,617

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
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » Sat Feb 18, 2017 5:47 pm

@blackhornet - I like it + very elegant!
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,228
Reputation: 44,888

Post » Sat Feb 18, 2017 6:01 pm

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

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
39
S
22
G
76
Posts: 285
Reputation: 42,186


Return to How do I....?

Who is online

Users browsing this forum: KTML5 and 22 guests

cron