Howto "Find path to" random instance of an object?

0 favourites
  • 13 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • How to "Find path to" random instance of an object?

    well I cannot hotlink images... so hope someone understand what I mean

    I got an enemy with the Pathfining behavior, I want him to be able to move towards different instances of the same object...

    hope someone can help me...

    On the event sheet it looks like this:

    System every 2.0 seconds ---> Enemy Find path to (Destination.X, Destination.Y)

    On the layout I have many instances of "Destination" so I'd like that every 2 seconds the Enemy find path to a different instance of Destination.

    I know am total noob, please help me

  • Hey. You just need to select what instance for the event to pick so if you do something like:

    system > pick a random Destination instance ---> Enemy Find path to (Destination.X, Destination.Y)

    it'll work.

    Complimentary capx showing how deceitfully easy it actually is.

    As I'm here can I ask if you managed to make an object find a path to a different instance? I want a pig to go to another random pig for mating purposes.

  • PErhaps your making the calls too fast ?

    "is calculating path"

    "on path found"

    etc

    I'm not seeing an interupt path or break of path find or stop follow path.

    Perhaps make sure the actions are completed first, before you call a new.

  • I think the same kind of solution applies here

    topic

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It worked, thank you!!! <img src="smileys/smiley1.gif" border="0" align="middle" />

  • Guys I got another question, this a bit more complex

    If Enemy1 choose a random Destination but that destination is already "occupied" by other enemies I'd like the Enemy1 to choose another Destination.

    How can I tell to Enemy1 that the destination has randomly chosen is occupied by another enemy?

  • You can store the occupied destinations in a array (by storing the destination instance ID or an actual XY position) then when you want to send an enemy to a destination, you loop through the destination instances and check for each if that destination is already in the array or not. If it is, skip it, else, add it to the array and send the enemy there. This should work until each destination has been assigned an enemy, then it's to you to see what you want to do from here

  • A simpler approach

    add a invisible sprite the same size as your unit.

    Position it at the x,y whre you intend to travel too.

    check if the object is NOT overlapping, and execute pathfinding.

    No array needed.

  • yeah but your approach only works if the enemy has already reached its destination. So if all the enemies are assigned targets at the same time, which is likely, it won't work. Plus, it's a matter of taste, but I'd rather have an array than managing extra dummy sprites and having to clean up garbage

  • Very true :)

    Yours is more sophisticated and offers options for manipulation and saving state.

  • You can store the occupied destinations in a array (by storing the destination instance ID or an actual XY position) then when you want to send an enemy to a destination, you loop through the destination instances and check for each if that destination is already in the array or not. If it is, skip it, else, add it to the array and send the enemy there. This should work until each destination has been assigned an enemy, then it's to you to see what you want to do from here

    Hi, I have never used arrays. I'am gonna try to understand how to use them following your solution. Thank you

  • Here's something to try, I haven't tested it though. Unless it is absolutely necessary for you to store the location of every enemy for other reasons, this might be a simpler solution.

    Pick a random destination, and use Pick Overlapping Point to check if any enemies are located there. Then do a value comparison: if Enemy.PickedCount is equal to zero. If this is true, then the enemy can travel to the destination.

    I would not use this in a while loop. When you are ready for an enemy to travel, you could check a for a free destination once each tick, or even check 3-5 times per tick with a for loop.

    I just thought of something, if Pick Overlapping Point does not pick any enemies, I don't know if Enemy.PickedCount will equal zero, since maybe you can't reference Enemy without any actually picked. If my first suggestion doesn't work, try this: do not check if Enemy.PickedCount is equal to zero. Create a local variable, (could be called PickedCount if you want). After using Pick Overlapping Point, use a For Each loop for Enemy. In the action, Add 1 to your local variable PickedCount. In a subevent, do a value comparison: if PickedCount is equal to zero.

    I hope this helps!

  • Hi, I've been away for a while. Anyway thx for your reply. You guys here are very helpful, I like this community.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)