Skip to content

Clarify documentation for C# autoloads #11472

@sockeye-d

Description

@sockeye-d

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 -= 10

The 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 Instance and 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 -= 10

The 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 Instance and 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:manualIssues and PRs related to the Manual/Tutorials section of the documentationenhancementtopic:dotnet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions