{"author_link":"\/users\/tricky-fat-cat","author_name":"Tricky_Fat_Cat","author_uid":"tricky-fat-cat","comments":[],"epoch":1571156027,"event":"LD45","format":"md","ldjam_node_id":175836,"likes":2,"metadata":{"p_key":"137257","p_author":"Tricky_Fat_Cat","p_authorkey":"1145628","p_urlkey":"353533","p_title":"Input method manager","p_cat":"LDJam ","p_event":"LD45","p_time":"1571156027","p_likes":"2","p_comments":"0","p_status":"WAYBACK","us_key":"1145628","us_name":"Artyom Volkov","us_username":"artyom-volkov","event_start":"1570147200","event_key":"78","event_name":"Ludum Dare 45"},"node":{"_collation":{"body_sanitizer":"TextUtils::SanitizeHTML via existing importer","event":"LD45","removed_author":false},"_superparent":159347,"_trust":8,"author":145628,"body":"I have a strong notion, a game should always have gamepad and keyboard controls and a player shouldn't spend a lot of time on switching between them.\n\nThat's why I've made a simple input method manager which I used in [**HONK**](https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk). You should place this object in all rooms and it will change your input method on the go.\n\n# Create Event\n```\n\/\/ Create a list of gamepad slots\nconnectedGamepads = ds_list_create();\n\n\/\/ Set active gamepad\nglobal.ActiveGamepad = noone;\n\n\/\/ Setup input methods\nenum InputMethod\n{\n\tGamepad,\n\tKeyboardMouse\n}\n\n\/\/ Set current input\nglobal.CurrentInput = InputMethod.KeyboardMouse;\n\n\/\/ Set deadzone and threshhold for Xbox gamepad\nxboxAxisDeadzone = 0.25;\nxboxButtonThreshhold = 0.1;\n\n\/\/ Set deadzone and threshhold for PS4 gamepad\nps4AxisDeadzone = 0.2;\nps4ButtonThreshhold = 0.1;\n```\n\n# Clean Up event\n```\nds_list_destroy(connectedGamepads);\n```\n\n# Begin step\n```\n\/\/\/ @description InputSwitcher\n\nvar _listSize = ds_list_size(connectedGamepads);\n\nif (_listSize != 0)\n{\n\tfor (var i = 0; i < _listSize; i++)\n\t{\n\t\tvar _slot = connectedGamepads[| i];\n\t\t\n\t\tvar _leftStickV = gamepad_axis_value(_slot, gp_axislv) != 0;\n\t\tvar _leftStickH = gamepad_axis_value(_slot, gp_axislh) != 0;\n\t\tvar _rightStickV = gamepad_axis_value(_slot, gp_axisrv) != 0;\n\t\tvar _rightStickH = gamepad_axis_value(_slot, gp_axisrh) != 0;\n\n\t\tfor (var k = gp_face1; k < gp_axisrv; k++) \n\t\t{\n\t\t\tif (gamepad_button_check_pressed(_slot, k)) \n\t\t\t{\n\t\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t\t\tset_active_gamepad(_slot);\n\t\t\t}\n\t\t}\n\n\t\tif (_leftStickV && _leftStickH) || (_rightStickV && _rightStickH)\n\t\t{\n\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t\tset_active_gamepad(_slot);\n\t\t}\n\t}\n\t\n\tif (global.CurrentInput != InputMethod.KeyboardMouse)\n\t{\n\t\tvar _mouseKeyPressed = mouse_check_button_pressed(mb_any);\n\t\tvar _keyboardKeyPressed = keyboard_check_pressed(vk_anykey);\n\t\t\n\t\tif (_mouseKeyPressed || _keyboardKeyPressed)\n\t\t{\n\t\t\tglobal.CurrentInput = InputMethod.KeyboardMouse;\n\t\t}\n\t}\n}\n```\n\n# Async - System event\n```\n\/\/\/ @description GamepadConnectionHandler\n\nswitch(async_load[? \"event_type\"])          \n{\n\tcase \"gamepad discovered\":\n\t    var _gamepadSlot = async_load[? \"pad_index\"];\n\t\t\n\t\tif (gamepad_is_supported())\n\t\t{\n\t\t\tds_list_add(connectedGamepads, _gamepadSlot);\n\t\t\tset_active_gamepad(_gamepadSlot);\n\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t}\n\tbreak;\n   \n\tcase \"gamepad lost\":\n\t\tvar _gamepadSlot = async_load[? \"pad_index\"];\n\t\tvar _gamepadListIndex = ds_list_find_index(connectedGamepads, _gamepadSlot);\n\n\t\tds_list_delete(connectedGamepads, _gamepadListIndex);\n\t\t\n\t\tvar _listSize = ds_list_size(connectedGamepads);\n\t\t\n\t\tif (_listSize == 0)\n\t\t{\n\t\t\tglobal.ActiveGamepad = noone;\n\t\t\tglobal.CurrentInput = InputMethod.KeyboardMouse;\n\t\t}\n\t\telse if (_gamepadSlot == global.ActiveGamepad)\n\t\t{\n\t\t\tglobal.ActiveGamepad = connectedGamepads[| 0];\n\t\t}\n\tbreak;\n}\n```\n\n# set_active_gamepad script\n```\n\/\/ @param gamepadSlot\n\nvar _gamepadSlot = argument0;\n\nglobal.ActiveGamepad = _gamepadSlot;\nif (_gamepadSlot >= 0 && _gamepadSlot <= 3) \/\/ Set dead zones for Xbox controller\n{\n\tgamepad_set_axis_deadzone(_gamepadSlot, xboxAxisDeadzone);\n\tgamepad_set_button_threshold(_gamepadSlot, xboxButtonThreshhold);\n}\nelse \/\/ Set dead zones for Playstation controller\n{\n\tgamepad_set_axis_deadzone(_gamepadSlot, ps4AxisDeadzone);\n\tgamepad_set_button_threshold(_gamepadSlot, ps4ButtonThreshhold);\n}\n```","comments":0,"created":"2019-10-15T15:17:15Z","files":[],"files-timestamp":0,"id":175836,"love":2,"love-timestamp":"2019-10-16T03:23:25Z","meta":[],"modified":"2019-10-16T03:23:25Z","name":"Input method manager","node-timestamp":"2019-10-15T16:13:47Z","parent":160945,"parents":[1,5,9,159347,160945],"path":"\/events\/ludum-dare\/45\/honk\/input-method-manager","published":"2019-10-15T16:13:47Z","scope":"public","slug":"input-method-manager","subsubtype":"","subtype":"","type":"post","version":530229},"node_metadata":{"n_key":"175836","n_urlkey":"353533","n_parent":"160945","n_path":"\/events\/ludum-dare\/45\/honk\/input-method-manager","n_slug":"input-method-manager","n_type":"post","n_subtype":"","n_subsubtype":"","n_author":"145628","n_created":"1571152635","n_modified":"1571196205","n_version":"530229","n_status":"WAYBACK"},"source_url":"https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk\/input-method-manager","text":"I have a strong notion, a game should always have gamepad and keyboard controls and a player shouldn't spend a lot of time on switching between them.\n\nThat's why I've made a simple input method manager which I used in [**HONK**](https:\/\/ldjam.com\/events\/ludum-dare\/45\/honk). You should place this object in all rooms and it will change your input method on the go.\n\n# Create Event\n```\n\/\/ Create a list of gamepad slots\nconnectedGamepads = ds_list_create();\n\n\/\/ Set active gamepad\nglobal.ActiveGamepad = noone;\n\n\/\/ Setup input methods\nenum InputMethod\n{\n\tGamepad,\n\tKeyboardMouse\n}\n\n\/\/ Set current input\nglobal.CurrentInput = InputMethod.KeyboardMouse;\n\n\/\/ Set deadzone and threshhold for Xbox gamepad\nxboxAxisDeadzone = 0.25;\nxboxButtonThreshhold = 0.1;\n\n\/\/ Set deadzone and threshhold for PS4 gamepad\nps4AxisDeadzone = 0.2;\nps4ButtonThreshhold = 0.1;\n```\n\n# Clean Up event\n```\nds_list_destroy(connectedGamepads);\n```\n\n# Begin step\n```\n\/\/\/ @description InputSwitcher\n\nvar _listSize = ds_list_size(connectedGamepads);\n\nif (_listSize != 0)\n{\n\tfor (var i = 0; i < _listSize; i++)\n\t{\n\t\tvar _slot = connectedGamepads[| i];\n\t\t\n\t\tvar _leftStickV = gamepad_axis_value(_slot, gp_axislv) != 0;\n\t\tvar _leftStickH = gamepad_axis_value(_slot, gp_axislh) != 0;\n\t\tvar _rightStickV = gamepad_axis_value(_slot, gp_axisrv) != 0;\n\t\tvar _rightStickH = gamepad_axis_value(_slot, gp_axisrh) != 0;\n\n\t\tfor (var k = gp_face1; k < gp_axisrv; k++) \n\t\t{\n\t\t\tif (gamepad_button_check_pressed(_slot, k)) \n\t\t\t{\n\t\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t\t\tset_active_gamepad(_slot);\n\t\t\t}\n\t\t}\n\n\t\tif (_leftStickV && _leftStickH) || (_rightStickV && _rightStickH)\n\t\t{\n\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t\tset_active_gamepad(_slot);\n\t\t}\n\t}\n\t\n\tif (global.CurrentInput != InputMethod.KeyboardMouse)\n\t{\n\t\tvar _mouseKeyPressed = mouse_check_button_pressed(mb_any);\n\t\tvar _keyboardKeyPressed = keyboard_check_pressed(vk_anykey);\n\t\t\n\t\tif (_mouseKeyPressed || _keyboardKeyPressed)\n\t\t{\n\t\t\tglobal.CurrentInput = InputMethod.KeyboardMouse;\n\t\t}\n\t}\n}\n```\n\n# Async - System event\n```\n\/\/\/ @description GamepadConnectionHandler\n\nswitch(async_load[? \"event_type\"])          \n{\n\tcase \"gamepad discovered\":\n\t    var _gamepadSlot = async_load[? \"pad_index\"];\n\t\t\n\t\tif (gamepad_is_supported())\n\t\t{\n\t\t\tds_list_add(connectedGamepads, _gamepadSlot);\n\t\t\tset_active_gamepad(_gamepadSlot);\n\t\t\tglobal.CurrentInput = InputMethod.Gamepad;\n\t\t}\n\tbreak;\n   \n\tcase \"gamepad lost\":\n\t\tvar _gamepadSlot = async_load[? \"pad_index\"];\n\t\tvar _gamepadListIndex = ds_list_find_index(connectedGamepads, _gamepadSlot);\n\n\t\tds_list_delete(connectedGamepads, _gamepadListIndex);\n\t\t\n\t\tvar _listSize = ds_list_size(connectedGamepads);\n\t\t\n\t\tif (_listSize == 0)\n\t\t{\n\t\t\tglobal.ActiveGamepad = noone;\n\t\t\tglobal.CurrentInput = InputMethod.KeyboardMouse;\n\t\t}\n\t\telse if (_gamepadSlot == global.ActiveGamepad)\n\t\t{\n\t\t\tglobal.ActiveGamepad = connectedGamepads[| 0];\n\t\t}\n\tbreak;\n}\n```\n\n# set_active_gamepad script\n```\n\/\/ @param gamepadSlot\n\nvar _gamepadSlot = argument0;\n\nglobal.ActiveGamepad = _gamepadSlot;\nif (_gamepadSlot >= 0 && _gamepadSlot <= 3) \/\/ Set dead zones for Xbox controller\n{\n\tgamepad_set_axis_deadzone(_gamepadSlot, xboxAxisDeadzone);\n\tgamepad_set_button_threshold(_gamepadSlot, xboxButtonThreshhold);\n}\nelse \/\/ Set dead zones for Playstation controller\n{\n\tgamepad_set_axis_deadzone(_gamepadSlot, ps4AxisDeadzone);\n\tgamepad_set_button_threshold(_gamepadSlot, ps4ButtonThreshhold);\n}\n```","title":"Input method manager","wayback_source":[]}