The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+5 votes

Does anyone know the best way to make a menu list you can scroll with a keyboard. For example, I have a menu that has the following options:

Fight
Skill
Item
Run

They are sorted vertically, and I would like to sort through them using the up and down arrow keys, and then select whatever the highlighted option is with the select key ( whatever that may be in the end, we'll say its enter for simplicity sake). I've been trying to implement this system on my own but it is VERY quickly becoming unwieldy. So does anyone know how to do implement this correctly?

in Engine by (36 points)

2 Answers

+3 votes
Best answer

The way I would go about this is to store direction key presses as either boolean variables or a vector (I'd prefer vector personally,) controlled by the input function. Then, you can check what the active area is, whether it's the main game or a menu panel, to decide how to apply the 'direction' keys.

In the controller function.

#keep a global variable
var direction = Vector2(0,0)

func _input(event):
    if event.is_action("up") and event.is_pressed():
        direction.y = -1
    elif event.is_action("down") and event.is_pressed():
        direction.y = 1
    else:
        direction.y = 0

    if event.is_action("right") and event.is_pressed():
        direction.x = -1
    elif event.is_action("left") and event.is_pressed():
        direction.x = 1
    else:
        direction.x = 0

in game:

#however you move (I'll use the kinematicbody2d's move for this)
move(direction.normalized() * speed) #speed is how far you want to move per frame

in menu:


use a variable to keep track of position in array

var list_pos = 0

somewhere in process of the menu script

list_pos += direction.y 


The code may not be exact as this answer is off the top of my head.

by (225 points)
selected by
+3 votes

Just have them as children of Vboxcontainer (Hboxcontainer?) and place focus on element when turn comes up.

by (50 points)
Just out of curiosity, would I be able to make a sprite hover over these elements when they have focus? (To better show which is selected) and if so, would you be willing to give me a quick sudo code example?
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.