How you Find the nearest object to another object?

Get help using Construct 2

Post » Mon Dec 07, 2015 6:33 am

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.
B
5
S
1
Posts: 68
Reputation: 515

Post » Mon Dec 07, 2015 7:00 am

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
B
85
S
27
G
85
Posts: 5,068
Reputation: 57,870

Post » Mon Dec 07, 2015 7:20 am

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?
B
5
S
1
Posts: 68
Reputation: 515

Post » Mon Dec 07, 2015 7:31 am

They do different things so I'm unsure of the comparison.
B
85
S
27
G
85
Posts: 5,068
Reputation: 57,870

Post » Mon Dec 07, 2015 7:38 am

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.
B
5
S
1
Posts: 68
Reputation: 515

Post » Mon Dec 07, 2015 5:56 pm

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
B
85
S
27
G
85
Posts: 5,068
Reputation: 57,870


Return to How do I....?

Who is online

Users browsing this forum: AllanR, omnipaws and 12 guests