Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
自動読み込みと通常ノード¶
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.
このガイドでは、オートロード機能をいつ使用するか、およびそれを回避するために使用できるテクニックを学習します。
オーディオが切断される問題¶
他のエンジンは、多くの機能をグローバルにアクセス可能なオブジェクトに編成するシングルトンであるマネージャークラスの作成を奨励します。 Godotは、ノードツリーとシグナルのおかげで、グローバルな状態を回避する多くの方法を提供します。
たとえば、開発者がプラットフォーム・ゲームを構築していて、効果音を鳴らすコインを収集したいとします。それに使えるノードがあります: AudioStreamPlayer です。しかし、すでにサウンドを再生している最中にAudioStreamPlayerを呼び出すと、新しいサウンドが最初のサウンドを中断します。
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つのオブジェクトがすべてのオブジェクトのデータを担当するようになっています。
Sound
クラスにエラーがある場合、または利用可能なAudioStreamPlayerがない場合、それを呼び出すすべてのノードが中断する可能性があります。グローバルなアクセス: 任意のオブジェクトがどこからでも
Sound.play(sound_path)
を呼び出すことができるので、バグの原因を見つける簡単な方法はありません。グローバルなリソース割り当て: 最初から
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.
シーン内にコードを保持する場合、オーディオに関係するスクリプトは1つまたは2つだけです。
これとは対照的に、各シーンが必要な数の AudioStreamPlayer
ノードを自分で保持すれば、これらの問題はすべて解決します。
各シーンは、独自の状態情報を管理します。データに問題がある場合、その1つのシーンでのみ問題が発生します。
各シーンは、独自のノードのみにアクセスします。バグがある場合、どのノードに障害があるかを簡単に見つけることができます。
各シーンは、必要なリソースの量を正確に割り当てます。
自動読み込みを使用する必要がある場合¶
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")
.