Open torbenvanassche opened 1 month ago
Here is one example of how I might handle this situation. You could probably combine this with signals that you create yourself to do whatever you need to do
extends Node
var thread:Thread = Thread.new()
@onready var sprite_2d:Sprite2D= $"../Sprite2D" as Sprite2D
@onready var mona_lisa:Sprite2D = $"../mona_lisa" as Sprite2D
func _ready():
thread.start(thread_func)
func thread_func():
var mona_lisa_texture = load("res://mona.png")
var i = 0
var j = 10000
while i != 30000000: # LONG WHILE LOOP TO SIMULATE LOADING TIME
i += 1
if i % j == 0:
print(i)
call_deferred("done")
return mona_lisa_texture
func _process(delta):
sprite_2d.position.x += 2
func done():
print("All finished, destroying thread objects")
mona_lisa.texture = thread.wait_to_finish()
Describe the project you are working on
I am working on a project where i want to have adjacent rooms load into memory when you enter a room. For this I am using the
ResourceLoader.load_threaded_request
. This allows me to dynamically load the nodes I need and clean up the ones I don't.Describe the problem or limitation you are having in your project
The problem is that there is no way of knowing when something has finished loading, other than manually checking. The only way this is currently possible is by periodically checking the status of
ResourceLoader.load_threaded_get_status
. This makes it a little tricky to manage the data and its loading.Describe the feature / enhancement and how it helps to overcome the problem or limitation
Having a signal would prevent the need for the periodic check, I would just be able to connect the signal to add the node directly, rather than polling every X seconds to see if the asset is ready, then instantiating it if it is.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
In terms of how it works on the engine side, I have little insight to offer. However, as a user I could see the following work:
Where you can then have
signal load_threaded_finished(data: Variant)
so you can work with the data you receive directlyIf this enhancement will not be used often, can it be worked around with a few lines of script?
I wrote some code that works around the issue with a
Timer
but as I mentioned above, this is not optimal:Is there a reason why this should be core and not an add-on in the asset library?
I think this could be useful, not just for me, but for a lot of things in the engine as a whole. Personally, I feel this is something that should exist and I was surprised that it doesn't yet.