The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+3 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():
    print("Start")
    yield(get_tree().create_timer(1.0), "timeout")
    print("End")

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 (26 points)

await ToSignal(timer, "timeout");

4 Answers

–4 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 (183 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

+3 votes
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 (26 points)
edited by
0 votes

I'm sorry, probably late to the party :D

Looking to do things through program rather than nodes. c#

Made a float variable, set an if statement to check if it's working.

public class maketimer : Node2D
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";

public float myTime = 0.0f;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
    System.Console.WriteLine("confirm script");
    hello();

}

// // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
myTime += delta;
System.Console.WriteLine(myTime.ToString());
if(myTime > 3){
System.Console.WriteLine("tick");
myTime = 0;
}

}
public void hello(){
System.Console.WriteLine("from timer");
}
}


by (14 points)
await ToSignal(GetTree().CreateTimer(0.5f), "timeout");

Just make the containing function async as well.

0 votes

I know this is an old post but this is how I do it

using System;
using System.Threading.Tasks;

public override void _Ready()
{
    DelayMethod();
}

private async DelayMethod()
{
    await Task.Delay(TimeSpan.FromMilliseconds(1000));
    GD.Print("1 second delay!");
}
by (14 points)
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 Frequently asked questions and 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 [email protected] with your username.