+1 vote

Hello, I've been struggling way too much with incrementing a loading bar to change scene from title screen to my game scene.

I followed the singleton tuto, then there's this page :

http://docs.godotengine.org/en/3.0/tutorials/io/background_loading.html#doc-background-loading

I'm totally lost, talking about polls and threads. Couldn't find anything either in the demo ressources from the Godot website.

Is there somewhere a beginner's guide for loading bars ? :/
Thanks !

in Engine by (248 points)
edited by

1 Answer

+2 votes
Best answer

The idea is to switch to a scene that runs a loading animation and having a thread to set up the next scene. once the thread is finished you remove the loading screen scene and add the new scene created by the thread into the tree. The code below is something i just typed up for you to get an idea of how to go about this, I didn't actually test this code.

onready var load_stuff = Thread.new()
var current_scene= #your scene here

#new_scene here is a node and loading is a boolean
func goto_scene(new_scene,loading):
current_scene.queue_free()
    current_scene=new_scene
# Add new scene as child of root
get_tree().get_root().call_deferred("add_child",current_scene)
# optional, to make it compatible with the SceneTree.change_scene() API
get_tree().set_current_scene( current_scene )
# if we are using the loading scene then we want a thread to prep next scene
if(loading):
load_stuff.start(self,"prep_scene")

func prep_scene():
#your code for setting up the next scene goes here
var s = ResourceLoader.load(path)
var scene= s.instance()
#make sure if add_child function is used anywhere during the thread you use call deferred since add_child() is not very thread safe, look at the goto_scene for an example of this.
#once the new scene is prepared we can switch to it, also set false since we aren't doing the loading screen again
goto_scene(scene,false)
load_stuff.call_deferred('wait to finish')
by (108 points)
selected by

Hi happycamper, thanks for helping !!

I put this line in the titlescreen's script :

func _on_loadbutton_pressed():
    get_node("/root/global").goto_scene("/root/ingame",true)

the first argument should be a node, right ? But if the scene it's in is not loaded yet, how can it be targetted ? Should I put "res://scenes/ingame.tscn" instead ? (none seem to work)

and how the gotoscene and prepscene work together ?
The way I understand it, is :
1/ running gotoscene with loading = 1
2/ running prep
scene
3/ running goto_scene with loading = 0

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.