Topic was automatically imported from the old Question2Answer platform.
Asked By
Settoo
extends MarginContainer
const World = preload(“res://World.tscn”)
onready var selector_one = $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_action_just_pressed(“ui_down”) and current_selection < 1:
current_selection += 1
set_current_selection(current_selection)
elif Input.is_action_just_pressed(“ui_up”) and current_selection < 0:
current_selection -= 1
set_current_selection(current_selection)
elif Input.is_action_just_pressed(“ui_accept”):
handle_selection(current_selection)
func handle_selection(_current_selection):
if _current_selection == 0:
get_parent().add_child(World.instance())
queue_free()
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("> ")
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.