Greetings, I’m making a game on godot and I ran into a problem, now I’ll tell you what my problem is.
I’m making a resource loader, because when I first load the game, it hangs in some moments, for example, I have a game of horror, and when opening cameras, doors or movement in the room there are lags, I realized that this is because the game loads some resources while in the scene, not when it loads. I started to study how to make a resource loader, and wrote a script for loading resources, which finds all files .tscn and .png and loads them through ResourceLoader. I started debugging to find out what directories the script sees, and realized that it sees all directories and subdirectories, but it can’t find any files with the extensions I need, so I made a script that finds all files in the project, and it could only find files with the extensions .ctex and .scn.
I tried to make it so that he loaded them, but not only that he loads absolutely all the files of the project, even unnecessary to me, which makes loading long, but after that breaks vulkan, and I have stopped working shaders. well, and of course kills fps. I tried to search and selectively load resources, but all directories are empty, and all files that the script finds are exclusively in the import directory. I can’t figure out what to do, please help me. If you need more information, I can provide it all
I personally don’t think that making a custom ResourceLoader is required in basically any circumstance. You can use preload keyword to load resources earlier than they’re used, or even load resources on a separate thread to avoid loading lag.
How can I avoid lags at the first start of the game? It kills the comfort for players very much, can you please advise? Preload the scene when switching from the menu scene to the game scene?
You can e.g. load the resources on a separate thread when the player is in the main menu. Look up how threaded resource loading works and try to apply to your scenario.
What resources can I work with in the project? I read how it works, but I can not understand why it does not find the resource to which I set the path, for example var resource_path = “res://src/scenes/test.tscn” it does not find it
extends Node
var resource_path = “res://src/scenes/test.tscn”
var load_id = 0
func _ready():
# Initialization of asynchronous loading
load_id = ResourceLoader.load_threaded_request(resource_path)
set_process(true)
func _process(delta):
If load_id != 0:
var status = ResourceLoader.load_threaded_get_status(load_id)
if status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED:
var resource = ResourceLoader.load_threaded_get(load_id)
# Use the loaded resource
set_process(false)
elif status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_FAILED:
print(“Error loading resource.”)
set_process(false)
I think you misinterpreted the return values of the load_threaded_request() - it’s not an ID, it’s an Error type.
Your code should be as below, for me it works:
extends Node
var resource_path = "res://src/scenes/test.tscn"
func _ready():
# Initialization of asynchronous loading
ResourceLoader.load_threaded_request(resource_path)
set_process(true)
func _process(delta):
var status = ResourceLoader.load_threaded_get_status(resource_path)
if status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED:
var resource = ResourceLoader.load_threaded_get(resource_path)
# Use the loaded resource
set_process(false)
elif status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_FAILED:
print("Error loading resource.")
set_process(false)
Ok, great, when exporting it found what is needed, to avoid lags, do I need to load only the main scene (game) ? or do I need to load all the heavy scenes ?
@wchc has the best current solution, 4.4 will implement ubershaders which can reduce shader compilation stutters you may see in addition to scene loading.
Depends on your game structure, if you can find time to use threaded_request before loading a scene, you should use it.
Hi, I modified the script a little bit, but the lags didn’t go anywhere, I load game scenes, and other scenes, also I loaded in streams models, but it didn’t help for some reason, here is the script I modified, and the video, with the problem.
extends Node
var resource_paths = ["res://src/Scenes/test.tscn", "res://src/Scenes/monitor.tscn", "res://src/Scenes/Light.tscn", "res://src/Scenes/freddy.tscn", "res://src/Scenes/foxy.tscn", "res://src/Scenes/bonnie.tscn", "res://src/Models/game.blend", "res://src/Models/untitled.blend", "res://src/Models/Foxy2/foxy.blend", "res://src/Models/player/playermodel.blend"]
var current_index = 0
var loading_label: Label
func _ready():
loading_label = $VBoxContainer/Label
if resource_paths.size() > 0:
start_loading_resource()
else:
loading_label.text = "No resources to load."
func start_loading_resource():
var current_path = resource_paths[current_index]
var load_status = ResourceLoader.load_threaded_request(current_path)
if load_status != OK:
print("Error during initialisation of resource loading:", current_path)
loading_label.text = "Resource loading error: " + current_path
set_process(false)
else:
print("Resource loading has begun:", current_path)
loading_label.text = "Resource loading: " + current_path
set_process(true)
func _process(delta):
if current_index >= resource_paths.size():
set_process(false)
return
var current_path = resource_paths[current_index]
var status = ResourceLoader.load_threaded_get_status(current_path)
if status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_LOADED:
var resource = ResourceLoader.load_threaded_get(current_path)
if resource:
print("The resource has been successfully uploaded:", current_path)
loading_label.text = "The resource has been successfully uploaded: " + current_path
else:
print("Error: no downloaded resource is available.")
loading_label.text = "Error: resource is missing."
current_index += 1
if current_index < resource_paths.size():
start_loading_resource()
else:
loading_label.text = "All resources have been successfully downloaded. Switching to the main stage."
print("All resources have been successfully downloaded. Switching to the main stage.")
var first_scene = ResourceLoader.load(resource_paths[0])
if first_scene:
get_tree().change_scene_to_packed(first_scene)
else:
print("Error loading the main scene.")
elif status == ResourceLoader.ThreadLoadStatus.THREAD_LOAD_FAILED:
print("Error when loading a resource:", current_path)
loading_label.text = "Resource loading error: " + current_path
set_process(false)
else:
print("Uploading continues: ", current_path)
loading_label.text = "Uploading continues: " + current_path
I notice you have blend files in there. I think you are experiencing shader compilation stutters, not loading stutters. If the stutters are happening mid-game, well beyond when the scene loads, then it’s from shader compilations that only trigger on-sight.
If all of these resource_paths files are already inside the game scene, then they do not need to be loaded individually, just the first scene. Since you aren’t doing anything with the load_threaded_get resources, it’s really only caching the file, which does help but might be extra work for little return, especially on subsequent calls.
This video goes over one developer’s troubles and how they crossed those hurdles. They settled on a complex solution to demo the game at a fast speed in the background to ensure every material was seen in every lighting condition it would meet in regular gameplay.
I did 2 things that helped me. the first was to make a fake shader compilation screen while the camera was moving around the scene and manually compiling all the shaders, this helped me remove the staters, the second was to upgrade to 4.4 dev 7, having ubershaders also solved my problem, I decided to go with the second option, Thanks!!!