Valks-Games/sankari

Cleaning up code for timers

valkyrienyanko opened this issue · 1 comments

Consider the following snippet of code. Maybe there should be wrapper classes to simplify all this. The use cases for these are not specific to setting a delay bool.

if (Delay)
    return;

Delay = true;

// method 1
// pros: one line of code
// cons: can't loop the timer
GetTree().CreateTimer(1).Timeout += () => Delay = false;

// method 2
// pros: can loop timer, can refer to timer later with methods like Start() Stop()
// cons: messy
// GTimer.cs is a thing but one could argue it needs refactoring
var timer = new Godot.Timer();
timer.OneShot = true;
timer.Autostart = true;
timer.WaitTime = 1;
timer.Timeout += () => Delay = false;
AddChild(timer);

// method 3
// pros: convenience method for animation tweeners specific to animation
// cons: doesn't have stuff like IsActive()
TweenDelay = GetTree().CreateTween();
TweenDelay.TweenCallback(Callable.From(() => Delay = false)).SetDelay(1);

Looks fine to me now