instancing_with_signals.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. .. _doc_instancing_with_signals:
  2. Instancing with signals
  3. =======================
  4. Signals provide a way to decouple game objects, allowing you to avoid forcing a
  5. fixed arrangement of nodes. One sign that a signal might be called for is when
  6. you find yourself using ``get_parent()``. Referring directly to a node's parent
  7. means that you can't easily move that node to another location in the scene tree.
  8. This can be especially problematic when you are instancing objects at runtime
  9. and may want to place them in an arbitrary location in the running scene tree.
  10. Below we'll consider an example of such a situation: firing bullets.
  11. Shooting example
  12. ----------------
  13. Consider a player character that can rotate and shoot towards the mouse. Every
  14. time the mouse button is clicked, we create an instance of the bullet at the
  15. player's location. See :ref:`doc_instancing` for details.
  16. We'll use an ``Area2D`` for the bullet, which moves in a straight line at a
  17. given velocity:
  18. .. tabs::
  19. .. code-tab:: gdscript GDScript
  20. extends Area2D
  21. var velocity = Vector2.ZERO
  22. func _physics_process(delta):
  23. position += velocity * delta
  24. .. code-tab:: csharp
  25. public class Bullet : Area2D
  26. {
  27. Vector2 Velocity = new Vector2();
  28. public override void _PhysicsProcess(float delta)
  29. {
  30. Position += Velocity * delta;
  31. }
  32. }
  33. However, if the bullets are added as children of the player, then they will
  34. remain "attached" to the player as it rotates:
  35. .. image:: img/signals_shoot1.gif
  36. Instead, we need the bullets to be independent of the player's movement - once
  37. fired, they should continue traveling in a straight line and the player can no
  38. longer affect them. Instead of being added to the scene tree as a child of the
  39. player, it makes more sense to add the bullet as a child of the "main" game
  40. scene, which may be the player's parent or even further up the tree.
  41. You could do this by adding the bullet to the main scene directly:
  42. .. tabs::
  43. .. code-tab:: gdscript GDScript
  44. var bullet_instance = Bullet.instance()
  45. get_parent().add_child(bullet_instance)
  46. .. code-tab:: csharp
  47. Node bulletInstance = Bullet.Instance();
  48. GetParent().AddChild(bulletInstance);
  49. However, this will lead to a different problem. Now if you try to test your
  50. "Player" scene independently, it will crash on shooting, because there is no
  51. parent node to access. This makes it a lot harder to test your player code
  52. independently and also means that if you decide to change your main scene's
  53. node structure, the player's parent may no longer be the appropriate node to
  54. receive the bullets.
  55. The solution to this is to use a signal to "emit" the bullets from the player.
  56. The player then has no need to "know" what happens to the bullets after that -
  57. whatever node is connected to the signal can "receive" the bullets and take the
  58. appropriate action to spawn them.
  59. Here is the code for the player using signals to emit the bullet:
  60. .. tabs::
  61. .. code-tab:: gdscript GDScript
  62. extends Sprite
  63. signal shoot(bullet, direction, location)
  64. var Bullet = preload("res://Bullet.tscn")
  65. func _input(event):
  66. if event is InputEventMouseButton:
  67. if event.button_index == BUTTON_LEFT and event.pressed:
  68. emit_signal("shoot", Bullet, rotation, position)
  69. func _process(delta):
  70. look_at(get_global_mouse_position())
  71. .. code-tab:: csharp
  72. public class Player : Sprite
  73. {
  74. [Signal]
  75. delegate void Shoot(PackedScene bullet, Vector2 direction, Vector2 location);
  76. private PackedScene _bullet = GD.Load<PackedScene>("res://Bullet.tscn");
  77. public override void _Input(InputEvent event)
  78. {
  79. if (input is InputEventMouseButton mouseButton)
  80. {
  81. if (mouseButton.ButtonIndex == (int)ButtonList.Left && mouseButton.Pressed)
  82. {
  83. EmitSignal(nameof(Shoot), _bullet, Rotation, Position);
  84. }
  85. }
  86. }
  87. public override _Process(float delta)
  88. {
  89. LookAt(GetGlobalMousePosition());
  90. }
  91. }
  92. In the main scene, we then connect the player's signal (it will appear in the
  93. "Node" tab).
  94. .. tabs::
  95. .. code-tab:: gdscript GDScript
  96. func _on_Player_shoot(Bullet, direction, location):
  97. var b = Bullet.instance()
  98. add_child(b)
  99. b.rotation = direction
  100. b.position = location
  101. b.velocity = b.velocity.rotated(direction)
  102. .. code-tab:: csharp
  103. public void _on_Player_Shoot(PackedScene bullet, Vector2 direction, Vector2 location)
  104. {
  105. var bulletInstance = (Bullet)bullet.Instance();
  106. AddChild(bulletInstance);
  107. bulletInstance.Rotation = direction;
  108. bulletInstance.Position = location;
  109. bulletInstance.Velocity = bulletInstance.Velocity.Rotated(direction);
  110. }
  111. Now the bullets will maintain their own movement independent of the player's
  112. rotation:
  113. .. image:: img/signals_shoot2.gif