-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Your Godot version:
4.6.dev3
Issue description:
The documentation for autoloads in C# reads quite badly:
If the Enable column is checked (which is the default), then the singleton can be accessed directly in GDScript:
PlayerVariables.health -= 10The Enable column has no effect in C# code. However, if the singleton is a C# script, a similar effect can be achieved by including a static property called
Instanceand assigning it in_Ready():public partial class PlayerVariables : Node { public static PlayerVariables Instance { get; private set; } public int Health { get; set; } public override void _Ready() { Instance = this; } }
The enable toggle has nothing to do with accessing the instance in C# in a type-safe manner.
I think this should be something more like this:
If the Enable column is checked (which is the default), then the singleton can be accessed directly in GDScript:
PlayerVariables.health -= 10The Enable column has no effect in C# code. Unlike GDScript, C# doesn't have a built-in way to get autoloaded nodes by name, but a similar effect can be achieved by including a static property called
Instanceand assigning it in_Ready():public partial class PlayerVariables : Node { public static PlayerVariables Instance { get; private set; } public int Health { get; set; } public override void _Ready() { Instance = this; } }
URL to the documentation page:
https://docs.godotengine.org/en/latest/tutorials/scripting/singletons_autoload.html