Godot Version
Godot 4.5stable
Question
I have a loading screen alongside a scaling map generation however the scene will load properly before the generation finishes, what would be the easiest way to make it so the loading screen waits until the map is completely generated first?
I did not make the loading screen myself, I followed a tutorial I had found as I do not quite understand the ins and outs of threaded loading yet.
Map Generation:
func placeGrass(areaX,areaY):
for i in areaY+1:
for k in areaX+1:
var grassBlade = grass.instantiate()
grassBlade.position.x = (0.5 * -areaX/2) + (k * 0.5)
grassBlade.position.z = (0.5 * -areaY/2) + (i * 0.5)
grassBlade.rotation.y = deg_to_rad(randi())
add_child(grassBlade)
global.grassTotal += 1
k += 1
i += 1
if i % 5 == 0: await get_tree().process_frame
Loading Screen:
extends Node
signal progress_changed(progress)
signal load_finished()
var loading_screen:PackedScene = preload("uid://bfc4ubf4m18ua")
var loaded_resource:PackedScene
var scene_path: String
var progress:Array = []
var use_sub_threads:bool = true
func _ready() -> void:
set_process(false)
func load_scene(_scene_path: String) -> void:
scene_path = _scene_path
var new_load_screen = loading_screen.instantiate()
add_child(new_load_screen)
progress_changed.connect(new_load_screen._on_progress_changed)
load_finished.connect(new_load_screen._on_load_finished)
await new_load_screen.loading_screen_ready
start_load()
func start_load() -> void:
var state = ResourceLoader.load_threaded_request(scene_path, "", use_sub_threads)
if state == OK:
set_process(true)
func _process(_delta) -> void:
var load_status = ResourceLoader.load_threaded_get_status(scene_path, progress)
progress_changed.emit(progress[0])
match load_status:
ResourceLoader.THREAD_LOAD_INVALID_RESOURCE, ResourceLoader.THREAD_LOAD_FAILED:
set_process(false)
ResourceLoader.THREAD_LOAD_LOADED:
loaded_resource = ResourceLoader.load_threaded_get(scene_path)
get_tree().change_scene_to_packed(loaded_resource)
load_finished.emit()