How to Clean up my Menu code

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

Hello everyone :slight_smile: 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!

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

RakuNana | 2020-09-05 06:41

:bust_in_silhouette: Reply From: njamster

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.

Thank you :slight_smile: 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 !

RakuNana | 2020-09-05 15:59

:bust_in_silhouette: Reply From: Magso

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

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 :slight_smile: I’ll certainly try it out and see which one suits my needs better. Thank you again :slight_smile: Cheers!

RakuNana | 2020-09-05 16:03