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)
    get_tree().input_event(ev)

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.

asked Sep 25, 2016 in Engine by T Lang (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)
    get_tree().input_event(ev)

use:

func _on_RightButton_pressed():
    Input.action_press("move_right")

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

answered Sep 26, 2016 by Hinsbart (896 points)
selected Sep 26, 2016 by T Lang

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.

-j

answered Sep 26, 2016 by jospic (1,415 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 How to use this Q&A? before posting your first questions.