Hello
I’m wondering how I can make sure that the enemy doesn’t just swing every time he’s within attacking range, but maybe swings at a more random interval. For instance he swings first, then waits 2 seconds, then swings again, then waits 1 second, then swings again, then wait 3 seconds and swing twice.
i tired use timer but It didn't work!!
How would we do this? Thank you!

    4 days later

    adam_40 I write my timers in the script:

    var timer : float = 1.0
    var timemax : float = 1.0
    func physics_process(delta):
         if timer < timemax:
              timer += delta

    When attack happens:

    if timer >= timemax:
         #Attack
         timer = 0.0 #reset

    For randomness, set timemax to a random value after the attack:

    if timer >= timemax:
         #Attack
         timemax = randf_range(1.0, 4.0) #random number between 1 and 4
         timer = 0.0

    adam_40 I'm doing a game with combat and I'm using a token system:

    • There is a "Combat Director" that has an array with "attack tokens": Every time an enemy wants to attack the player they request a token to the Combat Director and they only attack if the Director provides a token for them.
    • If there are available tokens, the Combat Director gives one to the enemy, deleting them from the array and the enemy performs the attack. if an enemy requests a token and there are no more tokens available, they won't attack, but they can keep requesting tokens until one becomes available again.
    • When the enemy finishes the attack, it gives the token back, sets the on_cooldown variable to true and starts a cooldown timer. If the enemy tries to attack again, we check on_cooldown to see if they can actually attack or we must ignore the request. Once the timer triggers, _on_cooldown is set to false and the enemy is free to attack again. This system allows for the enemies to "take turns", instead of the same one getting the token and never letting it go.
    • The Combat Director can check for other extra conditions to accept an attack request (note: it's very useful for the Combat Director to have an array with references to all enemies, as well as a reference to the player to do this kind of stuff). I usually don't use any extra conditions, but this may become useful if you add enemies with special attacks or logic.
    • The number of tokens is fixed, but it can change between different levels or difficulties. For example, in level 1 you can have only 1 token so only 1 enemy will be able to attack at a time, but in level 10 you can have 2 tokens and then 2 enemies will be able to attack at the same time.
    • I also have an Area3D in every enemy that detects if the player gets very close; In that case the enemy can do a "forced proximity attack" that doesn't require a token. This way the player can't trick the system by running away from the attacking enemy and hitting those that are waiting for a token.
    • You also have to consider other situations for the enemies to release the token: For example if an enemy dies before they can complete the attack they will return the token to the Combat Director.
    • Conversely, you can add other situations for enemies to request an attack, like if the player attacks them as it looks very strange to attack an enemy and not have it fight back. Or maybe your enemies can enter an "enraged" mode and attack without requesting a token. You can also have high priority enemies that can steal the token from other enemies and attack as they need it, like bosses.
    • There can also be different types of tokens, like one token for melee attacks and other for ranged attacks.
    • This provides some new opportunities for combat: For example I have given the player a ranged attack that doesn't deal any damage, but it forces the enemy to block, return the token and start the cooldown timer. This can be used to stop enemies on their tracks: If I'm attacking one enemy and I see that another one is approaching to attack, I quickly shoot at it and disable it for the cooldown duration. This way the player has a tool to handle mobs.
    • You can also do stuff like a special AoE attack that removes all tokens and puts all enemies on cooldown. For example, in my game you can execute enemies. When you do so, all close enemies flinch, return their token if they have one and are put on cooldown. This gives the player the chance to take the initiative once the execution finishes or escape is he was being surrounded.
    • Using more than 1 token will make the enemies very active and make the combat very fast paced, specially if you also use forced proximity attacks, that's why you need to give the player tools to control the enemy aggressiveness like the ranged attack or the AoE, but it depends on your combat system's intricacies: If your enemies are slow and they can only attack in very close range then the combat will feel completely different.
    • It also makes a huge difference WHEN the enemies return the token: If their attacks consist of 3 hit combos and they return the token after the first hit of the combo then it allows other enemies to start attacking much sooner and it becomes a very active combat system. But if instead they wait until the final attack of the combo, then the player can focus more on the attacking enemy without begin overwhelmed by others. You can use this to make your enemies more complex, like having attacks that the player needs to block with different timings...but if you allow enemies to pile up and attack while a previous combo is still going, then you need to make them more simple.

    Hope this helps 🙂

    just my 2cents and I'm sure someone more knowledgeable would verify this. you could make an array with series of timers you want the enemy to wait to attack. Then use the "randi % ...." or ".pick_random" to choose a random timer to run and send a "can attack" bool back to the enemy and repeat. which you could use smaller times as the level/enemy progresses. Just a thought...

      REVBENT It all depends on the game.
      My answer was more for a realtime game, where the enemy would wait 1 second to attack, then wait 2 seconds, etc.
      and @correojon 's answer was for a turn based game.

        Jesusemora Not at all, my answer is for a real-time combat system. The token system is not something I have come up with, it's something I read other action games used (I don't remember if it was OG God of War or Devil May Cry). All action games need a system like this to prioritize enemy actions, otherwise all of them would attack the player at the same time and it would be impossible, or you would have to put this prioritization code into the enemies themselves, which would be incredibly hard to manage. It's not something easy to notice, but watch a gameplay video of any action game like Ninja Gaiden, DMC, GoW or Bayonetta and you'll see that at any given time there are only 1 or 2 enemies actually doing something and attacking the player, the rest are just standing on the background playing some idle animation or circling around the player without interfering in the fight. In Assassin's Creed it's incredibly obvious how each enemy stands in a circle waiting for their turn to attack.

          correojon dont forget about TES oblivion.

          The token system sounds good but what about the horde type games like hell divers 2, like deep rock galactic, where hordes attack, multiple enemies attack same player? Maybe they use token system that is not that obvious, idk.

            kuligs2 In horde like games individual enemies are very slow and weak by themselves. They spend most of the time moving towards the player and the player has plenty of time to deal with them before they reach him. The challenge is different from something like DMC, where instead you interact with the enemy when it reaches your position (or when you reach their position) and those interactions are much more complex, with the player having many moves at his disposal, or the enemy having many more actions and attacks they can do, some of which require precise timing and specific responses. In a horde game, when the enemy reach you, they swarm you with the only move they have and you've lost, you're not supposed to be able to kill them at that point. You can do this in action games as well, for example Doom 2016 has cannon fodder enemies that pose no risk and are really disguised ammo and health crates. OG God of War uses fodder enemies like this a lot to make the player feel more powerful by easily dispatching many enemies; You can kill them in a couple of hits and your attacks will interrupt their actions, so it doesn't really matter if they all attack at once as you can kill them all with the same attack.

            However, special units DO use a token system: In horde games there are big units with special characteristics that appear in much lower numbers. These can use a priority system (being it token-based or not) so that not all of them attack at the same time. In multiplayer games, maybe you can have 3 or 4 tokens that allow that many units to attack simultaneously, but a single player is not supposed to be able to deal with all these attacks at once. These attacks are often very powerful, with big windups and require the player to react to them, so in a way they're similar to the simpler enemies in pure single-player action games. Doom 2016 has special tokens so that Imps have their own pool of many tokens and that way they can attack at the same time, but heavier units have a separate pool with just 1 token to share so you don't get 2 Cyberdemons shooting missiles at the same time. Then in higher difficulties you can ramp up the number of tokens in each pool and unleash hell on the player.