![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | DogeMassaji |
How can I edit my InputMap with code? I cannot see any examples in the doc.Or there is some examples which I missed?
![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | DogeMassaji |
How can I edit my InputMap with code? I cannot see any examples in the doc.Or there is some examples which I missed?
![]() |
Reply From: | Xrayez |
Never used it but I think it’ll go like this:
func _ready():
InputMap.add_action("my_action")
InputMap.action_add_event("my_action", event)
See InputMap
singleton documentation for other methods on how to modify input map.
Thank you. But what I confused in is how to set an InputEvent binding a key like “KEY_A” so that when I press the “A” key it can feedback the action Input.is_action_pressed("A")
.
Here is the Input singleton of the document:
var ev = InputEventAction.new()
ev.set_as_action("move_left", true)
Input.parse_input_event(ev)
Still not understand how to do it.
DogeMassaji | 2018-07-10 15:55
![]() |
Reply From: | DogeMassaji |
OK. I got the answer in the forum.
var ev = InputEventKey.new()
ev.scancode = KEY_A
InputMap.add_action("ui_a")
InputMap.action_add_event("ui_a", ev)
![]() |
Reply From: | BananaBread |
Working on a plugin in Godot 4.0.1 so here is an update:
var key = InputEventKey.new() key.physical_keycode = KEY_W InputMap.add_action("__custom_action") InputMap.action_add_event("__custom_action", key)
In my editor plugin context it’s also advisable to clean up the action. But that’s probably not the case for a game.
InputMap.erase_action("__custom_action")
func _ready() -> void:
add_action_with_keycode("move_front", KEY_W)
add_action_with_keycode("move_back", KEY_S)
add_action_with_keycode("move_left", KEY_A)
add_action_with_keycode("move_right", KEY_D)
add_action_with_keycode("jump", KEY_SPACE)
func add_action_with_keycode(action, key):
var event = InputEventKey.new()
event.physical_keycode = key
InputMap.add_action(action)
InputMap.action_add_event(action, event)
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _ready() -> void:
add_action_with_keycode("move_front", KEY_W)
add_action_with_keycode("move_back", KEY_S)
add_action_with_keycode("move_left", KEY_A)
add_action_with_keycode("move_right", KEY_D)
add_action_with_keycode("jump", KEY_SPACE)
func add_action_with_keycode(action, key):
var event = InputEventKey.new()
event.physical_keycode = key
InputMap.add_action(action)
InputMap.action_add_event(action, event)
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("move_left", "move_right", "move_front", "move_back")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
References:
After launching the game it should add new inputs to the godot.project file.
However they are unrecognized by gdscript for some reason and probably need enforcement using previously discussedInputMap
. Update: apparently it is because I added@tool
aboveextends CharacterBody3D
and reloaded the current project. To resolve: simply don’t use@tool
in a CharacterBody3D.
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _ready():
setup_input_actions()
func setup_input_actions():
# Define your input actions here
add_action("jump", KEY_SPACE)
add_action("move_left", KEY_A)
add_action("move_right", KEY_D)
add_action("move_forward", KEY_W)
add_action("move_backward", KEY_S)
# Save to project.godot
ProjectSettings.save()
print("Input actions saved to Project Settings.")
func add_action(action_name: String, key_scancode: int):
var event := InputEventKey.new()
event.physical_keycode = key_scancode
var property := "input/%s" % action_name
var action: Dictionary = ProjectSettings.get_setting(property, { "deadzone": 0.5, "events": [] })
# Check for existing events to avoid duplicates
var event_exists := false
for existing_event in action["events"]:
if is_same_key_event(existing_event, event):
event_exists = true
break
if not event_exists:
action["events"].append(event)
ProjectSettings.set_setting(property, action)
# Update InputMap for immediate use in the editor
if not InputMap.has_action(action_name):
InputMap.add_action(action_name)
InputMap.action_add_event(action_name, event)
func is_same_key_event(a: InputEvent, b: InputEvent) -> bool:
if a is InputEventKey and b is InputEventKey:
return a.physical_keycode == b.physical_keycode
return false
func add_action_with_keycode(action, key):
var event = InputEventKey.new()
event.physical_keycode = key
InputMap.add_action(action)
InputMap.action_add_event(action, event)
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()