godotengine / godotengine/godot-docs

Clarify documentation for C# autoloads

Open
#11,472 1 comment 0 reactions 0 assignees View on GitHub
area:manual enhancement topic:dotnet
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

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:
>
> ```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()`:
>
> ```cs
> 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:
>
> ```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()`:
>
> ```cs
> 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.