Advertisement
corp0

Untitled

Nov 20th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. using Godot;
  2.  
  3. public abstract class SingletonAutoLoad<T> : Node where T : Node
  4. {
  5.     private static T instance;
  6.     public static T Instance => instance;
  7.    
  8.     public override void _Ready()
  9.     {
  10.         if (instance != null)
  11.         {
  12.             GD.PrintErr("SingletonManager: " + typeof(T).Name + " already exists!");
  13.             return;
  14.         }
  15.  
  16.         instance = this as T;
  17.         AfterInit();
  18.     }
  19.  
  20.     public virtual void AfterInit()
  21.     {
  22.     }
  23. }
  24.  
  25. public class ConcreteAutoLoadThing: SingletonAutoLoad<ConcreteAutoLoadThing>
  26. {
  27.     public override void AfterInit()
  28.     {
  29.         // my ready logic without touching the singleton initialization
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement