The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I'm trying to make button-controls for my character.
The character is a KinematicBody2d and moved using the _fixed_process(delta).

Everything works as expected when using input_map and mapping keys, and using is_action_pressed, but when trying to programatically create an action, it does not work.

I want to add a button, so when clicked, it fires the same action as the key-input.
In the button:

func _on_RightButton_pressed():
    var ev = InputEvent()
    ev.type = InputEvent.ACTION
    ev.set_as_action("move_right", true)

I can see the action in the _input-function of my character, but the _fixed_process does not catch the action.

From my character:

func _fixed_process(delta):
    if Input.is_action_pressed("move_right"):
        print("Move Right")

func _input(event):
    if not event.is_echo() && event.is_pressed():
        if event.is_action("move_right"):
            print("Move Right")

So..what is the difference between an action set using Input Map (prints "Move Right" from both _fixed_process and _input) and explicit defining an action in code (only prints "Move Right" from _input)

What is it I'm not understanding here?
From other examples I've seen that some move their character directly in the _input-function, and that would solve my problem, but still I don't get the difference between setting an action in Input Map and using a script.

in Engine by (15 points)

2 Answers

+1 vote
Best answer

This question came up on discord a few days ago, and it seems like sending an InputEvent down the SceneTree to generate an Input Action is broken right now.
Luckily there's another way to do this (which is also easier).
Instead of:

func _on_RightButton_pressed():
    var ev = InputEvent()
    ev.type = InputEvent.ACTION
    ev.set_as_action("move_right", true)


func _on_RightButton_pressed():

later on, when you need to release the action, use Input.action_release("move_right")

by (922 points)
selected by

Awesome, that did the trick!

And the code looks a lot cleaner as well :)

Thank you!

0 votes

_fixed_process is called every physic frame and probably not always capture the key event.
Maybe you should use _processthat is called every frame.


by (1,488 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.