Created
February 2, 2024 12:52
-
-
Save Lerg/3aafe34311a1b7b97b08cddfe18a53c5 to your computer and use it in GitHub Desktop.
Timer with a lambda function execution on timeout for Godot 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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