Triggers in loops

Get help using Construct 2

Post » Mon Jul 22, 2013 6:22 am

I have this basic scenario for my game:

For Each Enemy
-- Find Path
-> On path found | Move on path

"On path found" is, of course, a trigger.
Can someone explain to me why a trigger doesn't work inside a loop like this?

I'm finding I have to be very careful with my For Each usage (and sometimes finding it's even redundant...). So if I could get some clarification, that would be wonderful. Thanks!
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
132
S
22
G
8
Posts: 770
Reputation: 14,596

Post » Mon Jul 22, 2013 6:53 am

A trigger happens sometime in the future, you don't know when. It could be in a tick, it could be in X seconds. Since you are calling Find path, then immediately checking the trigger condition, the chances of the trigger being true within that time are pretty much zero. You need to move your trigger outside of the For loop, so that when it occurs sometime later, it can be processed and your Move on path called.blackhornet2013-07-22 07:05:14
ImageImageImage
B
71
S
21
G
211
Posts: 3,614
Reputation: 119,147

Post » Mon Jul 22, 2013 9:20 am

When a trigger fires, it must check all its parent events are true before it can fire, so triggers in subevents work properly.

If 'For each enemy' is a parent event, when firing 'On path found' it has to check if 'For each enemy' is true or not. It doesn't really make any sense to ask this, because it's a loop - a flow control condition, not a true/false condition. So I think in this case the engine simply returns false and doesn't run the trigger.

Why would you put a trigger in a subevent to a loop anyway? What are you hoping it would do differently to being a top-level event?

You may even not need the 'for each' for the 'find path' action, since the engine has a built-in for-each to run actions on every instance that meets the event's conditions. You only need an explicit 'for-each' condition when what the engine does automatically isn't enough.Ashley2013-07-22 09:21:39
Scirra Founder
B
386
S
229
G
87
Posts: 24,207
Reputation: 191,582

Post » Mon Jul 22, 2013 7:03 pm

@blackhornet - Aye. In my learning and testing, I've found that to be more so the case than not. In trying to isolate a larger problem I'm facing in developing my enemy behavior mechanics, I'm presently attempting to isolate and identify where things work and do not. At one point, I did have the "On path found | Move along path" event set outside the whole series of events and loops that controlled enemy movement. I switched it inside the loop(s) just to test some things...

@Ashley - Thank you, sir! :) Someone @newt shared with me a short tutorial on how For Each loops work, so now I'm trying to better implement (or remove) the use of them. My question here is simply trying to better understand the limitations of the functions that the loops have. Kinda like asking, "The rocket fires up...that's its function. But what's its limitation? How FAR UP can it go?" Hopefully that makes sense.

Your point that For Each has no true/false factor is one such limitation (or quality, I guess) that makes things much clearer for future reference.

Ultimately, what I wanted to ensure happen was that ONLY each instance whose path had been found would actually move along the path. This was before I fully/better understood how For Each works. I had previous implementations of my enemy behavior that had multiple instances moving when maybe only one or two should have done anything. That led me to thinking I needed For Each regularly...apparently not.

...Hmm...would it help if I shared my capx?
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
132
S
22
G
8
Posts: 770
Reputation: 14,596

Post » Mon Jul 22, 2013 7:55 pm

A CAPX is always the fastest way to get help. Just be clear what you want.
ImageImageImage
B
71
S
21
G
211
Posts: 3,614
Reputation: 119,147

Post » Mon Jul 22, 2013 9:51 pm

@blackhornet - LOL Trying to be, truly. But as you can see, I can already say a lot when I'm trying to be brief. Anyway, here's the capx.
https://www.sugarsync.com/pf/D6025908_4317202_6840595

Look for event sheet "ES CharEnemy", starting particularly at line 17, though lines 13-16 also factor into things.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
132
S
22
G
8
Posts: 770
Reputation: 14,596

Post » Mon Jul 22, 2013 10:19 pm

Sorry, it's still not clear how your game works, and what is broken. I'm also not seeing the Pathfinding mechanism there, that you started this thread with.
ImageImageImage
B
71
S
21
G
211
Posts: 3,614
Reputation: 119,147

Post » Tue Jul 23, 2013 4:32 am

@blackhornet - Hmmm...
Well, a friend helped me identify one problem. I had quotation marks in the Properties field for one of my variables. So the editor was literally looking for ""String""... Heh. I'm going to look things over from there and see what I can resolve from there.

But the short explanation goes like this:
PATROL: Find path between designated PatrolNodes.
CHASE: Find path to last known location of Player.
ALERT: Find path within a radius from last known location of Player, search randomly for a brief time.
RESUME: After some time has passed, if Player is not found, return to nearest PatrolNode and resume PATROL status.

That's basically what's supposed to happen. Does that help at all?
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
132
S
22
G
8
Posts: 770
Reputation: 14,596

Post » Tue Jul 23, 2013 7:56 am

I didn't get that far, but there is some pathfinding working.

OpPASS v1-1-4_BHT_PF.capx

I did have to change the PF Cell Size, as it was too big for your narrow paths.blackhornet2013-07-23 07:57:01
ImageImageImage
B
71
S
21
G
211
Posts: 3,614
Reputation: 119,147

Post » Wed Jul 24, 2013 7:23 am

@blackhornet - Dude. Stunningly amazing! I'm still looking it over, but I think I've kinda got the gist of what you've got going on. But I'm confused on one part.
Line 39...

Pick Node by evaluating NodePatrol.UID <> Enemy.CurrentNP

What in the world is the " <> " bit? LOL I've never seen that before.
I'm seeking Narnia. Who wants to come with me! Aslan is on the move!
B
132
S
22
G
8
Posts: 770
Reputation: 14,596

Next

Return to How do I....?

Who is online

Users browsing this forum: Christmas, marc254 and 3 guests