Godot 4.2.1
I have this GDScript code in godot 4.2.1 and I want to use active buttons when I press the buttons, project is work but when I click the buttons active buttons not work, any idea will appreciated.
extends Node2D
var scenes = {
"one": "res://Scenes/One.tscn",
"two": "res://Scenes/Two.tscn",
"three": "res://Scenes/Three.tscn",
"four": "res://Scenes/Four.tscn"
}
var oneButton
var twoButton
var threeButton
var fourButton
var oneButtonActive
var twoButtonActive
var threeButtonActive
var fourButtonActive
func _ready():
# Load active textures here
oneButtonActive = preload("res://Assets/bg_one.png")
twoButtonActive = preload("res://Assets/bg_two.png")
threeButtonActive = preload("res://Assets/bg_three.png")
fourButtonActive = preload("res://Assets/bg_four.png")
oneButton = $HBoxContainer/TextureRect/HBoxContainer/oneButton
twoButton = $HBoxContainer/TextureRect/HBoxContainer2/twoButton
threeButton = $HBoxContainer/TextureRect/HBoxContainer3/threeButton
fourButton = $HBoxContainer/TextureRect/HBoxContainer4/fourButton
oneButton.texture_normal = preload("res://Assets/one.png")
twoButton.texture_normal = preload("res://Assets/two.png")
threeButton.texture_normal = preload("res://Assets/three.png")
fourButton.texture_normal = preload("res://Assets/four.png")
oneButton.connect("pressed", Callable(self, "_on_one_button_pressed"))
twoButton.connect("pressed", Callable(self, "_on_two_button_pressed"))
threeButton.connect("pressed", Callable(self, "_on_three_button_pressed"))
fourButton.connect("pressed", Callable(self, "_on_four_button_pressed"))
func _on_one_button_pressed():
oneButton.texture_normal = oneButtonActive
change_scene("one")
func _on_two_button_pressed():
twoButton.texture_normal = twoButtonActive
change_scene("two")
func _on_three_button_pressed():
threeButton.texture_normal = threeButtonActive
change_scene("three")
func _on_four_button_pressed():
fourButton.texture_normal = fourButtonActive
change_scene("four")
func change_scene(scene_name: String) -> void:
if scenes.has(scene_name):
var new_scene_resource = load(scenes[scene_name])
if new_scene_resource is PackedScene:
var new_scene = new_scene_resource.instantiate()
if new_scene:
# Add the new scene to the tree
get_tree().root.add_child(new_scene)
if get_tree().current_scene:
get_tree().current_scene.queue_free()
get_tree().current_scene = new_scene
else:
print("Error: failed to instance the scene.")
else:
print("Error: scene not found.")