2
0

controller_features.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. .. _doc_controller_features:
  2. Controller features
  3. ===================
  4. Godot supports controller-specific features that can further enhance the gameplay
  5. experience. This page describes these features, how existing games have used them,
  6. and how you can get started with them in Godot.
  7. .. warning::
  8. These controller features are currently only supported on Windows, macOS, and Linux.
  9. .. warning::
  10. Unless you specifically advertise your game as requiring specific controllers,
  11. remember that there is no guarantee that players will have a controller with
  12. any given features.
  13. As a result, we suggest using these features to enhance the gameplay experience
  14. for players whose controllers support them, without detracting from those who
  15. don't have controllers.
  16. LED color
  17. ---------
  18. Games can use the LED lights on certain controllers to subtly complement the on-screen gameplay by
  19. providing some matching visuals in the player's hands. Here are some notable examples:
  20. - In *Hades*, the color of the light matches the god you're receiving a boon from.
  21. - In *Resident Evil 2*, the color of the light indicates your health (green for full, yellow for medium, red for low).
  22. - In *Star Wars Jedi: Fallen Order*, the color of the light matches your lightsaber's color.
  23. Use the method :ref:`Input.set_joy_light()<class_Input_method_set_joy_light>` to set the color
  24. of a given controller's LEDs.
  25. To determine if a given controller supports setting LED lights, use the :ref:`Input.has_joy_light()<class_Input_method_has_joy_light>`
  26. method. The PlayStation DualShock and DualSense controllers are known to support LED lights.
  27. The following ``_process()`` method sets the LED color according to the currently pressed button,
  28. and turns it off if no button is being pressed:
  29. .. code-block::
  30. func _process(_delta):
  31. var color := Color.BLACK
  32. if Input.is_joy_button_pressed(0, JOY_BUTTON_A):
  33. color = Color.BLUE
  34. elif Input.is_joy_button_pressed(0, JOY_BUTTON_X):
  35. color = Color.MAGENTA
  36. elif Input.is_joy_button_pressed(0, JOY_BUTTON_B):
  37. color = Color.RED
  38. elif Input.is_joy_button_pressed(0, JOY_BUTTON_Y):
  39. color = Color.GREEN
  40. Input.set_joy_light(0, color)
  41. The following example smoothly fades the LED through hues in a loop:
  42. .. code-block::
  43. var hue = 0.0
  44. func _process(delta):
  45. var col = Color.from_hsv(hue, 1.0, 1.0)
  46. Input.set_joy_light(0, col)
  47. hue += delta * 0.1
  48. The following example makes the LED blink red three times when the south button (Cross/X on PlayStation controllers) is pressed:
  49. .. code-block::
  50. var blink_tween: Tween = null
  51. func _process(_delta):
  52. var ready_to_blink = not blink_tween or not blink_tween.is_running()
  53. if Input.is_joy_button_pressed(0, JOY_BUTTON_A) and ready_to_blink:
  54. do_blink()
  55. func do_blink():
  56. if blink_tween:
  57. blink_tween.kill()
  58. blink_tween = create_tween()
  59. blink_tween.tween_callback(func(): Input.set_joy_light(0, Color.RED))
  60. blink_tween.tween_interval(0.2)
  61. blink_tween.tween_callback(func(): Input.set_joy_light(0, Color.BLACK))
  62. blink_tween.tween_interval(0.2)
  63. blink_tween.set_loops(3)