0 votes

Hi,
I've been using the Custom scene switcher from the documentation in some projects and I think it is a bit overkill considering the methods that SceneTree offers.

I've replaced this global.gd script:

extends Node

var current_scene = null

func _ready():
    var root = get_tree().get_root()
    current_scene = root.get_child( root.get_child_count() -1 )

func goto_scene(path):    
    call_deferred("_deferred_goto_scene",path)


func _deferred_goto_scene(path):
    current_scene.free()
    var s = ResourceLoader.load(path)
    current_scene = s.instance()
    get_tree().get_root().add_child(current_scene)
    get_tree().set_current_scene( current_scene )

by this one:

extends Node

var current_scene = null
var new_scene = null

func _ready():
    var root = get_tree().get_root()
    current_scene = root.get_child( root.get_child_count() -1 )
    pass

func goto_scene(path):
    var s = ResourceLoader.load(path)
    new_scene=s.instance()
    get_tree().get_root().add_child(new_scene)
    get_tree().set_current_scene(new_scene)
    current_scene.queue_free()
    current_scene=new_scene

It seems to work properly.

Does anyone foresee any problem with this method? (tested only on simple scene switching).

in Engine by (690 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.