how can i detect wich action is pressed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Drachenbauer

Hello
I mapped the names of my four custom actions as strings with vector2 values for the four directions.

var dir = {"left":Vector2(-1, 0), "right":Vector2(1, 0), "up":Vector2(0, -1), "down":Vector2(0, 1)}

How can i now react on anything pressed and output the matching action to put it into a variable to do this (the variable, i want to store that, is key)?

    if step == 0:
        old_pos = position
        direction = dir[key]
    
    if grid.is_valid_move(pos, direction) || step > 0:
        move = true
        step += 1
        pos += STEP * direction
        	
        if step == 144:
            move = false
            step = 0
            pos = Vector2(round(pos.x), round(pos.y))
            grid.update_child_pos(self)
        
    position = pos
	
    else:
        direction = Vector2(0, 0)

For detecting inputs, have you looked at the _unhandled_input() or _input() functions?

Ertain | 2022-03-28 23:33

:bust_in_silhouette: Reply From: Noddy

In the _physics_process(delta) function you can put

if Input.is_action_pressed("action"):
#code

Or one of the many other is_action functions that are under Input. It’s pretty much up to you. Happy coding.

i thaught maybe there is a solution, that i don´t have to use such lines fou rach direction.
i thaught about a single line, that returns me, wich action is pressed, as a string variable, that i can use to take the correct content from the directions-dictionary-variable

Drachenbauer | 2022-03-31 13:25