Created
September 29, 2023 03:40
-
-
Save kennethrapp/302bd14b4397687e6e31e268b3c266ea to your computer and use it in GitHub Desktop.
Godot custom input map loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[move_up] | |
axis="LEFT_Y" | |
axis_value=-1.0 | |
key=["w", "UP"] | |
[move_down] | |
axis="LEFT_Y" | |
axis_value=1.0 | |
key=["s","DOWN"] | |
[move_left] | |
axis="LEFT_X" | |
axis_value=-1.0 | |
key=["a","LEFT"] | |
[move_right] | |
axis="LEFT_X" | |
axis_value=1.0 | |
key=["d","RIGHT"] | |
[sprint] | |
button = "BUTTON_LEFT_STICK" | |
key="SHIFT" | |
[crouch] | |
button = "BUTTON_RIGHT_STICK" | |
key="CTRL" | |
[jump] | |
button = "JOY_BUTTON_X" | |
key="SPACEBAR" | |
[kick] | |
button = "JOY_BUTTON_A" | |
key="f" | |
[look_left] | |
axis="RIGHT_Y" | |
axis_value=-1.0 | |
[look_right] | |
axis="RIGHT_Y" | |
axis_value=1.0 | |
[look_up] | |
axis="RIGHT_X" | |
axis_value=-1.0 | |
[look_down] | |
axis="RIGHT_X" | |
axis_value=1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#no class name because it's a singleton | |
extends Node | |
#set as an autoload singleton GlobalInputMap | |
#then call GlobalInputMap.load_input_map(path_to_map) | |
#in the ready function of the player | |
var input_map :ConfigFile = ConfigFile.new() | |
var const_map:Dictionary = { | |
"AXIS_INVALID": JOY_AXIS_INVALID, | |
"LEFT_X": JOY_AXIS_LEFT_X, | |
"LEFT_Y": JOY_AXIS_LEFT_Y, | |
"RIGHT_X": JOY_AXIS_RIGHT_X, | |
"RIGHT_Y": JOY_AXIS_RIGHT_Y, | |
"TRIGGER_LEFT": JOY_AXIS_TRIGGER_LEFT, | |
"TRIGGER_RIGHT": JOY_AXIS_TRIGGER_RIGHT, | |
"JOY_INVALID": JOY_BUTTON_INVALID, | |
"JOY_BUTTON_A": JOY_BUTTON_A, | |
"JOY_BUTTON_B": JOY_BUTTON_B, | |
"JOY_BUTTON_X": JOY_BUTTON_X, | |
"JOY_BUTTON_Y": JOY_BUTTON_Y, | |
"BACK" : JOY_BUTTON_BACK, | |
"GUIDE" : JOY_BUTTON_GUIDE, | |
"START" : JOY_BUTTON_START, | |
"BUTTON_LEFT_STICK" : JOY_BUTTON_LEFT_STICK, | |
"BUTTON_RIGHT_STICK" : JOY_BUTTON_RIGHT_STICK, | |
"LEFT_SHOULDER" : JOY_BUTTON_LEFT_SHOULDER, | |
"RIGHT_SHOULDER" : JOY_BUTTON_RIGHT_SHOULDER, | |
"DPAD_UP" : JOY_BUTTON_DPAD_UP, | |
"DPAD_DOWN" : JOY_BUTTON_DPAD_DOWN, | |
"DPAD_LEFT" : JOY_BUTTON_DPAD_LEFT, | |
"DPAD_RIGHT" : JOY_BUTTON_DPAD_RIGHT, | |
"MISC1" : JOY_BUTTON_MISC1, | |
"PADDLE1" : JOY_BUTTON_PADDLE1, | |
"PADDLE2" : JOY_BUTTON_PADDLE2, | |
"PADDLE3" : JOY_BUTTON_PADDLE3, | |
"PADDLE4" : JOY_BUTTON_PADDLE4, | |
"TOUCHPAD" : JOY_BUTTON_TOUCHPAD | |
} | |
func load_input_map(config_path:String)->void: | |
var err:int = input_map.load(config_path) | |
var axis_input | |
var button_input | |
var key_input | |
var ev:InputEvent | |
if err: | |
pass | |
else: | |
# defaults can't be set to null | |
for action in input_map.get_sections(): | |
if not InputMap.has_action(action): | |
InputMap.add_action(action) | |
axis_input = input_map.get_value(action,"axis",false) | |
if const_map.has(axis_input): | |
ev=InputEventJoypadMotion.new() | |
ev.device = input_map.get_value(action,"device", 0) | |
ev.axis = const_map[axis_input] | |
ev.axis_value = input_map.get_value(action,"axis_value") | |
InputMap.action_add_event(action,ev) | |
button_input = input_map.get_value(action,"button",false) | |
if const_map.has(button_input): | |
ev = InputEventJoypadButton.new() | |
ev.device = input_map.get_value(action,"device",-1) | |
ev.button_index=const_map[button_input] | |
InputMap.action_add_event(action,ev) | |
key_input = input_map.get_value(action,"key",false) | |
if key_input is Array: | |
for key in key_input: | |
ev = InputEventKey.new() | |
ev.keycode = OS.find_keycode_from_string(key) | |
InputMap.action_add_event(action, ev) | |
elif key_input is String: | |
ev = InputEventKey.new() | |
ev.keycode=OS.find_keycode_from_string(key_input) | |
InputMap.action_add_event(action,ev) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment