scripting_player_input.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. :article_outdated: True
  2. .. Intention: only introduce one necessary input method at this point. The
  3. Inputs section of the docs should provide more guides comparing the various
  4. tools you have to manage the complexity of user input.
  5. .. _doc_scripting_player_input:
  6. Listening to player input
  7. =========================
  8. Building upon the previous lesson :ref:`doc_scripting_first_script`, let's look
  9. at another important feature of any game: giving control to the player.
  10. To add this, we need to modify our ``sprite_2d.gd`` code.
  11. .. image:: img/scripting_first_script_moving_with_input.gif
  12. You have two main tools to process the player's input in Godot:
  13. 1. The built-in input callbacks, mainly ``_unhandled_input()``. Like
  14. ``_process()``, it's a built-in virtual function that Godot calls every time
  15. the player presses a key. It's the tool you want to use to react to events
  16. that don't happen every frame, like pressing :kbd:`Space` to jump. To learn
  17. more about input callbacks, see :ref:`doc_inputevent`.
  18. 2. The ``Input`` singleton. A singleton is a globally accessible object. Godot
  19. provides access to several in scripts. It's the right tool to check for input
  20. every frame.
  21. We're going to use the ``Input`` singleton here as we need to know if the player
  22. wants to turn or move every frame.
  23. For turning, we should use a new variable: ``direction``. In our ``_process()``
  24. function, replace the ``rotation += angular_speed * delta`` line with the
  25. code below.
  26. .. tabs::
  27. .. code-tab:: gdscript GDScript
  28. var direction = 0
  29. if Input.is_action_pressed("ui_left"):
  30. direction = -1
  31. if Input.is_action_pressed("ui_right"):
  32. direction = 1
  33. rotation += angular_speed * direction * delta
  34. .. code-tab:: csharp C#
  35. var direction = 0;
  36. if (Input.IsActionPressed("ui_left"))
  37. {
  38. direction = -1;
  39. }
  40. if (Input.IsActionPressed("ui_right"))
  41. {
  42. direction = 1;
  43. }
  44. Rotation += _angularSpeed * direction * (float)delta;
  45. Our ``direction`` local variable is a multiplier representing the direction in
  46. which the player wants to turn. A value of ``0`` means the player isn't pressing
  47. the left or the right arrow key. A value of ``1`` means the player wants to turn
  48. right, and ``-1`` means they want to turn left.
  49. To produce these values, we introduce conditions and the use of ``Input``. A
  50. condition starts with the ``if`` keyword in GDScript and ends with a colon. The
  51. condition is the expression between the keyword and the end of the line.
  52. To check if a key was pressed this frame, we call ``Input.is_action_pressed()``.
  53. The method takes a text string representing an input action and returns ``true``
  54. if the action is pressed, ``false`` otherwise.
  55. The two actions we use above, "ui_left" and "ui_right", are predefined in every
  56. Godot project. They respectively trigger when the player presses the left and
  57. right arrows on the keyboard or left and right on a gamepad's D-pad.
  58. .. note:: You can see and edit input actions in your project by going to Project
  59. -> Project Settings and clicking on the Input Map tab.
  60. Finally, we use the ``direction`` as a multiplier when we update the node's
  61. ``rotation``: ``rotation += angular_speed * direction * delta``.
  62. If you run the scene with this code, the icon should rotate when you press
  63. :kbd:`Left` and :kbd:`Right`.
  64. Moving when pressing "up"
  65. -------------------------
  66. To only move when pressing a key, we need to modify the code that calculates the
  67. velocity. Replace the line starting with ``var velocity`` with the code below.
  68. .. tabs::
  69. .. code-tab:: gdscript GDScript
  70. var velocity = Vector2.ZERO
  71. if Input.is_action_pressed("ui_up"):
  72. velocity = Vector2.UP.rotated(rotation) * speed
  73. .. code-tab:: csharp C#
  74. var velocity = Vector2.Zero;
  75. if (Input.IsActionPressed("ui_up"))
  76. {
  77. velocity = Vector2.Up.Rotated(Rotation) * _speed;
  78. }
  79. We initialize the ``velocity`` with a value of ``Vector2.ZERO``, another
  80. constant of the built-in ``Vector`` type representing a 2D vector of length 0.
  81. If the player presses the "ui_up" action, we then update the velocity's value,
  82. causing the sprite to move forward.
  83. Complete script
  84. ---------------
  85. Here is the complete ``sprite_2d.gd`` file for reference.
  86. .. tabs::
  87. .. code-tab:: gdscript GDScript
  88. extends Sprite2D
  89. var speed = 400
  90. var angular_speed = PI
  91. func _process(delta):
  92. var direction = 0
  93. if Input.is_action_pressed("ui_left"):
  94. direction = -1
  95. if Input.is_action_pressed("ui_right"):
  96. direction = 1
  97. rotation += angular_speed * direction * delta
  98. var velocity = Vector2.ZERO
  99. if Input.is_action_pressed("ui_up"):
  100. velocity = Vector2.UP.rotated(rotation) * speed
  101. position += velocity * delta
  102. .. code-tab:: csharp C#
  103. using Godot;
  104. public partial class Sprite : Sprite2D
  105. {
  106. private float _speed = 400;
  107. private float _angularSpeed = Mathf.Pi;
  108. public override void _Process(double delta)
  109. {
  110. var direction = 0;
  111. if (Input.IsActionPressed("ui_left"))
  112. {
  113. direction = -1;
  114. }
  115. if (Input.IsActionPressed("ui_right"))
  116. {
  117. direction = 1;
  118. }
  119. Rotation += _angularSpeed * direction * (float)delta;
  120. var velocity = Vector2.Zero;
  121. if (Input.IsActionPressed("ui_up"))
  122. {
  123. velocity = Vector2.Up.Rotated(Rotation) * _speed;
  124. }
  125. Position += velocity * (float)delta;
  126. }
  127. }
  128. If you run the scene, you should now be able to rotate with the left and right
  129. arrow keys and move forward by pressing :kbd:`Up`.
  130. .. image:: img/scripting_first_script_moving_with_input.gif
  131. Summary
  132. -------
  133. In summary, every script in Godot represents a class and extends one of the
  134. engine's built-in classes. The node types your classes inherit from give you
  135. access to properties like ``rotation`` and ``position`` in our sprite's case.
  136. You also inherit many functions, which we didn't get to use in this example.
  137. In GDScript, the variables you put at the top of the file are your class's
  138. properties, also called member variables. Besides variables, you can define
  139. functions, which, for the most part, will be your classes' methods.
  140. Godot provides several virtual functions you can define to connect your class
  141. with the engine. These include ``_process()``, to apply changes to the node
  142. every frame, and ``_unhandled_input()``, to receive input events like key and
  143. button presses from the users. There are quite a few more.
  144. The ``Input`` singleton allows you to react to the players' input anywhere in
  145. your code. In particular, you'll get to use it in the ``_process()`` loop.
  146. In the next lesson :ref:`doc_signals`, we'll build upon the relationship between
  147. scripts and nodes by having our nodes trigger code in scripts.