tricky "wait"

Get help using Construct 2

Post » Mon Nov 04, 2013 7:33 pm

Hello all.

I just wanted to share a reather inpleasant behaviour I encounter when I use wait in a particulat fashion and would like to ask for your opinion on how to apraoch such situations.

I am using a relatively straightforward aproach as I am trying to create AI in an RTS type of game. There are a few tasks an npc can do, and it chooses what to do based on immediate environmental observations. If everything seems fine, for example, and there is a food source nearby npc gathers food. This happens as follows:

1.move to food source (by pathfinding)
2.turn to face the source
3.wait 2 seconds
4.create a piece of food sprite over npc and pin to it.
5.reduce food value remaining in food source
6.find path to home.

now the same npc attacks an enemy npc if they come close. below is how it works

0.detect enemy
1.move to enemy
2.turn to face the enemy
3.call attack function
4.wait cooldown seconds
5.repeat

Now the problem happens when the food is detected and the npc moved to food source, but also an enemy came nearby before the chopping of food, thus attack protocol is initiated. Even though the npc leaves the food source, runs to enemy and attacks it, still a piece of food is created on top of it and pinned and still the food source is reduced. Because it was there waiting for the 2 seconds to pass, but enemy came before chopping of the food and thus npc departed, but the code below the "wait" still was waiting in line to happen. And they happen, even if the npc was teleported to other side of the layout when the "wait" is over.

I have a few ideas on how to fix this but I am hoping there is a much more easier way... Could someone with experience, especially in "wait" help on this?
B
18
S
4
G
1
Posts: 332
Reputation: 3,149

Post » Mon Nov 04, 2013 7:46 pm

easiest fix would be to use timer behaviour instead of wait and add an extra condition to on timer to check if player is still at the right position to receive the food.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Nov 05, 2013 2:02 am

Yes, wait introduces so many problems that is hardly feasible to use anywhere in a script except when doing something extremely linear that isn't influenced by anything. Better get used to use timers.mindfaQ2013-11-05 02:03:33
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Post » Tue Nov 05, 2013 11:02 am

Yes, that is what I feared it would lead to. I already tried to introduce a conditional check after the wait via instance variables... The problem is, if you chech for a condition in a higher event, and check for the same condition in a sub event for it, the check fails. I will try to simulate this behaviour in an example.

But, I can't imagine how timer behaviour will be different in that regard, because after all, it is the trigger that solves the problem, not the behaviour itself. Could you elaborate some on this? Or maybe the smallest bit of example could be very welcome.

Thanks;
-Wind.
B
18
S
4
G
1
Posts: 332
Reputation: 3,149

Post » Tue Nov 05, 2013 11:19 am

Something like this would probably work:

1.move to food source (by pathfinding)
2.turn to face the source
3.set boolean variable "WaitingForFood" to true
4.Set timer "FoodWait" 2 seconds

1 On timer "FoodWait" if boolean "WaitingForFood" is true
4.create a piece of food sprite over npc and pin to it.
5.reduce food value remaining in food source
6.find path to home.

0.if timer "cooldown" = 0 - detect enemy
1.move to enemy and set Boolean "WaitingForFood" to false
2.turn to face the enemy
3.call attack function
4.Set timer "cooldown" for whatever seconds"
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Tue Nov 05, 2013 11:23 am

Only issue left is what you'd like to happen when player has food and is walking home.

Attack while keeping food?
Destroy food on attacking?
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests