Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- public partial class Awaiters : Node
- {
- static Awaiters _instance;
- public static Awaiters Instance => _instance;
- static SceneTree Tree => Instance.GetTree();
- public static SignalAwaiter WaitNextFrame() => Tree.ToSignal(Tree, "process_frame");
- public static SignalAwaiter WaitSeconds(float s) => Tree.ToSignal(Tree.CreateTimer(s), "timeout");
- static List<System.Action> _oneshots = new List<System.Action>();
- static List<System.Action> _processingOneshots = new List<System.Action>();
- public Awaiters() : base()
- {
- if (_instance != null)
- {
- GD.PrintErr($"Multiple instances of {typeof(Awaiters).Name} Singletons created!");
- return;
- }
- _instance = this;
- }
- static void _ProcessOneshots()
- {
- var temp = _processingOneshots;
- _processingOneshots = _oneshots;
- _oneshots = temp;
- _oneshots.Clear();
- foreach (var action in _processingOneshots) action?.Invoke();
- }
- public static void AddOneshot(System.Action oneshot)
- {
- _oneshots.Add(oneshot);
- Instance.SetProcess(true);
- }
- public static void CancelOneshot(System.Action oneshot){
- _oneshots.Remove(oneshot);
- }
- public override void _Process(double delta)
- {
- base._Process(delta);
- _ProcessOneshots();
- SetProcess(_oneshots.Count > 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment