How do i create a reference of the timer node?

Godot Version

3.5

Question

I’m new at godot and i’m using c# for coding. Just recently following its step-by-step guide, I stumbled upon the topic “Connect signal via code” where they teach how you can manually connect a button and its function to the sprite or any other node.

The problem starts here, as i’m coding on vscode it shows red sqiggly lines under “timer” which details out as “it is an ambiguous reference between godot. Timer and system.timers.timer” but using either of them isn’t helping either.

So my question here is, how do i define it

example #1

using Godot;
using System;
public partial class MyCsharpTimer:Timer {
	int m_old_time = 0;
	public override void _Ready() {
		OneShot = true;
		Start(5f);
	}
	public override void _Process(double delta) {
		int v_old_time = Mathf.CeilToInt(TimeLeft);
		if(v_old_time != m_old_time)
			GD.Print(v_old_time);
			m_old_time = v_old_time;
	}
}
public partial class Csharptimer:Node2D {
	public override void _Ready() {
		MyCsharpTimer v_timer = new MyCsharpTimer();
		AddChild(v_timer);
	}
}

example #2 with signa;ls

using Godot;
using System;
public partial class MyTimer:Timer {

	public Action m_timeout_func = null;

	void DoTimeout() {
		GD.Print("normal function");


		Disconnect(Timer.SignalName.Timeout, Callable.From(DoTimeout)); // and how to disconnect the signal
	}

	public override void _Ready() {

		Action m_variable_timeout_function = () => {
			GD.Print("function stored as a variable");
		};

		Start(2); // timer triggers every 2 secs

		// connect timeout signals

		Connect(Timer.SignalName.Timeout, Callable.From(DoTimeout)); // void DoTimeout() {}

		Connect(Timer.SignalName.Timeout, Callable.From(m_variable_timeout_function)); // Action m_timeout_func = () => {}

		Connect(Timer.SignalName.Timeout, Callable.From(() => {
			GD.Print("anonymous function");
		}));

	}
}

docs

Thanks ,but my question is only about this line of code :

public override void _Ready()
{
var timer = GetNode(<)Timer(>)(“Timer”);
}

How do i define “Timer” (the middle one) in c# ? Because everytime i write this code, red squiggly lines appear underneath “Timer” saying "it has ambiguious meaning between godot.timer and system.timers.timer. How do i solve that?

first you can is remove using System; when you don’t needed. If is not working or not possible, use full name with namespace aka Godot.Timer

1 Like

You were right, i used godot.timer and it helped. But now another problem arises. It do not recognize the timeout function. I have written in capital and small but nothing worked

I have edited your original post. Please in the future when posting, replace the full lines saying “<!-- replace this line -->”, including the <!-- and --> parts. Those are HTML comments, so anything between that is not visible.

use unique names now you have var Timer, Godot Timer, and System Timer…

The timer problem is solved, we cool.
But now, i am not able to pick timeout function with it.

It says i’m missing a namspace or something. but then how can it pass the timer node but not its function.

Is there any namespace or derivatives that you know of, i could possibly be missing here in order to call the timeout function in c# ??

Does the method you are trying to connect exist?
there some working code:

	public override void _Ready()
	{
		var timer = new Timer();
		AddChild(timer);
		timer.Start(3f);
		timer.Timeout += MyMethod;

	}
	private void MyMethod()
	{
		GD.Print("Test");
	}

Sorry , i meant signal. I can connect with the Timer node but i can’t connect it to its signal via code in c#.

Rest of the things i was talking about earlier is what is happening to the signal.
Sorry, again

in My code:
timer is variable where i put Timer class object (in my code i created new timer but you can use GetNode like you did)

timer.Timeout - Signal
+= Connect
MyMethod My method, name is depends on your naming convention

in custom Signal you needed disconnect to with -=

Btw which version of godot are you using?

in first post you was trying connect with += i don’t have much experience with 3.X.

try something like this,

	public override void _Ready()
	{

		var timer = new Timer();
		AddChild(timer);
		timer.Start(3f);
		timer.Connect(Timer.SignalName.Timeout,Callable.From(MyMethod));

	}
	public void MyMethod()
	{
		GD.Print("Test");
	}

@zakeflame100
I found some short tutorial with 3.4.5

source:

1 Like

Thanks, i’ll try it and see if it works.

1 Like

Sorry dude for the late reply. It really was what i wanted thanks for your help .

2 Likes