Перейти к основному содержимому

Singletons

A singleton represents a Godot object where there is only a single instance of that object present in your game. The Godot engine provides dozens of singletons for various purposes. In addition, user code can also add and register their own singletons that can be accessed globally. To add a singleton reference to a script, place an Engine Singleton node onto the graph.

Singleton types

By default, Godot provides dozens of singletons, each providing access to very specific features. These singletons are registered by a unique name and can be referenced using that name. The Godot provided singletons are:

Singleton NameDescription
AudioServerServer interface for low-level audio access.
CameraServerServer keeping track of different cameras accessible in Godot.
ClassDBA class information repository.
DisplayServerA server interface for low-level window management.
EditorInterfaceGodot editor's interface.
EngineProvides access to engine properties.
EngineDebuggerExposes the internal debugger.
GDExtensionManagerProvides access to GDExtension functionality.
GDScriptLanguageProtocol-
Geometry2DProvides methods for some common 2D geometric operations.
Geometry3DProvides methods for some common 3D geometric operations.
InputA singleton for handling inputs.
InputMapA singleton that manages all InputEventActions.
IPInternet protocol (IP) support functions such as DNS resolution.
JavaClassWrapper-
JavaScriptBridgeSingleton that connects the engine with the browser's JavaScript context in Web export.
MarshallsData transformation (marshalling) and encoding helpers.
NativeMenuA server interface for OS native menus.
NavigationMeshGeneratorHelper class for creating and clearing navigation meshes.
NavigationServer2DA server interface for low-level 2D navigation access.
NavigationServer3DA server interface for low-level 3D navigation access.
OSProvides access to common operating system functionalities.
PerformanceExposes performance-related data.
PhysicsServer2DA server interface for low-level 2D physics access.
PhysicsServer2DManagerA singleton for managing PhysicsServer2D implementations.
PhysicsServer3DA server interface for low-level 3D physics access.
PhysicsServer3DManagerA singleton for managing PhysicsServer3D implementations.
ProjectSettingsStores globally-accessible variables.
RenderingServerServer for anything visible.
ResourceLoaderA singleton for loading resource files.
ResourceSaverA singleton for saving Resources to the filesystem.
ResourceUIDA singleton that manages the unique identifiers of all resources within a project.
TextServerManagerA singleton for managing TextServer implementations.
ThemeDBA singleton that provides access to static information about Theme resources used by the engine and by your project.
TimeA singleton for working with time data.
TranslationServerA server responsible for language translations.
WorkerThreadPoolA singleton that allocates some Threads on startup, used to offload tasks to these threads.
XRServerServer for AR and VR features.

Selecting the singleton

Once an Engine Singleton node has been placed onto the graph canvas, it will default to the Engine singleton. To change which singleton you want to access:

  1. Select the Singleton node in the graph.
  2. Select the designed choice in the Inspector view for the Singleton property.

Accessing singleton functions

Godot singletons provide access to commonly used functions, such as Time.get_unix_time_from_system(). To access singleton functions, simply:

  1. Drag the mouse away from the output pin on the Engine Singleton node.
  2. When releasing the mouse, the All Actions dialog will show.
  3. Search for the desired function, i.e. get unix time from system
  4. Select the function and press Add or hit Enter to place the function onto the graph.

Registering a user-defined singleton

In addition to the singletons that are provided by Godot, users can also add their own singletons. When a singleton is added, it's expected that you guarantee that the instance remains valid until the singleton is unregistered. If a registered singleton is deleted before it's removed, the editor or game will likely crash.

To add a user-defined singleton using Orchestrator:

  1. Place an Engine Singleton node that refers to the Engine singleton onto the graph.
  2. Drag the mouse away from the output pin.
  3. In the All Actions dialog, search for register and select Call Register Singleton.
  4. Provide a unique name to register the singleton instance with, i.e. MySingleton.
  5. With a reference to your singleton object, connect the reference to the instance input pin.
к сведению

User-defined singletons are registered dynamically at runtime, and therefore are not accessible using the Engine Singleton node. To access a user-defined singleton, use the Engine Singleton node calling the Call Get Singleton function to obtain a reference at runtime.

For most cases, it may be simpler to use an Autoload node instead, since autoloads are registered in the Project Settings and are directly selectable in the Inspector view when selecting the Autoload node. You also won't need to worry about unregistering the Autoload as Godot will handle this automatically.

To unregister a user-defined singleton using Orchestrator:

  1. Place an Engine Singleton node that refers to the Engine singleton onto the graph.
  2. Drag the mouse away from the output pin.
  3. In the All Actions dialog, search for unregister and select Call Unregister Singleton.
  4. Provide the registered singleton's name in the name input pin.