Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Autoloads versus regular nodes

Godot offers a feature to automatically load nodes at the root of your project, allowing you to access them globally, that can fulfill the role of a Singleton: Singletons (Autoload). These autoloaded nodes are not freed when you change the scene from code with SceneTree.change_scene_to_file.

이 가이드에서는 오토로드 기능을 사용할 때와 이를 피하기 위한 기법을 배우게 됩니다.

오디오 잘림 문제

Other engines can encourage the use of creating manager classes, singletons that organize a lot of functionality into a globally accessible object. Godot offers many ways to avoid global state thanks to the node tree and signals.

For example, let's say we are building a platformer and want to collect coins that play a sound effect. There's a node for that: the AudioStreamPlayer. But if we call the AudioStreamPlayer while it is already playing a sound, the new sound interrupts the first.

A solution is to code a global, autoloaded sound manager class. It generates a pool of AudioStreamPlayer nodes that cycle through as each new request for sound effects comes in. Say we call that class Sound, you can use it from anywhere in your project by calling Sound.play("coin_pickup.ogg"). This solves the problem in the short term but causes more problems:

  1. Global state: one object is now responsible for all objects' data. If the Sound class has errors or doesn't have an AudioStreamPlayer available, all the nodes calling it can break.

  2. Global access: now that any object can call Sound.play(sound_path) from anywhere, there's no longer an easy way to find the source of a bug.

  3. 전역 리소스 할당: AudioStreamPlayer 노드 풀을 처음에 생성하기 때문에 너무 적게 생성하면 버그가 발생하고 너무 많이 생성하면 메모리를 비효율적으로 사용하게 됩니다.

참고

About global access, the problem is that any code anywhere could pass wrong data to the Sound autoload in our example. As a result, the domain to explore to fix the bug spans the entire project.

코드를 씬 내에 국한시킬 경우, 오디오에 개입할 수 있는 스크립트는 한 두개 뿐입니다.

Contrast this with each scene keeping as many AudioStreamPlayer nodes as it needs within itself and all these problems go away:

  1. 각 씬은 자체적인 상태 정보를 관리합니다. 데이터에 문제가 생기면, 해당 데이터가 있는 씬 내에서만 사고를 일으킵니다.

  2. 각 씬은 자기 자신의 노드들에만 액세스합니다. 따라서 버그가 일어난다면 어느 노드에서 오류가 생긴 것인지 특정하기가 쉽습니다.

  3. 각 씬은 자신에게 정확히 필요한 양의 리소스만을 할당합니다.

공유 기능 및 데이터 관리하기

오토로드를 써야 하는 또 다른 이유는 동일한 메소드나 데이터를 여러 씬에서 재사용 하고 싶기 때문입니다.

In the case of functions, you can create a new type of Node that provides that feature for an individual scene using the class_name keyword in GDScript.

데이터의 경우 다음이 가능합니다:

  1. 새로운 타입의 :ref:`Resource <class_Resource>`을 만들어서 데이터를 공유하는 것.

  2. owner 속성(property)로 액세스 할 수 있는 씬의 루트 노드처럼 각 노드에 액세스 할 수 있는 오브젝트에 데이터를 저장하는 것.

언제 오토로드를 사용해야 하는지

GDScript supports the creation of static functions using static func. When combined with class_name, this makes it possible to create libraries of helper functions without having to create an instance to call them. The limitation of static functions is that they can't reference member variables, non-static functions or self.

Since Godot 4.1, GDScript also supports static variables using static var. This means you can now share a variables across instances of a class without having to create a separate autoload.

Still, autoloaded nodes can simplify your code for systems with a wide scope. If the autoload is managing its own information and not invading the data of other objects, then it's a great way to create systems that handle broad-scoped tasks. For example, a quest or a dialogue system.

참고

An autoload is not necessarily a singleton. Nothing prevents you from instantiating copies of an autoloaded node. An autoload is only a tool that makes a node load automatically as a child of the root of your scene tree, regardless of your game's node structure or which scene you run, e.g. by pressing the F6 key.

As a result, you can get the autoloaded node, for example an autoload called Sound, by calling get_node("/root/Sound").