Add Singleton attribute
This would be useful for referencing and setting up singletons. There's seemingly an implementation of it here.
Basically, turn this:
public partial class MyNode : Node {
public static MyNode Singleton { get; private set; }
public MyNode() { // Or _EnterTree or _Ready
Singleton = this;
}
}
Into this:
[Singleton]
public partial class MyNode : Node {
}
Hi JL.
Whilst I wouldn't want to nick the other guy's idea (mwhahaha), we could certainly add something similar to this project if there's a need for it.
So... since Autoloads are the Godot way of doing Singletons, one would currently create a scene (maybe in an Autoloads folder) and add it to the global Autoloads list. Then it is added to the scene tree before the main scene and remains for the lifetime of the project.
We recently injected a static Autoload class to provide strongly typed access (where possible) to declared autoloads. I did think of including an attribute to declare an Autoload in code so one does not need to manually add it to the project settings. Maybe we should aim for something like this? Access would be Autoload.MySingleton rather than MySingleton.Instance, but it would at least provide a convenient way for it to be auto-added to the scene tree, albeit on first use rather than before the main scene.
What do you think?
@Cat-Lips Hi. Personally, I'm not a fan of autoloads, since they require you to structure your project in a "black magic" kind of way. That is, instead of having a scene that contains several script nodes, you have to create a new scene for each script and put them in the autoload manager.
Putting the singletons in a class is an interesting idea. I'm a bit concerned it would reduce intellisense though, since you couldn't click on MySingleton to be taken to the class definition.
Personally, I'm not a fan of autoloads, since they require you to structure your project in a "black magic" kind of way. That is, instead of having a scene that contains several script nodes, you have to create a new scene for each script and put them in the autoload manager.
As far as I'm aware, anything you can do in a normal scene can be done in an autoload. They're just auto-instantiated before the main scene.
Putting the singletons in a class is an interesting idea. I'm a bit concerned it would reduce intellisense though, since you couldn't click on
MySingletonto be taken to the class definition.
Yeah, you'd have to type Autoload. to see the available members.
No reason why I can't do a Singleton attribute and extend the Autoload attribute (separate tasks).
Leave it with me and I'll get to it when I can (unless urgent!) :)