# Finding shortest distance to object

### » Sun Dec 18, 2011 12:02 am

For most of the day, I've been killing myself trying to figure out how to measure the shortest distance between two objects and then moving one of the objects to the other. For example, a man gets hungry. There are several break rooms in his workplace and Construct can't really decide which he should move to, so it ends up picking the one so far away that he starves to death on the way.
B
5
G
1
Posts: 5
Reputation: 835

### » Sun Dec 18, 2011 12:19 am

Does this help?
http://www.scirra.com/forum/pick-closest-object_topic42831.html
B
24
S
9
G
2
Posts: 294
Reputation: 3,160

### » Sun Dec 18, 2011 9:34 am

Almost worked, but if I have several instances of scientists that suddenly get hungry at the same time they all pick the same table. Still, better than nothing. I suppose it'd work pretty well as long as not all the scientists are created at the same time.

Thanks!

Edit:

Actually nevermind, seems it only finds the closest table to the first scientist to spawn.Murkat2011-12-18 10:50:42
B
5
G
1
Posts: 5
Reputation: 835

### » Sun Dec 18, 2011 11:24 am

Well, if you can't find a solution, you could post your .cap and I see what I can do. So I get a better overview of all.zyblade2011-12-18 11:25:11
B
24
S
9
G
2
Posts: 294
Reputation: 3,160

### » Sun Dec 18, 2011 11:52 am

FYI, there is a system expression: Distance(x1, y1, x2, y2)
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

### » Sun Dec 18, 2011 2:52 pm

Murkat, try to reverse your picking.

If you are picking table vs. scientist, with the scientist as the condition... try making the table the condition instead. Get what I mean?

That will probably fix your issue :)

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
45
S
17
G
37
Posts: 2,853
Reputation: 25,966

### » Sun Dec 18, 2011 3:24 pm

Woo, fixed it with a For Each loop. I'm a bit concerned that this might slow the game down in the future, but testing it with somewhere between 50-100 instances seemed to work pretty well, and I doubt I'll need more than that.

Man that felt good.

Edit:

@SoldjahBoy Not quite sure what you mean, but doing it like this seemed to fix the problem.

If there's a better way of doing it(I literally just found out how sub-events work today), I'd very much appreciate the input. I can already tell this method of doing it is less than optimal, but it's the only one that worked, so yeah.Murkat2011-12-18 17:49:39
B
5
G
1
Posts: 5
Reputation: 835