Nearest instance?

Discussion and feedback on Construct 2

Post » Tue Sep 20, 2011 11:02 am

When I mention an object in another object's field (e.g. move towards object), if there are multiple instances of the second object, it does not pick the nearest instance but a seemingly random one.

Why does this happen? Could a feature be implemented to make the nearest instance be chosen (since I am sure that 99% of the time people would want this).

If this is unavoidable, what can I do to make the object choose the nearest instance?

thanks, sqiddster
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Sep 20, 2011 12:25 pm

I agree, that would be a very useful function. Seems strange that it's not already available, got used to that in Game Maker quite easy to implement as a plugin but would be better to be in the main built-in System object.
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Tue Sep 20, 2011 1:22 pm

There was an option in CC "Pick closest", in C2 it's not yet added (hope it will).

You can do it in many ways. Ie. add a instance variable "pick" or whatever and them compare that value to what you want to do.
ImageImageImageImage
B
140
S
58
G
36
Posts: 2,547
Reputation: 31,170

Post » Tue Sep 20, 2011 2:31 pm

Picking the nearest object would slow down games a lot - instead of taking the first picked (which seems to you to be random), it would have to check every single instance measuring its distance. If you have 100 object instances, that's 100 times as much work, and it affects every single condition!

You can add a for-each loop yourself to test each individual instance's distance. I'll see if I can add a 'pick closest' for the next build.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,508

Post » Tue Sep 20, 2011 3:13 pm

[QUOTE=Ashley] Picking the nearest object would slow down games a lot - instead of taking the first picked (which seems to you to be random), it would have to check every single instance measuring its distance. If you have 100 object instances, that's 100 times as much work, and it affects every single condition!

You can add a for-each loop yourself to test each individual instance's distance. I'll see if I can add a 'pick closest' for the next build.[/QUOTE]

Damn iterations!! An idea I might use is to have some kind of list/array of all specified objects and an associated distance to whatever target then use min(a,b,c...) to find the the smallest distance or max(a,b,c...) to find the largest distance or maybe an array/list which sorts itself as objects are added from smallest to highest then you can pick the first for nearest and last for furthest. Each has its pros and cons but there's a few ways to do it whilst trying to keep away from the iterations...

I tend to think of the most complicated way of doing something so there's probably simpler methods.
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Tue Sep 20, 2011 10:32 pm

Yes, there must be simpler way to pick the closest instance! I caertainly need this and there is quite a lot of instances, too. hmm...
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Tue Sep 20, 2011 10:45 pm

Have a collision mask a moderate distance around the player object, if any of the other objects overlap it do a for loop of those objects only. That means that even if a level has 100 objects, it may only pick 5 of them.

However, if none of the objects overlap that object then a full for loop is probably needed =/
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Tue Sep 20, 2011 10:55 pm

A couple of subevents makes it fairly easy. You don't need an array - just two values for 'closest so far' and 'closest UID'. Local variables would be good for those. The algorithm works like this:

1. Set 'closest so far' to a large number e.g. 999999, and 'closest UID' to -1.
2. For each object:
---a) if the distance to this object is less than 'closest so far':
-----> set 'closest so far' to the distance to this object
-----> set 'closest UID' to this object's UID
3. Pick object with UID = 'closest UID'
---> do something with this instance - it's the closest
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,508

Post » Tue Sep 20, 2011 11:06 pm

Would a loop going from a point out on both sides be quicker than 0 to w/h?
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Wed Sep 21, 2011 4:08 am

I thought about a box slowly expanding from the center and checking if x and y coordinates of other objects are within that box. The downside is if the nearest object is far away. Either way, you have to iterate.
B
35
S
8
G
8
Posts: 532
Reputation: 6,868

Next

Return to Construct 2 General

Who is online

Users browsing this forum: spy84 and 4 guests