+2 votes

So I am trying to put a delay between lines of code using C#. In GDScript, I can do something like this:

func _ready():
    yield(get_tree().create_timer(1.0), "timeout")

But I can't figure out how to do the same thing using C#. Are there anyway to achieve the same effect using C#? Thanks!

Godot version Godot_v3.2.3-stable_mono_win64
in Engine by (25 points)

await ToSignal(timer, "timeout");

2 Answers

0 votes

consider using this way, not best but enough to do your work

float time = 0;

float timer(float delta, float a){
    return a += 1 * delta;

public override void _Process(float delta){
    time = timer(delta,  time);
    if  (time > 5.0f){
        //do whatever you want to do    
by (188 points)

Are there any function in C# that does exactly like yield(get_tree().create_timer(1.0), "timeout")? Because I just want a delay between specific pieces of code, not an overall timer.

consider visiting this page of godot document, not sure if this what you want

and btw what you want to delay code execution if you can tell me I can further assist you

+1 vote
public async void ShowGameOver()
    // option 1
    var messageTimer = GetNode<Timer>("MessageTimer");
    await ToSignal(messageTimer, "timeout");

     // option 2   
    await ToSignal(GetTree().CreateTimer(1), "timeout");


you can read more here https://docs.godotengine.org/en/3.2/getting_started/step_by_step/your_first_game.html#overview

by (22 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.