Input(event) and fixed_process(delta)

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

Hi!
So, there is this thing when you can use input(event) to get single signal of (pressed)&(released) inputs, but what if i want to make that as an input for movement?
Can you connect “func” between multiple “func” or call a variable, get a signal that an input was pressed in "input(event) " and then execute it in “fixed_process(delta)”?
I hope this doesnt sound too confusing~

:bust_in_silhouette: Reply From: eons

Yes, sometimes you need to do things that way to get more control over input events and not just for movement.

One way, managing variables on _input (always with set_process_input and set_fixed_process true):

var movement = Vector2()

func _input(event):
 if event.type == InputEvent.KEY && !event.is_echo():
    if event.is_action_pressed("left"):
        movement.x = -1
    elif event.is_action_released("left"):
        movement.x = 0

func _fixed_process(delta):
  move(speed*movement*delta)

Another, clearing variables on process:

var movement = Vector2()

func _input(event):
 if event.type == InputEvent.KEY:
    if event.is_action_pressed("left"):
        movement.x = -1

func _fixed_process(delta):
  move(speed*movement*delta)
  movement.x = 0

What works better depends on the game, like, grid based movement may get better result with the second, doing extra position checks before clearing the movement variable and ignoring input event while moving.

:bust_in_silhouette: Reply From: avencherus

Not entirely clear on what you’re saying. But you can also actively poll for input using the Input singleton at anytime during process loops.

Such as:

extends Node2D

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
	# or actions: Input.is_action_just_pressed("ui_up")
	if(Input.is_key_pressed(KEY_UP)):  
		print("UP is being pressed.")

problem with this one is that action is pressed “delta” times… what i meant is that it checks was it presed in input func (once)
func _input(event):
if event.is_action_pressed(…
and when its pressed (one time) then somehow affect fixed process func that executes delta times per second u know…

Serenade | 2016-11-16 23:18

Sadly, the is_action_just_* was not added to the 2.1.1 (has some problems).

But there are ways to do it with just Input too (from simple flags to more complex structures), search in the Q&A, there are many answers to that if it is what you are looking for.

eons | 2016-11-19 03:13