Instancing with signals

Signals provide a way to decouple game objects, allowing you to avoid forcing a fixed arrangement of nodes. One sign that a signal might be called for is when you find yourself using get_parent(). Referring directly to a node’s parent means that you can’t easily move that node to another location in the scene tree. This can be especially problematic when you are instancing objects at runtime and may want to place them in an arbitrary location in the running scene tree.

Below we’ll consider an example of such a situation: firing bullets.

射击示例

Consider a player character that can rotate and shoot towards the mouse. Every time the mouse button is clicked, we create an instance of the bullet at the player’s location. See 实例化 for details.

We’ll use an Area2D for the bullet, which moves in a straight line at a given velocity:

GDScript

C#

  1. extends Area2D
  2. var velocity = Vector2.ZERO
  3. func _physics_process(delta):
  4. position += velocity * delta
  1. public class Bullet : Area2D
  2. {
  3. Vector2 Velocity = new Vector2();
  4. public override void _PhysicsProcess(float delta)
  5. {
  6. Position += Velocity * delta;
  7. }
  8. }

然而,如果子弹作为游戏角色的子节点添加,那么当它旋转时,子弹将仍然保持“附着”在游戏角色身上:

../../_images/signals_shoot1.gif

相反,我们需要子弹独立于游戏角色的移动——一旦发射,子弹就会继续沿着直线运动,游戏角色就不能再影响它们了。与其作为游戏角色的子节点被添加到场景树中,不如将子弹作为“主”游戏场景的子节点添加上去更有意义,后者可能是游戏角色的父节点,甚至可能是更高层级的树。

You could do this by adding the bullet to the main scene directly:

GDScript

C#

  1. var bullet_instance = Bullet.instance()
  2. get_parent().add_child(bullet_instance)
  1. Node bulletInstance = Bullet.Instance();
  2. GetParent().AddChild(bulletInstance);

However, this will lead to a different problem. Now if you try to test your “Player” scene independently, it will crash on shooting, because there is no parent node to access. This makes it a lot harder to test your player code independently and also means that if you decide to change your main scene’s node structure, the player’s parent may no longer be the appropriate node to receive the bullets.

解决这个问题的方法是使用一个信号来“发射”游戏角色的子弹。游戏角色不需要“知道”子弹在那之后发生了什么——任何连接到信号的节点都可以“接收”子弹并采取适当的行动来产生它们。

下面是游戏角色使用信号发射子弹的代码:

GDScript

C#

  1. extends Sprite
  2. signal shoot(bullet, direction, location)
  3. var Bullet = preload("res://Bullet.tscn")
  4. func _input(event):
  5. if event is InputEventMouseButton:
  6. if event.button_index == BUTTON_LEFT and event.pressed:
  7. emit_signal("shoot", Bullet, rotation, position)
  8. func _process(delta):
  9. look_at(get_global_mouse_position())
  1. public class Player : Sprite
  2. {
  3. [Signal]
  4. delegate void Shoot(PackedScene bullet, Vector2 direction, Vector2 location);
  5. private PackedScene _bullet = GD.Load<PackedScene>("res://Bullet.tscn");
  6. public override void _Input(InputEvent event)
  7. {
  8. if (input is InputEventMouseButton mouseButton)
  9. {
  10. if (mouseButton.ButtonIndex == (int)ButtonList.Left && mouseButton.Pressed)
  11. {
  12. EmitSignal(nameof(Shoot), _bullet, Rotation, Position);
  13. }
  14. }
  15. }
  16. public override _Process(float delta)
  17. {
  18. LookAt(GetGlobalMousePosition());
  19. }
  20. }

在主场景中,我们连接游戏角色的信号(它将出现在“Node”选项卡中)。

GDScript

C#

  1. func _on_Player_shoot(Bullet, direction, location):
  2. var b = Bullet.instance()
  3. add_child(b)
  4. b.rotation = direction
  5. b.position = location
  6. b.velocity = b.velocity.rotated(direction)
  1. public void _on_Player_Shoot(PackedScene bullet, Vector2 direction, Vector2 location)
  2. {
  3. var bulletInstance = (Bullet)bullet.Instance();
  4. AddChild(bulletInstance);
  5. bulletInstance.Rotation = direction;
  6. bulletInstance.Position = location;
  7. bulletInstance.Velocity = bulletInstance.Velocity.Rotated(direction);
  8. }

现在子弹将保持自己的运动独立于游戏角色的旋转:

../../_images/signals_shoot2.gif