{"author_link":"\/users\/tricky-fat-cat","author_name":"Tricky_Fat_Cat","author_uid":"tricky-fat-cat","comments":[],"epoch":1602236841,"event":"LD47","format":"md","ldjam_node_id":230444,"likes":4,"metadata":{"p_key":"151018","p_author":"Tricky_Fat_Cat","p_authorkey":"1145628","p_urlkey":"367523","p_title":"Simple input manager for Godot","p_cat":"LDJam ","p_event":"LD47","p_time":"1602236841","p_likes":"4","p_comments":"0","p_status":"WAYBACK","us_key":"1145628","us_name":"Artyom Volkov","us_username":"artyom-volkov","event_start":"1601596800","event_key":"76","event_name":"Ludum Dare 47"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD47","removed_author":false},"_superparent":212256,"_trust":8,"author":145628,"body":"[**HONK II**](https:\/\/ldjam.com\/events\/ludum-dare\/47\/honk-ii) has gamepad support, but I didn't manage to improve it during the jam. However, I have special input manager, which is very helpful when you need to switch between different input devices on the go and when some of your mechanics have different logic for different input method because of their specifics.\n\nMake this script autoload and enjoy. Hope, this code will be helpful.\n\n```\nextends Node\n\nenum InputDevice {\n\tKEYBOARD,\n\tGAMEPAD\n}\n\nconst JOY_DEADZONE : float = 0.3\nconst JOY_ID_DEFAULT : int = 0\n\nvar current_input_device : int = InputDevice.KEYBOARD\nvar joy_id_current : int = JOY_ID_DEFAULT\n\nfunc _input(event: InputEvent) -> void:\n\tif (event is InputEventKey or event is InputEventMouse or event is InputEventMouseMotion) and not is_current_input_keyboard():\n\t\tcurrent_input_device = InputDevice.KEYBOARD\n\t\tInput.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)\n\t\tEvents.emit_signal(\"input_device_changed\", current_input_device)\n\t\treturn\n\n\tif (event is InputEventJoypadButton or event is InputEventJoypadMotion) and not is_current_input_gamepad():\n\t\tif event is InputEventJoypadMotion and (get_analog_right_direction(event.device) != Vector2.ZERO or get_analog_left_direction(event.device) != Vector2.ZERO):\n\t\t\treturn\n\n\t\tjoy_id_current = event.device\n\t\tcurrent_input_device = InputDevice.GAMEPAD\n\t\tInput.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)\n\t\tEvents.emit_signal(\"input_device_changed\", current_input_device)\n\t\treturn\n\n\n\nstatic func get_joy_analog_direction(joy_index: int, axis_x: int, axis_y: int) -> Vector2:\n\tif joy_index < 0:\n\t\tpush_error(\"Gamepad with index %s not found\" % joy_index)\n\t\treturn Vector2.ZERO\n\n\tvar joy_x = Input.get_joy_axis(joy_index, axis_x)\n\tvar joy_y = Input.get_joy_axis(joy_index, axis_y)\n\tvar joy_direction = Vector2(joy_x, joy_y)\n\n\tif joy_direction.length() <= JOY_DEADZONE:\n\t\tjoy_direction = Vector2.ZERO\n\n\treturn joy_direction\n\n\nstatic func get_analog_right_direction(joy_index: int = JOY_ID_DEFAULT) -> Vector2:\n\treturn get_joy_analog_direction(joy_index, JOY_ANALOG_RX, JOY_ANALOG_RY)\n\n\nstatic func get_analog_left_direction(joy_index: int = JOY_ID_DEFAULT) -> Vector2:\n\treturn get_joy_analog_direction(joy_index, JOY_ANALOG_LX, JOY_ANALOG_LY)\n\n\nfunc is_current_input_keyboard() -> bool:\n\treturn current_input_device == InputDevice.KEYBOARD\n\n\nfunc is_current_input_gamepad() -> bool:\n\treturn current_input_device == InputDevice.GAMEPAD\n```","comments":2,"comments-timestamp":"2020-10-09T10:00:09Z","created":"2020-10-09T09:32:57Z","files":[],"files-timestamp":0,"id":230444,"love":4,"love-timestamp":"2020-10-09T11:30:38Z","meta":[],"modified":"2020-10-09T11:30:38Z","name":"Simple input manager for Godot","node-timestamp":"2020-10-09T09:47:21Z","parent":212279,"parents":[1,5,9,212256,212279],"path":"\/events\/ludum-dare\/47\/honk-ii\/simple-input-manager-for-godot","published":"2020-10-09T09:47:21Z","scope":"public","slug":"simple-input-manager-for-godot","subsubtype":"","subtype":"","type":"post","version":701942},"node_metadata":{"n_key":"230444","n_urlkey":"367523","n_parent":"212279","n_path":"\/events\/ludum-dare\/47\/honk-ii\/simple-input-manager-for-godot","n_slug":"simple-input-manager-for-godot","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"145628","n_created":"1602235977","n_modified":"1602243038","n_version":"701942","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/47\/honk-ii\/simple-input-manager-for-godot","text":"[**HONK II**](https:\/\/ldjam.com\/events\/ludum-dare\/47\/honk-ii) has gamepad support, but I didn't manage to improve it during the jam. However, I have special input manager, which is very helpful when you need to switch between different input devices on the go and when some of your mechanics have different logic for different input method because of their specifics.\n\nMake this script autoload and enjoy. Hope, this code will be helpful.\n\n```\nextends Node\n\nenum InputDevice {\n\tKEYBOARD,\n\tGAMEPAD\n}\n\nconst JOY_DEADZONE : float = 0.3\nconst JOY_ID_DEFAULT : int = 0\n\nvar current_input_device : int = InputDevice.KEYBOARD\nvar joy_id_current : int = JOY_ID_DEFAULT\n\nfunc _input(event: InputEvent) -> void:\n\tif (event is InputEventKey or event is InputEventMouse or event is InputEventMouseMotion) and not is_current_input_keyboard():\n\t\tcurrent_input_device = InputDevice.KEYBOARD\n\t\tInput.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)\n\t\tEvents.emit_signal(\"input_device_changed\", current_input_device)\n\t\treturn\n\n\tif (event is InputEventJoypadButton or event is InputEventJoypadMotion) and not is_current_input_gamepad():\n\t\tif event is InputEventJoypadMotion and (get_analog_right_direction(event.device) != Vector2.ZERO or get_analog_left_direction(event.device) != Vector2.ZERO):\n\t\t\treturn\n\n\t\tjoy_id_current = event.device\n\t\tcurrent_input_device = InputDevice.GAMEPAD\n\t\tInput.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)\n\t\tEvents.emit_signal(\"input_device_changed\", current_input_device)\n\t\treturn\n\n\n\nstatic func get_joy_analog_direction(joy_index: int, axis_x: int, axis_y: int) -> Vector2:\n\tif joy_index < 0:\n\t\tpush_error(\"Gamepad with index %s not found\" % joy_index)\n\t\treturn Vector2.ZERO\n\n\tvar joy_x = Input.get_joy_axis(joy_index, axis_x)\n\tvar joy_y = Input.get_joy_axis(joy_index, axis_y)\n\tvar joy_direction = Vector2(joy_x, joy_y)\n\n\tif joy_direction.length() <= JOY_DEADZONE:\n\t\tjoy_direction = Vector2.ZERO\n\n\treturn joy_direction\n\n\nstatic func get_analog_right_direction(joy_index: int = JOY_ID_DEFAULT) -> Vector2:\n\treturn get_joy_analog_direction(joy_index, JOY_ANALOG_RX, JOY_ANALOG_RY)\n\n\nstatic func get_analog_left_direction(joy_index: int = JOY_ID_DEFAULT) -> Vector2:\n\treturn get_joy_analog_direction(joy_index, JOY_ANALOG_LX, JOY_ANALOG_LY)\n\n\nfunc is_current_input_keyboard() -> bool:\n\treturn current_input_device == InputDevice.KEYBOARD\n\n\nfunc is_current_input_gamepad() -> bool:\n\treturn current_input_device == InputDevice.GAMEPAD\n```","title":"Simple input manager for Godot","wayback_source":[]}