0 votes

extends MarginContainer

const World = preload("res://World.tscn")

onready var selectorone = $CenterContainer/VBoxContainer/CenterContainer2/VBoxContainer/CenterContainer/HBoxContainer/Selector
onready var selector
two = $CenterContainer/VBoxContainer/CenterContainer2/VBoxContainer/CenterContainer2/HBoxContainer/Selector

var current_selection = 0

func ready():
set
current_selection(0)

func process(delta):
if Input.is
actionjustpressed("uidown") and currentselection < 1:
currentselection += 1
set
currentselection(currentselection)
elif Input.isactionjustpressed("uiup") and currentselection < 0:
current
selection -= 1
setcurrentselection(currentselection)
elif Input.is
actionjustpressed("uiaccept"):
handle
selection(current_selection)

func handleselection(currentselection):
if _current
selection == 0:
getparent().addchild(World.instance())
queue_free()

func setcurrentselection(currentselection):
selectorone.text = ""
selector
two.text = ""
if currentselection == 0:
selectorone.text = ">"
elif _current
selection == 1:
selector_two.text = ">"

Godot version Godot Engine v3.3.stable.official
in Engine by (12 points)

1 Answer

0 votes

From what I can see, there's an issue with your condition:

elif Input.is_action_just_pressed("ui_up") and current_selection < 0:

You're checking if current_selection is less than zero while you should check if current_select is more than zero. So:

elif Input.is_action_just_pressed("ui_up") and current_selection > 0:

That'll hopefully fix the issue.


Btw an easier way to do something like this (Adding an ">" to show the selection) would be to use focus_entered and focus_exited signals.
You simply add the ">" when the button gains focus and remove it when it loses focus.

For example:
You could create a single script called menu_button.gd and attach it to both of your menu nodes. The script could look something like this:

export(bool) starts_with_focus

func _ready():
    connect("focus_entered", self, "_on_focus_entered")
    connect("focus_exited", self, "_on_focus_exited")

    if starts_with_focus:
        grab_focus()

func _on_focus_entered():
    text = text.insert("> ")

func _on_focus_exited():
    text = text.trim_prefix("> ")

Methods: String.insert() | String.trim_prefix() | connect() | grab_focus()

Remember that one of your nodes must start with focus for this to work. That's why we have an exported variable called starts_with_focus. Using an exported variable makes it easier to choose which one of your nodes will start with focus.


Edit: Suggested a possibly easier way to do what OP wants to do.

by (180 points)
edited by
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.