Generating InputEvent.ACTION from script, does not affect _fixed_process

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By T Lang
:warning: Old Version Published before Godot 3 was released.

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.

:bust_in_silhouette: Reply From: jospic

_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

:bust_in_silhouette: Reply From: Hinsbart

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")

Awesome, that did the trick!

And the code looks a lot cleaner as well :slight_smile:

Thank you!

T Lang | 2016-09-26 20:10