Godot singleton and other written language is not the same, is not achieved by constructing the class. . But through the set, nothing wrong class or an ordinary class, declared as a singleton on the line.
Next, I'll give you a step by step demonstration
1. Create a simple single-case model
Create a Node2D, and create corresponding class
extends Node
class_name Conf
func getStr():
return "hellow"
Example 2. Binding to a single
Open the Project Settings
To this page
Find the class you need to bind
After setting the call name, click Add
Well the screenshot above has been added
3. Check
Run the test, select remote mode tree structure, you will see that you have to help create a good run in real time when the Conf
4. Call
Call is as simple as calling the same way as ordinary enough
· · ·
var gc = get_node ( "/ root / Conf")
Print (gc getStr ().);
· · ·
Try it yourself
Official Tutorial: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/singletons_autoload.html?highlight=singleton
problem:
Examples can be used gd official code directly Singleton, but did not succeed at 3.1, have to understand the small partners can provide some comments received, thank you.
If this test using pure gd code will not be created in the whole life cycle of the code, leaving only the name of a class declaration, uninitialized
I will put this issue to developers
Question 2:
When Singleton gd code as a pure, absolutely do not add the keyword tool, and do not add class_name
I found the problem, but the new version can only wait for the official to solve, like data persistence encountered bug, the problem itself from the development of software, the software may have a memory leak, leading to the collapse function after the restart app these functions have been restored. . .
Reproduced in: https: //www.jianshu.com/p/6d1dd73c8d6b