How to delay actions consistently?

Godot Version

Godot 4.0.

Problem

Hello! I am making a “card” game, where cards have the ability to play other cards.

My problem is that everything is happening instantly and I want to add a small delay after a card is played. Cards are played by a function called “play_card(pos_in_deck: int)”. When a card is played, another function called “check_effects(card_info: Dictionary)” checks if it can play another card, if it can, it calls “play_card(next_pos)” again.

I have tried using “await get_tree().create_timer(playing_speed).timeout”, and it works, but only sometimes. If a card that can play another card is played, and that card plays a card that has the ability to play another card. When the “played by a card” card is played it activates the other card instantly, without any delay.

Question

How can I delay parts of my code consistently?

Depends on when you are using awaits, maybe you need more than timeouts. Give cards a signal like “animation_finished” that other cards, or other turns can await

animation_finished as mentioned.

Another option would be to see if you could use a tween and use a callback once the tween finishes.

A lot of it will depend on use case and setup.

“animation_finished” is a good idea, but my “cards” aren’t actual cards right now, they are just groups of dictionaries in “card” nodes, that are only used to store information currently, but I do also call the cards, but only when that card has finished everything it can do, which means it will call other cards before the card itself finishes playing.

I appreciate the suggestion, but I can’t use animations in my game currently.

Not sure I understand the limitation.

Eitherway why can’t you await before the “Played by a card” effect? Any function that has await can be awaited itself, maybe that’s the key here?

class_name Card extends Node2D

@export var play_next: Card

func play_card():
    print("playing my card")
    await get_tree().create_timer(playing_speed).timeout # waiting before

    if play_next:
        print("Playing next on play")
        await play_next.play_card() # waiting for finish
   
    print("resolved all card effects")
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.