Is it the same?

For the previous project Speedy Saucer I made some research to load new levels (so to load scenes).
And since we have seen @export I wonder if
@export myScene: PackedScene;

func load_my_scene()

is equivalent to preload(path).instantiate() ?

That’s one way to load a scene, and to answer your question, yes, those will pretty much do the same thing (slight difference because you’re using preload()). That said, you probably want to be using something more like this if you’re using paths (and similar if you’re using PackedScenes):

var scene_path: String = preload("your scene path")

func load_my_scene(): #you can use a parameter for choosing the scene

Even though the course does not use call_deferred() when changing scenes, it’s a good idea, as there have been multiple recent issues related to this.

If you use instantiate() for level scenes, first of all you need to set an instance variable to the return value of instantiate() and then use get_tree().add_child(instance) (or do it all in one line if you really want to). You will see this in Alien Attack if it hasn’t been introduced already. Second, and more importantly, this will not “unload” the previous level scene; both will be in the scene tree at the same time and you’ll be left with a mess.

instantiate() can actually work for this, but getting a safe and reliable scene transition that way involves doing a lot of other things; it’s way simpler to just use Godot’s built-in stuff. Hope that makes sense =)

Privacy & Terms