Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Cashew |
var error_code: int = get_tree().change_scene("res://Levels/Level1.tscn")
Probably this?
jgodfrey | 2023-02-01 17:56
The thing is I want to assign get_tree().change_scene("res://Levels/Level1.tscn")
as an int variable so I can print is if it is not equal to 0
The reason for this stems from the Warning message where get_tree().change_scene("res://Levels/Level1.tscn")
tells you it returns a value but it is never used.
Cashew | 2023-02-01 18:00
I’m not sure I follow exactly, but here’s some background that might be helpful.
First, get_tree()
is a method associated with anything inheriting from Node
. However, it will return null
if the node from which it’s called IS NOT a part of the scene tree (that’s outline in the link I provided above).
So, assuming you call it from a node that’s not in the scene tree, you cannot then immediately try to call change_scene()
because you’ll be working with a null reference.
So, if there’s a chance you might be calling get_tree()
from a node not in the scene tree, do that first. That way you can verify that you have a valid reference before tyring to use it. So (untested) but something like this:
var tree = get_tree()
if tree:
var error_code: int = tree.change_scene("res://Levels/Level1.tscn")
if error_code != OK:
print("failed to load requested scene")
else:
print("failed to get tree")
jgodfrey | 2023-02-01 19:24
Thank you very much, I am quite new to Godot and coding so your info is much appreciated!
Cashew | 2023-02-02 21:05