Create a delay between code execution using C#

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KDX^2

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!

await ToSignal(timer, "timeout");

steelx | 2021-03-01 15:10

:bust_in_silhouette: Reply From: Vikrant

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	
	}
}

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.

KDX^2 | 2021-02-26 03:28

consider visiting [this page] [1] 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

Vikrant | 2021-02-27 14:30

:bust_in_silhouette: Reply From: steelx
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 Your first game — Godot Engine (3.2) documentation in English

1 Like
:bust_in_silhouette: Reply From: Roadie985

I’m sorry, probably late to the party :smiley:

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”);
}
}


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

Just make the containing function async as well.

Rainsong | 2022-04-07 00:25

:bust_in_silhouette: Reply From: SalusGames

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!");
}
2 Likes