tricky "wait"

0 favourites
  • 6 posts
From the Asset Store
Tricky Brick - Addictive classic game - Full game to post google store.
  • 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?

  • 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.

  • 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.

  • 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.

  • 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"

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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?

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