Skip to content

Instantly share code, notes, and snippets.

@Lerg
Created February 2, 2024 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lerg/3aafe34311a1b7b97b08cddfe18a53c5 to your computer and use it in GitHub Desktop.
Save Lerg/3aafe34311a1b7b97b08cddfe18a53c5 to your computer and use it in GitHub Desktop.
Timer with a lambda function execution on timeout for Godot 3
using Godot;
using System;
/*
var timer = new LambdaTimer(() => GD.Print("hello timer!"));
AddChild(timer);
*/
public class LambdaTimer : Timer {
private Action onTimeOutAction { get; }
private int count;
public LambdaTimer() {}
public LambdaTimer(Node parent, float duration = 1f, int count = 1, Action action = null, bool auto_start = true) {
onTimeOutAction = action;
this.count = count;
Autostart = auto_start;
OneShot = count <= 1;
WaitTime = duration;
Connect("timeout", this, nameof(OnTimeOut));
parent.AddChild(this);
}
private void OnTimeOut() {
onTimeOutAction?.Invoke();
--count;
if (!OneShot && count == 0) {
Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment