mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 17:28:58 +00:00
classref: Sync with current master branch (215acd5)
This commit is contained in:
+4
-150
@@ -10,7 +10,7 @@
|
||||
Object
|
||||
======
|
||||
|
||||
**Inherited By:** :ref:`AudioServer<class_AudioServer>`, :ref:`CameraServer<class_CameraServer>`, :ref:`ClassDB<class_ClassDB>`, :ref:`DisplayServer<class_DisplayServer>`, :ref:`EditorFileSystemDirectory<class_EditorFileSystemDirectory>`, :ref:`EditorInterface<class_EditorInterface>`, :ref:`EditorPaths<class_EditorPaths>`, :ref:`EditorSelection<class_EditorSelection>`, :ref:`EditorUndoRedoManager<class_EditorUndoRedoManager>`, :ref:`EditorVCSInterface<class_EditorVCSInterface>`, :ref:`Engine<class_Engine>`, :ref:`EngineDebugger<class_EngineDebugger>`, :ref:`FramebufferCacheRD<class_FramebufferCacheRD>`, :ref:`GDExtensionManager<class_GDExtensionManager>`, :ref:`Geometry2D<class_Geometry2D>`, :ref:`Geometry3D<class_Geometry3D>`, :ref:`Input<class_Input>`, :ref:`InputMap<class_InputMap>`, :ref:`IP<class_IP>`, :ref:`JavaClassWrapper<class_JavaClassWrapper>`, :ref:`JavaScriptBridge<class_JavaScriptBridge>`, :ref:`JNISingleton<class_JNISingleton>`, :ref:`JSONRPC<class_JSONRPC>`, :ref:`MainLoop<class_MainLoop>`, :ref:`Marshalls<class_Marshalls>`, :ref:`MovieWriter<class_MovieWriter>`, :ref:`NativeMenu<class_NativeMenu>`, :ref:`NavigationMeshGenerator<class_NavigationMeshGenerator>`, :ref:`NavigationServer2D<class_NavigationServer2D>`, :ref:`NavigationServer3D<class_NavigationServer3D>`, :ref:`Node<class_Node>`, :ref:`OpenXRExtensionWrapperExtension<class_OpenXRExtensionWrapperExtension>`, :ref:`OpenXRInteractionProfileMetadata<class_OpenXRInteractionProfileMetadata>`, :ref:`OS<class_OS>`, :ref:`Performance<class_Performance>`, :ref:`PhysicsDirectBodyState2D<class_PhysicsDirectBodyState2D>`, :ref:`PhysicsDirectBodyState3D<class_PhysicsDirectBodyState3D>`, :ref:`PhysicsDirectSpaceState2D<class_PhysicsDirectSpaceState2D>`, :ref:`PhysicsDirectSpaceState3D<class_PhysicsDirectSpaceState3D>`, :ref:`PhysicsServer2D<class_PhysicsServer2D>`, :ref:`PhysicsServer2DManager<class_PhysicsServer2DManager>`, :ref:`PhysicsServer3D<class_PhysicsServer3D>`, :ref:`PhysicsServer3DManager<class_PhysicsServer3DManager>`, :ref:`PhysicsServer3DRenderingServerHandler<class_PhysicsServer3DRenderingServerHandler>`, :ref:`ProjectSettings<class_ProjectSettings>`, :ref:`RefCounted<class_RefCounted>`, :ref:`RenderData<class_RenderData>`, :ref:`RenderingDevice<class_RenderingDevice>`, :ref:`RenderingServer<class_RenderingServer>`, :ref:`RenderSceneData<class_RenderSceneData>`, :ref:`ResourceLoader<class_ResourceLoader>`, :ref:`ResourceSaver<class_ResourceSaver>`, :ref:`ResourceUID<class_ResourceUID>`, :ref:`ScriptLanguage<class_ScriptLanguage>`, :ref:`ShaderIncludeDB<class_ShaderIncludeDB>`, :ref:`TextServerManager<class_TextServerManager>`, :ref:`ThemeDB<class_ThemeDB>`, :ref:`TileData<class_TileData>`, :ref:`Time<class_Time>`, :ref:`TranslationServer<class_TranslationServer>`, :ref:`TreeItem<class_TreeItem>`, :ref:`UndoRedo<class_UndoRedo>`, :ref:`UniformSetCacheRD<class_UniformSetCacheRD>`, :ref:`WorkerThreadPool<class_WorkerThreadPool>`, :ref:`XRServer<class_XRServer>`, :ref:`XRVRS<class_XRVRS>`
|
||||
**Inherited By:** :ref:`AudioServer<class_AudioServer>`, :ref:`CameraServer<class_CameraServer>`, :ref:`ClassDB<class_ClassDB>`, :ref:`DisplayServer<class_DisplayServer>`, :ref:`EditorFileSystemDirectory<class_EditorFileSystemDirectory>`, :ref:`EditorInterface<class_EditorInterface>`, :ref:`EditorPaths<class_EditorPaths>`, :ref:`EditorSelection<class_EditorSelection>`, :ref:`EditorUndoRedoManager<class_EditorUndoRedoManager>`, :ref:`EditorVCSInterface<class_EditorVCSInterface>`, :ref:`Engine<class_Engine>`, :ref:`EngineDebugger<class_EngineDebugger>`, :ref:`FramebufferCacheRD<class_FramebufferCacheRD>`, :ref:`GDExtensionManager<class_GDExtensionManager>`, :ref:`Geometry2D<class_Geometry2D>`, :ref:`Geometry3D<class_Geometry3D>`, :ref:`Input<class_Input>`, :ref:`InputMap<class_InputMap>`, :ref:`IP<class_IP>`, :ref:`JavaClassWrapper<class_JavaClassWrapper>`, :ref:`JavaScriptBridge<class_JavaScriptBridge>`, :ref:`JNISingleton<class_JNISingleton>`, :ref:`JSONRPC<class_JSONRPC>`, :ref:`MainLoop<class_MainLoop>`, :ref:`Marshalls<class_Marshalls>`, :ref:`MovieWriter<class_MovieWriter>`, :ref:`NativeMenu<class_NativeMenu>`, :ref:`NavigationMeshGenerator<class_NavigationMeshGenerator>`, :ref:`NavigationServer2D<class_NavigationServer2D>`, :ref:`NavigationServer3D<class_NavigationServer3D>`, :ref:`Node<class_Node>`, :ref:`OpenXRExtensionWrapper<class_OpenXRExtensionWrapper>`, :ref:`OpenXRInteractionProfileMetadata<class_OpenXRInteractionProfileMetadata>`, :ref:`OS<class_OS>`, :ref:`Performance<class_Performance>`, :ref:`PhysicsDirectBodyState2D<class_PhysicsDirectBodyState2D>`, :ref:`PhysicsDirectBodyState3D<class_PhysicsDirectBodyState3D>`, :ref:`PhysicsDirectSpaceState2D<class_PhysicsDirectSpaceState2D>`, :ref:`PhysicsDirectSpaceState3D<class_PhysicsDirectSpaceState3D>`, :ref:`PhysicsServer2D<class_PhysicsServer2D>`, :ref:`PhysicsServer2DManager<class_PhysicsServer2DManager>`, :ref:`PhysicsServer3D<class_PhysicsServer3D>`, :ref:`PhysicsServer3DManager<class_PhysicsServer3DManager>`, :ref:`PhysicsServer3DRenderingServerHandler<class_PhysicsServer3DRenderingServerHandler>`, :ref:`ProjectSettings<class_ProjectSettings>`, :ref:`RefCounted<class_RefCounted>`, :ref:`RenderData<class_RenderData>`, :ref:`RenderingDevice<class_RenderingDevice>`, :ref:`RenderingServer<class_RenderingServer>`, :ref:`RenderSceneData<class_RenderSceneData>`, :ref:`ResourceLoader<class_ResourceLoader>`, :ref:`ResourceSaver<class_ResourceSaver>`, :ref:`ResourceUID<class_ResourceUID>`, :ref:`ScriptLanguage<class_ScriptLanguage>`, :ref:`ShaderIncludeDB<class_ShaderIncludeDB>`, :ref:`TextServerManager<class_TextServerManager>`, :ref:`ThemeDB<class_ThemeDB>`, :ref:`TileData<class_TileData>`, :ref:`Time<class_Time>`, :ref:`TranslationServer<class_TranslationServer>`, :ref:`TreeItem<class_TreeItem>`, :ref:`UndoRedo<class_UndoRedo>`, :ref:`UniformSetCacheRD<class_UniformSetCacheRD>`, :ref:`WorkerThreadPool<class_WorkerThreadPool>`, :ref:`XRServer<class_XRServer>`, :ref:`XRVRS<class_XRVRS>`
|
||||
|
||||
Base class for all other classes in the engine.
|
||||
|
||||
@@ -533,8 +533,6 @@ Returns the current iterable value. ``iter`` stores the iteration state, but unl
|
||||
|
||||
Initializes the iterator. ``iter`` stores the iteration state. Since GDScript does not support passing arguments by reference, a single-element array is used as a wrapper. Returns ``true`` so long as the iterator has not reached the end.
|
||||
|
||||
Example:
|
||||
|
||||
::
|
||||
|
||||
class MyRange:
|
||||
@@ -966,155 +964,11 @@ If this method is called during :ref:`NOTIFICATION_PREDELETE<class_Object_consta
|
||||
|
||||
Connects a ``signal`` by name to a ``callable``. Optional ``flags`` can be also added to configure the connection's behavior (see :ref:`ConnectFlags<enum_Object_ConnectFlags>` constants).
|
||||
|
||||
A signal can only be connected once to the same :ref:`Callable<class_Callable>`. If the signal is already connected, this method returns :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` and pushes an error message, unless the signal is connected with :ref:`CONNECT_REFERENCE_COUNTED<class_Object_constant_CONNECT_REFERENCE_COUNTED>`. To prevent this, use :ref:`is_connected()<class_Object_method_is_connected>` first to check for existing connections.
|
||||
|
||||
If the ``callable``'s object is freed, the connection will be lost.
|
||||
|
||||
\ **Examples with recommended syntax:**\
|
||||
|
||||
Connecting signals is one of the most common operations in Godot and the API gives many options to do so, which are described further down. The code block below shows the recommended approach.
|
||||
|
||||
|
||||
.. tabs::
|
||||
|
||||
.. code-tab:: gdscript
|
||||
|
||||
func _ready():
|
||||
var button = Button.new()
|
||||
# `button_down` here is a Signal variant type, and we thus call the Signal.connect() method, not Object.connect().
|
||||
# See discussion below for a more in-depth overview of the API.
|
||||
button.button_down.connect(_on_button_down)
|
||||
|
||||
# This assumes that a `Player` class exists, which defines a `hit` signal.
|
||||
var player = Player.new()
|
||||
# We use Signal.connect() again, and we also use the Callable.bind() method,
|
||||
# which returns a new Callable with the parameter binds.
|
||||
player.hit.connect(_on_player_hit.bind("sword", 100))
|
||||
|
||||
func _on_button_down():
|
||||
print("Button down!")
|
||||
|
||||
func _on_player_hit(weapon_type, damage):
|
||||
print("Hit with weapon %s for %d damage." % [weapon_type, damage])
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var button = new Button();
|
||||
// C# supports passing signals as events, so we can use this idiomatic construct:
|
||||
button.ButtonDown += OnButtonDown;
|
||||
|
||||
// This assumes that a `Player` class exists, which defines a `Hit` signal.
|
||||
var player = new Player();
|
||||
// We can use lambdas when we need to bind additional parameters.
|
||||
player.Hit += () => OnPlayerHit("sword", 100);
|
||||
}
|
||||
|
||||
private void OnButtonDown()
|
||||
{
|
||||
GD.Print("Button down!");
|
||||
}
|
||||
|
||||
private void OnPlayerHit(string weaponType, int damage)
|
||||
{
|
||||
GD.Print($"Hit with weapon {weaponType} for {damage} damage.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
\ **\ ``Object.connect()`` or ``Signal.connect()``?**\
|
||||
|
||||
As seen above, the recommended method to connect signals is not :ref:`connect()<class_Object_method_connect>`. The code block below shows the four options for connecting signals, using either this legacy method or the recommended :ref:`Signal.connect()<class_Signal_method_connect>`, and using either an implicit :ref:`Callable<class_Callable>` or a manually defined one.
|
||||
|
||||
|
||||
.. tabs::
|
||||
|
||||
.. code-tab:: gdscript
|
||||
|
||||
func _ready():
|
||||
var button = Button.new()
|
||||
# Option 1: Object.connect() with an implicit Callable for the defined function.
|
||||
button.connect("button_down", _on_button_down)
|
||||
# Option 2: Object.connect() with a constructed Callable using a target object and method name.
|
||||
button.connect("button_down", Callable(self, "_on_button_down"))
|
||||
# Option 3: Signal.connect() with an implicit Callable for the defined function.
|
||||
button.button_down.connect(_on_button_down)
|
||||
# Option 4: Signal.connect() with a constructed Callable using a target object and method name.
|
||||
button.button_down.connect(Callable(self, "_on_button_down"))
|
||||
|
||||
func _on_button_down():
|
||||
print("Button down!")
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var button = new Button();
|
||||
// Option 1: In C#, we can use signals as events and connect with this idiomatic syntax:
|
||||
button.ButtonDown += OnButtonDown;
|
||||
// Option 2: GodotObject.Connect() with a constructed Callable from a method group.
|
||||
button.Connect(Button.SignalName.ButtonDown, Callable.From(OnButtonDown));
|
||||
// Option 3: GodotObject.Connect() with a constructed Callable using a target object and method name.
|
||||
button.Connect(Button.SignalName.ButtonDown, new Callable(this, MethodName.OnButtonDown));
|
||||
}
|
||||
|
||||
private void OnButtonDown()
|
||||
{
|
||||
GD.Print("Button down!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
While all options have the same outcome (``button``'s :ref:`BaseButton.button_down<class_BaseButton_signal_button_down>` signal will be connected to ``_on_button_down``), **option 3** offers the best validation: it will print a compile-time error if either the ``button_down`` :ref:`Signal<class_Signal>` or the ``_on_button_down`` :ref:`Callable<class_Callable>` are not defined. On the other hand, **option 2** only relies on string names and will only be able to validate either names at runtime: it will print a runtime error if ``"button_down"`` doesn't correspond to a signal, or if ``"_on_button_down"`` is not a registered method in the object ``self``. The main reason for using options 1, 2, or 4 would be if you actually need to use strings (e.g. to connect signals programmatically based on strings read from a configuration file). Otherwise, option 3 is the recommended (and fastest) method.
|
||||
|
||||
\ **Binding and passing parameters:**\
|
||||
|
||||
The syntax to bind parameters is through :ref:`Callable.bind()<class_Callable_method_bind>`, which returns a copy of the :ref:`Callable<class_Callable>` with its parameters bound.
|
||||
|
||||
When calling :ref:`emit_signal()<class_Object_method_emit_signal>` or :ref:`Signal.emit()<class_Signal_method_emit>`, the signal parameters can be also passed. The examples below show the relationship between these signal parameters and bound parameters.
|
||||
|
||||
|
||||
.. tabs::
|
||||
|
||||
.. code-tab:: gdscript
|
||||
|
||||
func _ready():
|
||||
# This assumes that a `Player` class exists, which defines a `hit` signal.
|
||||
var player = Player.new()
|
||||
# Using Callable.bind().
|
||||
player.hit.connect(_on_player_hit.bind("sword", 100))
|
||||
|
||||
# Parameters added when emitting the signal are passed first.
|
||||
player.hit.emit("Dark lord", 5)
|
||||
|
||||
# We pass two arguments when emitting (`hit_by`, `level`),
|
||||
# and bind two more arguments when connecting (`weapon_type`, `damage`).
|
||||
func _on_player_hit(hit_by, level, weapon_type, damage):
|
||||
print("Hit by %s (level %d) with weapon %s for %d damage." % [hit_by, level, weapon_type, damage])
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// This assumes that a `Player` class exists, which defines a `Hit` signal.
|
||||
var player = new Player();
|
||||
// Using lambda expressions that create a closure that captures the additional parameters.
|
||||
// The lambda only receives the parameters defined by the signal's delegate.
|
||||
player.Hit += (hitBy, level) => OnPlayerHit(hitBy, level, "sword", 100);
|
||||
|
||||
// Parameters added when emitting the signal are passed first.
|
||||
player.EmitSignal(SignalName.Hit, "Dark lord", 5);
|
||||
}
|
||||
|
||||
// We pass two arguments when emitting (`hit_by`, `level`),
|
||||
// and bind two more arguments when connecting (`weapon_type`, `damage`).
|
||||
private void OnPlayerHit(string hitBy, int level, string weaponType, int damage)
|
||||
{
|
||||
GD.Print($"Hit by {hitBy} (level {level}) with weapon {weaponType} for {damage} damage.");
|
||||
}
|
||||
A signal can only be connected once to the same :ref:`Callable<class_Callable>`. If the signal is already connected, this method returns :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` and generates an error, unless the signal is connected with :ref:`CONNECT_REFERENCE_COUNTED<class_Object_constant_CONNECT_REFERENCE_COUNTED>`. To prevent this, use :ref:`is_connected()<class_Object_method_is_connected>` first to check for existing connections.
|
||||
|
||||
\ **Note:** If the ``callable``'s object is freed, the connection will be lost.
|
||||
|
||||
\ **Note:** In GDScript, it is generally recommended to connect signals with :ref:`Signal.connect()<class_Signal_method_connect>` instead.
|
||||
|
||||
.. rst-class:: classref-item-separator
|
||||
|
||||
|
||||
Reference in New Issue
Block a user