How do I manage enemy turns for a grid based game?

0 favourites
  • 7 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Hi! I checked many post and couldn't get a complete answer, so I would appreciate some help.

    How to I make enemies act in order?

    I'm making a turn based limited dungeon crawler, inspired by games like Hoplite.

    After the player acts, I want for all enemies to act, but not at the same time, they should do it one after another, so they don't end up occupying the same tile or such.

    The best solution so far I managed was to create a timer that multiples a base value for each of their LoopIndex, but I fear that this might bring troubles long term (if I need a new one to take more time to do an attack, for example), I could add a variable of "neededTimeToAttack", bur it feels more like a patch than a solution.

    I'm experimenting with setting each LoopIndex to a variable TurnOrder for each instance, then checking it against a global variable, but all enemies started acting simultaneously again

    Can someone detect the problem in my logic? It goes over my head.

    Cheers!

    Tagged:

  • Remove "Trigger once" condition from all events and never use it with objects that have multiple instances! When used incorrectly this condition will cause nothing but bugs.

    Use Timer behavior to schedule enemy movement. But it's better to run it on some object with only one instance, for example on the Player sprite. So your code can be something like this:

    // on start of enemy turn
    Player start Timer "enemy_move" for 1s once
    
    
    Player on Timer "enemy_move"
    
     // picking the next enemy which hasn't moved
     Enemy compare variable HasMoved=false
     Enemy pick instance with the lowest TurnOrder 
     --> Move this enemy
     --> Enemy set HasMoved to true
     --> Player start Timer "enemy_move" for 1s once
    
     Else 
     // all enemies has moved - end enemy turn
    

    So the timer will be repeated for every enemy until all of them move.

  • I think you can make a function that calls for the handling of enemy AI. At the end of the function it could call itself again on the condition that there are still unresolved enemies waiting.

  • Okay, I get the logic! a circular function that checks a variable of "acted this turn", if there are none left it pass to the player, much more elegant than my solution ha!

    I´ll try it and post pack :)

  • A "for each" will repeat an event for each instance of an object or family, separately. If your movement doesn't happen over time, this can be used to move all the enemies without conflict, as movement for each instance will get resolved individually in order. If movement is animated or takes time, use a timer behavior to trigger subsequent moves as dop described.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • dop2000 it worked wonders! Thank you very much, the only problem I had is that I still need to run "trigger once" or it wont work, I think its constantly checking and resetting the timer. I trust you that this might bring problems long term, so if you have a solution, it would be appreciated, either way, I consider this problem resolved!.

  • I still need to run "trigger once" or it wont work

    It's ok to use it with global variables. Just never use it inside of loops, triggers and with objects that have multiple instances. For example, this code is wrong and will often work incorrectly:

    Enemy health=0
    Trigger once
    ... Enemy play "death" animation
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)