Godot Version
Godot 4.2.1
Question
I am trying to make a change to a label on another Scene when I pick up an object. Code looks like this
Apple.cs
signal_manger.cs
AppleCounter.cs
I create my global like this:
I tried to follow this tutorial but did not work: https://www.youtube.com/watch?v=gZg2qgtPy6U
What is it I don’t understand D:
Any methods or variables in an autoload script need to be static instead, as technically they are singletons, even though they are not ‘true’ ones.
not completely sure what you think should be static
You are using the wrong path for GetNode
for your signal manager. GetNode
expects a scene tree path, not a resource path.
When you create an autoload in Godot, it just attaches an instance of the node you specify to the root of the scene tree. So in order to get that instance with GetNode
you need to use GetNode<your_node_type>("/root/<your_autoload_name>")
So in your case it’s GetNode<signal_manager>("/root/SignalManager")
Also in AppleCounter.cs
line 13 you’re using Self
, which doesn’t exist in C#. Take a look at Godot Docs on C# Signals to learn how signals are used in C#.
Your variables and methods that make up your class should be static.
So for example in your Signal manager you have
public deletgate void GotAppleEventHandler(int number);
This should be:
public static deletgate void GotAppleEventHandler(int number);
You have to do that in an autload script as there is only one copy of it in the game.
(although technically you could create another copy as these are not true singletons , but dont!)
You can of course just create a ‘pure’ c# singleton if you wanted and Godot would not know anything about it:
static modifier - C# Reference - C# | Microsoft Learn
However the autoload needs to be able to add the node to the tree so it has to inherit from the Node class. It there can technically be instantiated more than once but of course dont do that.
Bottom line is if you only have one version of a class in your game then anything inside that class needs to be ‘static’. Because static objects cannot be instantiated.
Hope that makes sense!