a loading bar demo

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nonomiyo

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 :

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 ? :confused:
Thanks !

:bust_in_silhouette: Reply From: happycamper

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):
# Add new scene as child of root
# 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

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
load_stuff.call_deferred('wait to finish')

Hi happycamper, thanks for helping !!

I put this line in the titlescreen’s script :

func _on_loadbutton_pressed():

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 goto_scene and prep_scene work together ?
The way I understand it, is :
1/ running goto_scene with loading = 1
2/ running prep_scene
3/ running goto_scene with loading = 0

nonomiyo | 2018-07-13 17:39