The Timer node is not working!

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

I’m trying to use Timers in Godot 4.0 with C# and functions like WaitTime or start aren’t working. Here is the code and the errors I’m getting.

using Godot;
using System;

public partial class Timer : Sprite2D
{
	PackedScene hitterVirus;
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		Timer timer = this.GetNode<Timer>("Clock");
		timer.WaitTime = 1;
		timer.Connect("timeout", this, "on_timeout");
		timer.Start();

		hitterVirus =(PackedScene)GD.Load("res://Scenes.tscn");
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}
	

	void on_timeout()
	{
		GD.Print("you got here");

		Sprite2D hitter = (Sprite2D)hitterVirus.Instantiate();
		this.AddChild(hitter);
	}
}

Errors:

error CS1061: 'Timer' does not contain a definition for 'WaitTime' and no accessible extension method 'WaitTime' accepting a first argument of type 'Timer' could be found (are you missing a using directive or an assembly reference?)

error CS1503: Argument 2: cannot convert from 'Timer' to 'Godot.Callable'

error CS1503: Argument 3: cannot convert from 'string' to 'uint'

error CS1061: 'Timer' does not contain a definition for 'Start' and no accessible extension method 'Start' accepting a first argument of type 'Timer' could be found (are you missing a using directive or an assembly reference?)

Your first problem here is your class is named ‘Timer’; call it something else, this leads to trouble as you’ve spotted - your class doesn’t have a WaitTime (or Start). Alternatively, prefix the godot version with its namespace.

Next, signals now take a Callable rather than a string, which can be expressed idiomatically in C#:

Timer myTimer = GetNode<Timer>("Timer");
myTimer.Timeout += () => GD.Print("Timeout!");  // or your event delegate

C# signals — Godot Engine (stable) documentation in English

Using signals — Godot Engine (stable) documentation in English

spaceyjase | 2023-03-28 16:33