0 votes

I'm having a problem with making a start screen and adding a menu to it after when the user presses start.

The problem is that it seems to skip the menu altogether and just starts the game, I checked the code, and everything seems to be in order, so I'm assuming that it's just that the button recognition is way too sensitive, even though the script I'm using allows for a check if the key is pressed then released.

Here's my code:

#Start Screen Code
extends Spatial

export(NodePath) var animationNode
export(NodePath) var mainMenu
export(String, FILE) var nextScene

var input_states = preload("res://Scripts/input_states.gd")

var btn_start = input_states.new("ui_accept")

func _process(delta):
    if btn_start.check() == 1:
        if get_node(animationNode).get_current_animation() == "Start_Screen":
            get_node(mainMenu).activate(delta)
            changeAnimation("Menu")
        elif !get_node(mainMenu).isActiveMenu():
            changeAnimation("Start_Screen")

func changeAnimation(value):
    get_node(animationNode).set_current_animation(value)
    get_node(animationNode).play(value)
__________
#Menu Script
extends Control

# member variables here, example:
# var a=2
# var b="textvar"

var input_states = preload("res://Scripts/input_states.gd")
export(int, "Main", "Pause") var menuType
export(Array) var menuItems
export(NodePath) var higherUpMenu
export(NodePath) var globalScript

var currentlySelected = 0

export(bool) var activeMenu = false

var btnUp = input_states.new("ui_up")
var btnDown = input_states.new("ui_down")
var btnSelect = input_states.new("ui_accept")

func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    set_fixed_process(true)
    pass

func _fixed_process(delta):
    if activeMenu:
        if menuType == 1:
            if get_tree().is_paused():
                show()
                if menuItems.size() > 1:
                    menuNavigation()
        else:
            show()
            if menuItems.size() > 1:
                menuNavigation()
    else:
        hide()

#This makes it so that when the user pressed up or down, the menu items will scroll accordingly
#If they press select, then the game will do the menu's action
func menuNavigation():
    if btnDown.check() == 1:
        clearAll()
        if currentlySelected < menuItems.size() - 1:
            currentlySelected += 1
        else:
            currentlySelected = 0
        get_node(menuItems[currentlySelected]).setCurrentlySelected(true)
    elif btnUp.check() == 1:
        clearAll()
        if currentlySelected > 0:
            currentlySelected -= 1
        else:
            currentlySelected = menuItems.size() -1
        get_node(menuItems[currentlySelected]).setCurrentlySelected(true)
    if btnSelect.check() == 1:
        get_node(menuItems[currentlySelected]).execute()


#This function is to clear all elements, this is used if the user uses the mouse rather than the keys
#to hover over a menu option.
func clearAll():
    for item in menuItems:
        get_node(item).setCurrentlySelected(false)

#This function is for when the use decides to exit, for example, in a pause menu, this will unpause the game
func exit_menu():
    if (menuType == 1):
        get_tree().set_pause(false)
        setAsActiveMenu(false)

func activate(delta):
    var timer = 0
    while(timer < 0.5):
        timer += delta
    setAsActiveMenu(true)
__________
#Menu item Script
extends Container

export(int, "Scene Change", "Value change", "Menu Changer", "Exit Menu") var menuItemType
export(Array) var arguments
export(NodePath) var animationPlayer

export(String, FILE) var fileSelectPlaceholder

var timer = 0
var cooler = 1

var currentlySelected = false

func _ready():
    # Called every time the node is added to the scene.
    # Initialization here
    pass

#This is set this as whether or not it is the currently selected item in the menu
func setCurrentlySelected(value):
    currentlySelected = value
    if currentlySelected == true:
        get_node(animationPlayer).play("Selected")
    else:
        get_node(animationPlayer).play("Idle")

func _on_Container_mouse_enter():
    get_node(animationPlayer).play("Selected")
    setCurrentlySelected(true)

func _on_Container_mouse_exit():
    get_node(animationPlayer).play("Idle")
    setCurrentlySelected(false)

func _input_event(event):
    if (event.type == InputEvent.MOUSE_BUTTON):
        execute()

# This executes the menu item's command, which depends on which type it is, it will change the scene
# if it's the scene type, it will change a value if it's a value changer, and it will change the menu
# if it's a menu changer, it can also exit the current menu.
func execute():
    if menuItemType == 0:
        get_tree().set_pause(false)
        get_tree().change_scene(arguments[0])
    elif menuItemType == 3:
        get_node(arguments[0]).exit_menu()

What am I doing wrong?

in Engine by (169 points)

Put a breakpont inside both inner if, maybe a single press is checked twice and once with the animation not active the other with animation active.

Disable some checks or buttons until some point of the animation (you can add a callback track) to prevent multiple button pressed checks.

Another option, use button signal and a "toggle" variable there, disable it later after you made the corresponding action (like when changing animation).
In some cases you may want to add an extra variable to ignore the button signals actions until it is released (check press, ignore pressed and check released).

Does the next menu contain a "Start game" button, by any chance? Perhaps your mouse click is passing through and reaches this button too fast while you animate the first screen?

Zylann: It is, yeah...

eons: Your toggle solution worked, at least as a temporary solution, so thank you!

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.