0 votes

I created my custom action in Project -> Project Settings -> Input Map -> "customspace". I called it "uimovement", it contains the movement's keys(up, down, left, right).

This is the code: I want to store the exact value of the pressed key in the variable "direction" or maybe print the value

if Input.is_action_pressed("ui_movement"): 
   direction = the exact pressed key(Is it UP, DOWN, LEFT or RIGHT?)
   or
   print(VALUE OF THE PRESSED KEY)
asked Apr 10, 2019 in Engine by Emmanuele (14 points)

1 Answer

0 votes

Your question is a little bit confusing, and I don't think you're understanding how input actions work. You added a new input action to the Input Map. Did you name it "custom_space" or "ui_movement" (not sure why you'd name it "ui" by the way)?

If you added all four directional keys to the same action, then any of them will trigger the action. That's the purpose of actions - that any number of inputs can be mapped to a single action. is_action_pressed() then tells you that action occurred. If you want to capture separate events for different keys, then you need multiple actions.

You might find it helpful to read the following new tutorial that was recently added to the docs: https://docs.godotengine.org/en/latest/tutorials/inputs/input_examples.html

answered Apr 10, 2019 by kidscancode (17,164 points)

Sorry for the "customspace" string, I was wrong when I copied the path.
I have a sprite that moves in the four direction.
I asked this question to avoid inserting so many IF constructs.

I was wondering if there was a shorter and more efficient way to write this code. That's all

There are four different direction keys, and you're going to want four different things to happen, so not really. There's really nothing wrong with four conditional statements in your movement code. Shorter doesn't always mean more efficient. You can always stick them in their own function and collapse it once you have it set up...

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.