浏览代码

Use 4.0 method of connecting to and emitting signals (#6311)

Change docs to use the new method of connecting and emitting signals
BlueStag 2 年之前
父节点
当前提交
135ad68cab

+ 1 - 1
getting_started/first_2d_game/03.coding_the_player.rst

@@ -478,7 +478,7 @@ this code to the function:
 
 
     func _on_Player_body_entered(body):
     func _on_Player_body_entered(body):
         hide() # Player disappears after being hit.
         hide() # Player disappears after being hit.
-        emit_signal("hit")
+        hit.emit()
         # Must be deferred as we can't change physics properties on a physics callback.
         # Must be deferred as we can't change physics properties on a physics callback.
         $CollisionShape2D.set_deferred("disabled", true)
         $CollisionShape2D.set_deferred("disabled", true)
 
 

+ 1 - 1
getting_started/first_2d_game/06.heads_up_display.rst

@@ -294,7 +294,7 @@ signal of ``StartButton``, and add the following code to the new functions:
 
 
     func _on_StartButton_pressed():
     func _on_StartButton_pressed():
         $StartButton.hide()
         $StartButton.hide()
-        emit_signal("start_game")
+        start_game.emit()
 
 
     func _on_MessageTimer_timeout():
     func _on_MessageTimer_timeout():
         $Message.hide()
         $Message.hide()

+ 1 - 1
getting_started/first_3d_game/06.jump_and_squash.rst

@@ -321,7 +321,7 @@ destroy the mob.
 
 
 
 
    func squash():
    func squash():
-       emit_signal("squashed")
+       squashed.emit()
        queue_free()
        queue_free()
 
 
  .. code-tab:: csharp
  .. code-tab:: csharp

+ 5 - 6
getting_started/first_3d_game/07.killing_player.rst

@@ -71,7 +71,7 @@ a ``die()`` function that helps us put a descriptive label on the code.
 
 
    # And this function at the bottom.
    # And this function at the bottom.
    func die():
    func die():
-       emit_signal("hit")
+       hit.emit()
        queue_free()
        queue_free()
 
 
 
 
@@ -269,8 +269,8 @@ Next is ``Mob.gd``.
         queue_free()
         queue_free()
 
 
     func squash():
     func squash():
-        emit_signal("squashed")
-        queue_free() # Destroy this node
+       squashed.emit()
+       queue_free() # Destroy this node
  .. code-tab:: csharp
  .. code-tab:: csharp
 
 
     using Godot;
     using Godot;
@@ -321,8 +321,7 @@ Finally, the longest script, ``Player.gd``:
 
 
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
-
-   extends CharacterBody3D
+    extends CharacterBody3D
 
 
     signal hit
     signal hit
 
 
@@ -396,7 +395,7 @@ Finally, the longest script, ``Player.gd``:
 
 
     # And this function at the bottom.
     # And this function at the bottom.
     func die():
     func die():
-        emit_signal("hit")
+        hit.emit()
         queue_free()
         queue_free()
 
 
     func _on_mob_detector_body_entered(body):
     func _on_mob_detector_body_entered(body):

+ 2 - 3
getting_started/first_3d_game/09.adding_animations.rst

@@ -293,7 +293,6 @@ Here's the *Player* script.
 
 
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
-
     extends CharacterBody3D
     extends CharacterBody3D
 
 
     signal hit
     signal hit
@@ -373,7 +372,7 @@ Here's the *Player* script.
 
 
     # And this function at the bottom.
     # And this function at the bottom.
     func die():
     func die():
-        emit_signal("hit")
+        hit.emit()
         queue_free()
         queue_free()
 
 
     func _on_mob_detector_body_entered(body):
     func _on_mob_detector_body_entered(body):
@@ -518,7 +517,7 @@ And the *Mob*'s script.
         queue_free()
         queue_free()
 
 
     func squash():
     func squash():
-        emit_signal("squashed")
+        squashed.emit()
         queue_free() # Destroy this node
         queue_free() # Destroy this node
  .. code-tab:: csharp
  .. code-tab:: csharp
 
 

+ 6 - 6
getting_started/step_by_step/signals.rst

@@ -258,10 +258,10 @@ Click the script icon next to Sprite2D to jump back to the scripting workspace.
 We need to do two operations to connect the nodes via code:
 We need to do two operations to connect the nodes via code:
 
 
 1. Get a reference to the Timer from the Sprite2D.
 1. Get a reference to the Timer from the Sprite2D.
-2. Call the Timer's ``connect()`` method.
+2. Call the ``connect()`` method on the Timer's "timeout" signal.
 
 
 .. note:: To connect to a signal via code, you need to call the ``connect()``
 .. note:: To connect to a signal via code, you need to call the ``connect()``
-          method of the node you want to listen to. In this case, we want to
+          method of the signal you want to listen to. In this case, we want to
           listen to the Timer's "timeout" signal.
           listen to the Timer's "timeout" signal.
 
 
 We want to connect the signal when the scene is instantiated, and we can do that
 We want to connect the signal when the scene is instantiated, and we can do that
@@ -439,7 +439,7 @@ you can connect to them like any other.
 
 
 .. image:: img/signals_17_custom_signal.png
 .. image:: img/signals_17_custom_signal.png
 
 
-To emit a signal in your scripts, call ``emit_signal()``.
+To emit a signal in your scripts, call ``emit()`` on the signal.
 
 
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
@@ -447,7 +447,7 @@ To emit a signal in your scripts, call ``emit_signal()``.
     func take_damage(amount):
     func take_damage(amount):
         health -= amount
         health -= amount
         if health <= 0:
         if health <= 0:
-            emit_signal("health_depleted")
+            health_depleted.emit()
 
 
  .. code-tab:: csharp C#
  .. code-tab:: csharp C#
 
 
@@ -493,7 +493,7 @@ names between parentheses:
     correct values.
     correct values.
 
 
 To emit values along with the signal, add them as extra arguments to the
 To emit values along with the signal, add them as extra arguments to the
-``emit_signal()`` function:
+``emit()`` function:
 
 
 .. tabs::
 .. tabs::
  .. code-tab:: gdscript GDScript
  .. code-tab:: gdscript GDScript
@@ -501,7 +501,7 @@ To emit values along with the signal, add them as extra arguments to the
     func take_damage(amount):
     func take_damage(amount):
         var old_health = health
         var old_health = health
         health -= amount
         health -= amount
-        emit_signal("health_changed", old_health, health)
+        health_changed.emit(old_health, health)
 
 
  .. code-tab:: csharp C#
  .. code-tab:: csharp C#
 
 

+ 2 - 2
tutorials/best_practices/godot_notifications.rst

@@ -249,10 +249,10 @@ nodes that one might create at runtime.
             NOTIFICATION_PARENTED:
             NOTIFICATION_PARENTED:
                 parent_cache = get_parent()
                 parent_cache = get_parent()
                 if connection_check():
                 if connection_check():
-                    parent_cache.connect("interacted_with", self, "_on_parent_interacted_with")
+                    parent_cache.interacted_with.connect(_on_parent_interacted_with)
             NOTIFICATION_UNPARENTED:
             NOTIFICATION_UNPARENTED:
                 if connection_check():
                 if connection_check():
-                    parent_cache.disconnect("interacted_with", self, "_on_parent_interacted_with")
+                    parent_cache.interacted_with.disconnect(_on_parent_interacted_with)
 
 
     func _on_parent_interacted_with():
     func _on_parent_interacted_with():
         print("I'm reacting to my parent's interaction!")
         print("I'm reacting to my parent's interaction!")

+ 2 - 2
tutorials/best_practices/scene_organization.rst

@@ -57,10 +57,10 @@ initialize it:
      .. code-tab:: gdscript GDScript
      .. code-tab:: gdscript GDScript
 
 
        # Parent
        # Parent
-       $Child.connect("signal_name", object_with_method, "method_on_the_object")
+       $Child.signal_name.connect(method_on_the_object)
 
 
        # Child
        # Child
-       emit_signal("signal_name") # Triggers parent-defined behavior.
+       signal_name.emit() # Triggers parent-defined behavior.
 
 
      .. code-tab:: csharp
      .. code-tab:: csharp
 
 

+ 5 - 5
tutorials/networking/high_level_multiplayer.rst

@@ -222,11 +222,11 @@ Let's get back to the lobby. Imagine that each player that connects to the serve
     # Connect all functions
     # Connect all functions
 
 
     func _ready():
     func _ready():
-        get_tree().connect("network_peer_connected", self, "_player_connected")
-        get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
-        get_tree().connect("connected_to_server", self, "_connected_ok")
-        get_tree().connect("connection_failed", self, "_connected_fail")
-        get_tree().connect("server_disconnected", self, "_server_disconnected")
+        get_tree().network_peer_connected.connect(_player_connected)
+        get_tree().network_peer_disconnected.connect(_player_disconnected)
+        get_tree().connected_to_server.connect(_connected_ok)
+        get_tree().connection_failed.connect(_connected_fail)
+        get_tree().server_disconnected.connect(_server_disconnected)
 
 
     # Player info, associate ID to data
     # Player info, associate ID to data
     var player_info = {}
     var player_info = {}

+ 2 - 2
tutorials/networking/webrtc.rst

@@ -119,8 +119,8 @@ This example expands on the previous one, separating the peers in two different
 
 
     func _ready():
     func _ready():
         # Connect all functions
         # Connect all functions
-        peer.connect("ice_candidate_created", self, "_on_ice_candidate")
-        peer.connect("session_description_created", self, "_on_session")
+        peer.ice_candidate_created.connect(_on_ice_candidate)
+        peer.session_description_created.connect(_on_session)
 
 
         # Register to the local signaling server (see below for the implementation)
         # Register to the local signaling server (see below for the implementation)
         Signaling.register(get_path())
         Signaling.register(get_path())

+ 14 - 14
tutorials/platform/android/android_in_app_purchases.rst

@@ -51,20 +51,20 @@ Initialization example:
 
 
             # These are all signals supported by the API
             # These are all signals supported by the API
             # You can drop some of these based on your needs
             # You can drop some of these based on your needs
-            payment.connect("billing_resume", self, "_on_billing_resume") # No params
-            payment.connect("connected", self, "_on_connected") # No params
-            payment.connect("disconnected", self, "_on_disconnected") # No params
-            payment.connect("connect_error", self, "_on_connect_error") # Response ID (int), Debug message (string)
-            payment.connect("price_change_acknowledged", self, "_on_price_acknowledged") # Response ID (int)
-            payment.connect("purchases_updated", self, "_on_purchases_updated") # Purchases (Dictionary[])
-            payment.connect("purchase_error", self, "_on_purchase_error") # Response ID (int), Debug message (string)
-            payment.connect("sku_details_query_completed", self, "_on_sku_details_query_completed") # SKUs (Dictionary[])
-            payment.connect("sku_details_query_error", self, "_on_sku_details_query_error") # Response ID (int), Debug message (string), Queried SKUs (string[])
-            payment.connect("purchase_acknowledged", self, "_on_purchase_acknowledged") # Purchase token (string)
-            payment.connect("purchase_acknowledgement_error", self, "_on_purchase_acknowledgement_error") # Response ID (int), Debug message (string), Purchase token (string)
-            payment.connect("purchase_consumed", self, "_on_purchase_consumed") # Purchase token (string)
-            payment.connect("purchase_consumption_error", self, "_on_purchase_consumption_error") # Response ID (int), Debug message (string), Purchase token (string)
-            payment.connect("query_purchases_response", self, "_on_query_purchases_response") # Purchases (Dictionary[])
+            payment.billing_resume.connect(_on_billing_resume) # No params
+            payment.connected.connect(_on_connected) # No params
+            payment.disconnected.connect(_on_disconnected) # No params
+            payment.connect_error.connect(_on_connect_error) # Response ID (int), Debug message (string)
+            payment.price_change_acknowledged.connect(_on_price_acknowledged) # Response ID (int)
+            payment.purchases_updated.connect(_on_purchases_updated) # Purchases (Dictionary[])
+            payment.purchase_error.connect(_on_purchase_error) # Response ID (int), Debug message (string)
+            payment.sku_details_query_completed.connect(_on_sku_details_query_completed) # SKUs (Dictionary[])
+            payment.sku_details_query_error.connect(_on_sku_details_query_error) # Response ID (int), Debug message (string), Queried SKUs (string[])
+            payment.purchase_acknowledged.connect(_on_purchase_acknowledged) # Purchase token (string)
+            payment.purchase_acknowledgement_error.connect(_on_purchase_acknowledgement_error) # Response ID (int), Debug message (string), Purchase token (string)
+            payment.purchase_consumed.connect(_on_purchase_consumed) # Purchase token (string)
+            payment.purchase_consumption_error.connect(_on_purchase_consumption_error) # Response ID (int), Debug message (string), Purchase token (string)
+            payment.query_purchases_response.connect(_on_query_purchases_response) # Purchases (Dictionary[])
 
 
             payment.startConnection()
             payment.startConnection()
         else:
         else:

+ 6 - 6
tutorials/scripting/gdscript/gdscript_styleguide.rst

@@ -69,7 +69,7 @@ Here is a complete class example based on these guidelines:
         _state.exit()
         _state.exit()
         self._state = target_state
         self._state = target_state
         _state.enter(msg)
         _state.enter(msg)
-        Events.emit_signal("player_state_changed", _state.name)
+        Events.player_state_changed.emit(_state.name)
 
 
 
 
     func set_is_active(value):
     func set_is_active(value):
@@ -86,7 +86,7 @@ Here is a complete class example based on these guidelines:
 
 
     func _on_state_changed(previous, new):
     func _on_state_changed(previous, new):
         print("state changed")
         print("state changed")
-        emit_signal("state_changed")
+        state_changed.emit()
 
 
 .. _formatting:
 .. _formatting:
 
 
@@ -264,13 +264,13 @@ Surround functions and class definitions with two blank lines:
     func heal(amount):
     func heal(amount):
         health += amount
         health += amount
         health = min(health, max_health)
         health = min(health, max_health)
-        emit_signal("health_changed", health)
+        health_changed.emit(health)
 
 
 
 
     func take_damage(amount, effect=null):
     func take_damage(amount, effect=null):
         health -= amount
         health -= amount
         health = max(0, health)
         health = max(0, health)
-        emit_signal("health_changed", health)
+        health_changed.emit(health)
 
 
 Use one blank line inside functions to separate logical sections.
 Use one blank line inside functions to separate logical sections.
 
 
@@ -841,12 +841,12 @@ in that order.
         _state.exit()
         _state.exit()
         self._state = target_state
         self._state = target_state
         _state.enter(msg)
         _state.enter(msg)
-        Events.emit_signal("player_state_changed", _state.name)
+        player_state_changed.emit(_state.name)
 
 
 
 
     func _on_state_changed(previous, new):
     func _on_state_changed(previous, new):
         print("state changed")
         print("state changed")
-        emit_signal("state_changed")
+        state_changed.emit()
 
 
 
 
 Static typing
 Static typing