0 votes

Hello everyone :) Hope everyone is doing well. My question today is a simple question of deceptive complexity. I'm currently coding a pause menu that can be controlled by key presses. An arrow sprite follows whichever button currently has focus, the code uses a bunch of "if" statements to achieve this. Because of this, my code is very messy and hard to keep track of (I'll put my code up here at the end so you can see it in all it's disorganized glory) as well as a few pictures. Anyways, I would like to know if anybody has figured out a much cleaner method to get a menu that has an arrow follow which button has focus without using a million "if" statements.



my code :

giving a link to the code instead. It's formatted wrong in question.

Thanks in advance! Peace!

in Engine by (55 points)

My apologies, I seemed to have put my script download to private by accident, updated :)

2 Answers

+1 vote
Best answer

Connect the focus_entered-signal of your buttons to a callback that moves the arrow sprite to the buttons position. No use in doing it in _process i.e. every frame.

Also: If you find yourself repeating the same node paths, consider adding a variable to store the node. That will be more efficient and more readable, e.g.:

onready var nav_buttons = $CanvasLayer/Control/Anchor/Pip_boy_Menu/Nav_buttons

func arrow_pos():
    if nav_buttons.get_node("Items").has_focus():
        # ...

Lastly to enure that your code is formatted properly on this site each line has to start with 4 spaces, each additional 4 spaces equal an indentation layer. If you don't take this into account, underscores are interpreted as markdown and indentation is lost.

by (10,580 points)
selected by

Thank you :) I see you everywhere answering questions. You rock! This solved my problem perfectly. I'll be using a dictionary to store the arrows pos since I'm using a position2D for it's location. I also appreciate explaining the code formatting for this site, though it still seems a bit cumbersome to do so. Cheers !

+1 vote

All the items are children to Nav_buttons so you could use a for loop

onready var nav_buttons = $CanvasLayer/Control/Anchor/Pip_boy_Menu/Nav_buttons

func arrow_pos():
    for i in nav_buttons.get_children():
        if i.has_focus():
            $CanvasLayer/Control/Anchor/Menu_Arrow.global_position = i.global_position
by (3,249 points)

Thank you! I'm sorry I had my download on private before, I didn't realize it until I got home last night. This looks like a very good solution to my problem as well :) I'll certainly try it out and see which one suits my needs better. Thank you again :) Cheers!

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.