Godot - Singleton / Singleton

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


2068504-2fbc9e6acf8a7caa.png

To this page


2068504-07e04d7f50097a14.png

Find the class you need to bind
2068504-90faa79ea9844459.png

After setting the call name, click Add


2068504-e498a9a9273c8568.png

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


2068504-50fb1b7aef45824d.png

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

Guess you like

Origin blog.csdn.net/weixin_33862041/article/details/91053777