How to temporarily disable some global classes (scripts with class_names)?

Godot Version

Godot 4.2.2

Question

Is there a way to disable global classes so that they are not loaded?

I created a test scene for debugging purposes and disabled some autoloads from the project settings. When I try to run the test scene, named classes prevent that because they reference the autoloads that I disabled. What am I missing? Sure there is a way to disable global classes without removing class_names?

I’m not aware of a way to do this. You could stop using class_names and instead import the classes manually where you need them e.g. const Player := preload("res://player.gd").

You could also add checks in the global classes to verify the singleton exists before using it.

Or you could add a “Environment” where you would store whether the game is running properly or in “test_scene_map_generation” or whatever. Then the other singletons could modify their behavior based on that and return dummy values or whatever

2 Likes

Thank you, I think I’ll go with the last approach. I’m kind of surprised that there is no way to control which global classes are loaded but disabling singletons is so simple. Of course I have no idea how they are implemented but I assumed they would work in a similar way.

Singletons are easy to disable because they are just nodes :smile:

When disabled they are not added to the node tree. If you open the “Remote” node tree when your game is running you can see the singletons attached to the tree.