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

Get help using Construct 2

Post » Sun Dec 01, 2013 4:41 pm

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
B
5
Posts: 40
Reputation: 307

Post » Sun Dec 01, 2013 9:31 pm

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.
B
10
S
1
Posts: 17
Reputation: 642

Post » Sun Dec 01, 2013 9:40 pm

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.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Mon Dec 02, 2013 1:14 am

I think the same kind of solution applies here

topic
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Mon Dec 02, 2013 11:11 am

It worked, thank you!!!
B
5
Posts: 40
Reputation: 307

Post » Mon Dec 02, 2013 12:24 pm

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?

B
5
Posts: 40
Reputation: 307

Post » Mon Dec 02, 2013 2:31 pm

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
B
12
S
5
G
2
Posts: 132
Reputation: 2,627

Post » Mon Dec 02, 2013 3:44 pm

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.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Mon Dec 02, 2013 5:07 pm

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
B
12
S
5
G
2
Posts: 132
Reputation: 2,627

Post » Mon Dec 02, 2013 5:26 pm

Very true :)

Yours is more sophisticated and offers options for manipulation and saving state.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests