C# - The process of creating a static class

Godot Version 4.2.1


using Godot;

namespace Game.Data
    public static partial class ModeTracker
        [Signal] public delegate void ModeChangedEventHandler();
        public enum Mode {None, Menu, Play, Pause, Selection};

        private static Mode currentMode = Mode.Menu;

I want to create a class that contains the current game mode. I thought about creating a static class, but wondered at what point. I assume that when I try to call from another class, for example to bind the ModeChanged signal to the player, then this will definitely happen, because the static class will be created before the player class is created. I don’t quite understand how this works and I noticed that I can’t inherit from a class to get a static class. What do you think is best?

1. Create a non-static class and inherit from RefCounted and create a new instance when the game starts.

2. Create a static class like in the example above.

3. Create a non-static class and inherit from RefCounted and instead of creating an instance, add it to Autoload

you probably don’t need a static class. just go with a normal class and store its instance in a static member of the class. then access the static member as needed. this example is not exactly what you are asking for but shows how to use a static member.

using Godot;
using System;

// MyTest node
public partial class MyTest:Node2D {

	public MyTest(string a_name) {

		Name = a_name;

		// access static member s_instance of MyStaticParent.
		// make this a child of MyStaticParent

	public override void _Ready() {

		// print name of parent and child
		string v_parent_name = GetParent<Node2D>().Name;
		GD.Print("parent node:" , v_parent_name, " child node:", Name);

// MyStaticParent node
public partial class MyStaticParent:Node2D {

	// static member to store instance of MyStaticParent
	public static MyStaticParent s_instance = null;

	// MyStaticParent constructor, always called before _Ready
 	public MyStaticParent() {

		// s_instance is a static member of MyStaticParent, set s_instance to this instance of MyStaticParent
		s_instance = this;

	public override void _Ready() {

		// create a couple MyTest nodes
		MyTest v_test1 = new MyTest("First");
		MyTest v_test2 = new MyTest("Second");

If you are using Autoload then there is no need to instantiate a new version. The Autoload feature will do that for you.

Just create a class like normal and make all your ‘stuff’ in it (variables and methods) static.

Thats how I do my fake singletons.

You can of course create true singletons in c# and Godot will not know they exist, and that will still work. However I don’t know if that has implications for Garbage Collection or not.