# Thread: Is it possible to select the nearest active object?

1. ## Is it possible to select the nearest active object?

First of all I am aware of an extension along the lines of advanced direction object. However, for two reasons this is not a path I want to take.

1. I have already fully coded some of the objects I wish to use this function for, as active objects. To change them would mean having to completely code them again from the ground up.
2. People told me that I needed to download advanced movement extensions to make accurate collisions but I was able to code them myself with a bit of time. The latter was far more rewarding and my last game truly felt like something I created because of it.

So, the question remains: How do you create a system to select the nearest object of a type to the active object you want to identify?

To be more specific, here's an example of what I'm trying to do.

An enemy is disarmed, and it's looking to pick up a weapon on the ground. There are two swords: Sword A and Sword B — Both are an instance of the same thing. One is farther away, one is closer.
What would you do to make the enemy's AI recognize the closer weapon as the most viable option, such that in the same scenario should Sword A be closer than Sword B, the enemy would go for Sword A; and vice versa, with potentially many more swords.

If this is complicated, I'm willing to learn. I'd rather understand what I'm doing than wing it off of other people's creations, with no disrespect to their skill or talent.

2. ## Is it possible to select the nearest active object?

Personally I would group all items in a qualifier and have an alterable value "distance" which could represent the distance from the AI to the item. I would then have an AI state which would call upon checking distance values for objects within a set range of itself (so it doesn't have to check all of the items, say 500 pixel radius). I would use the distance formula to compute the distance between two objects. I would probably use fast loops to compute these numbers when called for so its not running all the time. Ideally it would compute maybe 2 or 3 distances for items nearby and it would then have options on which item to go to, just in case all the items are of differing qualities. This also brings into the problem of path finding if there are obstacles in the way between the AI and the items.

That's my first thought process. I feel like there's an easier way of just finding the closest item if that's what you want. I think there was a thread on here recently that asked a similar question about finding the closest object. You should check out how they solved that. Good luck!

3. How would you "get the AI state to check for objects within a set range of itself"?

4. ## Is it possible to select the nearest active object?

On second thought, I wonder if you use the distance formula with the qualifier's X and Y position, if it would only select the items that meet the condition.

For example if you use a "compare two general values" condition and compare the distance formula using the qualifier coordinates which is less than 500 pixels, maybe it would include only those objects. Object selection in Fusion can be a pain sometimes, but if we can simplify it that would be for the best.

5. Jeez, it's very discouraging. With any other engine it's just a matter of adding numbers together and calculating the lowest of the two. It seems that you're very limited to the actual objects themselves, and Fusion decides what you can and cannot calculate. It's all relatively limited to "Is [value] greater/equal/lower than [value]" without making dozens of events. -___- I don't want to give up on this project but if that's the way it is then I cannot complete my idea. I don't know how to approach it. I'm stumped.

6. Did you take a look at this thread? It poses a similar question of finding the nearest object.

7. http://www.xable.us/closest-neighbors.php

Get into the habit of downloading and deconstructing .mfas. It's one of the best ways to learn if you really apply yourself to understand what is going on.

useful example/tutorial sites that might help with future problems:

http://www.diybandits.com.au/MMF/tutorials.html (make sure you look at the articles section too)
http://www.create-games.com/article.asp?id=1898 (he has many other good downloads and some articles on his profile)
https://onedrive.live.com/?authkey=!...E7EE094271BBDA
http://www.xable.us/tutorials.php
http://www.castles-of-britain.com/mmf2examples.htm

8. I made for you an example I hope it will be useful for you

https://www.dropbox.com/s/6nibx93ti1...tance.mfa?dl=0

9. Thanks a lot for taking the time to make that demonstration ColdFire. I've been thoroughly examining it before even touching it with my code and it's all starting to become a lot more bearable.

I have a question: How does comparing the "id" of each Active NPC to the loop index affect the code? I'm not really sure what that part does, but I'd love to know!

10. ## Is it possible to select the nearest active object?

I haven't looked at the file, but I'm assuming he's comparing the ID number to the loop index so on each loop it's checking a separate item.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•