GoDot 4 Latest Version
I keep getting the stack overflow error and have looked into it and think it is constantly running over and over it crashes my debug and I dont know how to fix it
Code:
extends Control
func _ready():
var level_s = load("res://level_s.tscn")
var level_s_instance = level_s.instantiate()
add_child(level_s_instance)
var buttons_node = level_s_instance.get_node("level_s")
for button in buttons_node.get_children():
button.connect("pressed", Callable(self, "_on_button_pressed"), [button])
func _on_button_pressed(button: Button):
await get_tree().create_timer(1.2).timeout
if button.name == "level1":
get_tree().change_scene_to_file("res://node.tscn")
if button.name == "level2":
get_tree().change_scene_to_file("res://level_2.tscn")
Is this script inside a level_s.tscn
? If so Godot will add it as a child of itself over and over again, blowing your stack.
2 Likes
You should address the cause, not the consequence. There’s no reason to load a scene inside its own script. What are you trying to achieve?
3 Likes
Level_s is my scene for level select with 2 buttons level 1 and level 2 I am trying to get those buttons to work to get to level_s you have to push the play button on my main menu that uses get_tree to get there.
Sounds like you can remove the part where you instance level_s.tscn
on ready. You may need to show the scene tree for me to know where the buttons are in level_s
, I suspect they are direct children though.
And I’d recommend using Godot 4.x signal connections over the old 3.x way
func _ready():
for button in self.get_children():
button.pressed.connect(_on_button_pressed.bind(button))
After doing that I got the air era invalid access to property or key pressed on a base object of type Sprite 2D
The code for the button node named play to go to level_s is
Extends button
Func _ready():
self.connect(“pressed”, Callable(self, “_on_play_pressed”))
Func _on_play_pressed():
Get_tree().change_scene_to_file(“res://level_s.tscn”)
Can you show your scene tree?
You can take a screenshot, if you are using windows shift+windows+s is a shortcut, you can select a box around the scene tree and ctrl+v to paste it into the forum.
Cool that’s it! So you have mixed children and cannot iterate over them without checking the type. Your background, a Sprite2D, does not have the pressed
signal to be connected, your code must only try to connect to Buttons.
func _ready() -> void:
for child in self.get_children():
if child is Button:
child.pressed.connect(_on_button_pressed.bind(child))
1 Like
Now I can get to the level_s but if I click level 1 button I get Cannot call method change scene to file on null value
I have it so when any button is clicked it switches to cut_scene and waits as long as the cut scene runs to then switch is that the issue
Sounds like it. This snippet you posted is using the button.name
to load the next scene, but if you are changing scenes between the click, then the button
is being deleted, thus a null error.
1 Like
So how can I switch to cut scene and go to the right level
if you play the same cutscene for every level select maybe you could instantiate the cutscene inside the level select scene, play it when a button is pressed, then use change_scene_to_file
. If a cutscene is unique for each level then the cutscene scene should be in charge of loading the next level when it is finished.
1 Like
I didn’t see your message but that’s what I did and I encountered a few errors which I could fix but I just made a new post about the standalone lambdas error if you want to look at that