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
1.move to enemy
2.turn to face the enemy
3.call attack function
4.wait cooldown seconds
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?