scripting_player_input.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, let's look at another important feature of
  8. any game: giving control to the player. To add this, we need to modify our code.
  9. .. image:: img/scripting_first_script_moving_with_input.gif
  10. You have two main tools to process the player's input in Godot:
  11. 1. The built-in input callbacks, mainly ``_unhandled_input()``. Like
  12. ``_process()``, it's a built-in virtual function that Godot calls every time
  13. the player presses a key. It's the tool you want to use to react to events
  14. that don't happen every frame, like pressing :kbd:`Space` to jump. To learn
  15. more about input callbacks, see :ref:`doc_inputevent`.
  16. 2. The ``Input`` singleton. A singleton is a globally accessible object. Godot
  17. provides access to several in scripts. It's the right tool to check for input
  18. every frame.
  19. We're going to use the ``Input`` singleton here as we need to know if the player
  20. wants to turn or move every frame.
  21. For turning, we should use a new variable: ``direction``. Update the top of the
  22. ``_process()`` function like so, up to the line where we increment the sprite's
  23. ``rotation``.
  24. .. tabs::
  25. .. code-tab:: gdscript GDScript
  26. func _process(delta):
  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. position += velocity * delta
  62. We initialize the ``velocity`` with a value of ``Vector2.ZERO``, another
  63. constant of the built-in ``Vector`` type representing a 2D vector of length 0.
  64. If the player presses the "ui_up" action, we then update the velocity's value,
  65. causing the sprite to move forward.
  66. Here is the complete ``Sprite.gd`` file for reference.
  67. .. tabs::
  68. .. code-tab:: gdscript GDScript
  69. extends Sprite
  70. var speed = 400
  71. var angular_speed = PI
  72. func _process(delta):
  73. var direction = 0
  74. if Input.is_action_pressed("ui_left"):
  75. direction = -1
  76. if Input.is_action_pressed("ui_right"):
  77. direction = 1
  78. rotation += angular_speed * direction * delta
  79. var velocity = Vector2.ZERO
  80. if Input.is_action_pressed("ui_up"):
  81. velocity = Vector2.UP.rotated(rotation) * speed
  82. position += velocity * delta
  83. If you run the scene, you should now be able to rotate with the left and right
  84. arrow keys and move forward by pressing :kbd:`Up`.
  85. .. image:: img/scripting_first_script_moving_with_input.gif
  86. Summary
  87. -------
  88. In summary, every script in Godot represents a class and extends one of the
  89. engine's built-in classes. The node types your classes inherit from give you
  90. access to properties like ``rotation`` and ``position`` in our sprite's case.
  91. You also inherit many functions, which we didn't get to use in this example.
  92. In GDScript, the variables you put at the top of the file are your class's
  93. properties, also called member variables. Besides variables, you can define
  94. functions, which, for the most part, will be your classes' methods.
  95. Godot provides several virtual functions you can define to connect your class
  96. with the engine. These include ``_process()``, to apply changes to the node
  97. every frame, and ``_unhandled_input()``, to receive input events like key and
  98. button presses from the users. There are quite a few more.
  99. The ``Input`` singleton allows you to react to the players' input anywhere in
  100. your code. In particular, you'll get to use it in the ``_process()`` loop.