scripting_player_input.rst 5.6 KB

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