Help with themes buttons code

Godot Version

4.2

Question

i got an error
Attempt to call function 'add_theme_stylebox_override' in base 'null instance' on a null instance.
but all nodes are exist…

image

var tabs = {
    0: $General,
    1: $Graphics,
    2: $Audio,
    3: $Interface
}

var btns = {
    0: $MarginContainer/HBoxContainer/General,
    1: $MarginContainer/HBoxContainer/Graphics,
    2: $MarginContainer/HBoxContainer/Audio,
    3: $MarginContainer/HBoxContainer/Interface
}

var btn_styles = {
    "normal": load("res://btn_normal.tres"),
    "pressed": load("res://btn_pressed.tres")
}

var colors = {
    "yellow": Color(1.0,0.733,0.129,1.0),
    "grey": Color(0.388,0.388,0.388,1.0)
}

var current_tab = 0

func _ready():
    for i in range(4):
        btns[i].add_theme_stylebox_override("normal", btn_styles["normal"])
        btns[i].add_theme_stylebox_override("hover", btn_styles["normal"])
        btns[i].set("theme_override_colors/font_color", colors["grey"])
        btns[i].set("theme_override_colors/font_hover_color", colors["grey"])
    select_tab(current_tab)

func select_tab(tab_index):
    current_tab = tab_index
    for i in range(4):
        if i == tab_index:
            btns[i].add_theme_stylebox_override("normal", btn_styles["pressed"])
            btns[i].add_theme_stylebox_override("hover", btn_styles["pressed"])
            btns[i].set("theme_override_colors/font_color", colors["yellow"])
            btns[i].set("theme_override_colors/font_hover_color", colors["yellow"])
            tabs[i].show()
        else:
            btns[i].add_theme_stylebox_override("normal", btn_styles["normal"])
            btns[i].add_theme_stylebox_override("hover", btn_styles["normal"])
            btns[i].set("theme_override_colors/font_color", colors["grey"])
            btns[i].set("theme_override_colors/font_hover_color", colors["grey"])
            tabs[i].hide()

func _unhandled_input(event):
    if Input.is_action_just_pressed("esc"):
        self.hide()

When the script initializes it initializes its variables. At that point in time the node is not inside the scene tree. Use @onready in the variables that access the scene tree in this case btns and tabs

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.