20
submitted 1 month ago* (last edited 1 month ago) by Smorty@lemmy.blahaj.zone to c/godot@programming.dev

I want to implement a threaded loading component into my game. I am currently saving all my settings and other level files as bytes externally (meaning not in the res:// folders), as I want to be able to export files and send them to others, so they can use them as well. There is the ResourceLoader.load_threaded_request() method, but that only returns Resources and not PackedByteArrays. As far as I can tell, there is only the FileAccess.get_file_as_bytes() method for importing byte files, which has to run in the main thread (and thus blocking it until the load is complete). Does someone know if I am missing some method here?

EDIT: I have put my fix for this into the comments

top 3 comments
sorted by: hot top controversial new old
[-] Smorty@lemmy.blahaj.zone 5 points 1 month ago* (last edited 1 month ago)

Fix: Run the FileAccess.get_file_as_bytes() method in a thread. I thought a thread wouldn't allow for loading files using FileAccess, but it seems I was mistaken. I personally just make the Thread run my _threaded_loading(file_path:String) method and then call a signal named loaded_bytes(data:PackedByteArray). The _threaded_loading method looks like this:

func _threaded_loading(path:String) -> void:
	print("I am the thread and I will now load this file")
	var file:FileAccess = FileAccess.open(path, FileAccess.READ)
	print("i have opened the file")
	var bytes:PackedByteArray = file.get_buffer(file.get_length())
	print("I have the bytes of the file")
	file.close()
	print("I have closed the file")
	call_deferred_thread_group("emit_signal", "loaded_bytes", bytes)
	return

It is important to emit the signal deferred, as a Thread on its own can't emit a signal on its own. I did print a lot in the method just to check that everything is working as intended. I hope this helps someone!

[-] Smorty@lemmy.blahaj.zone 2 points 1 month ago

I have found that this is incredibly fast. Loading files in threads in insane. Here is a demo where Godot loads 456 files, which all together add up to 1.6 GB in size in less than four seconds https://files.catbox.moe/g28op4.webm

[-] NocturnalMorning@lemmy.world 1 points 1 month ago* (last edited 1 month ago)

Have you tried asking this question in the Godot discord? The engine devs hang out there, might be able to give you a hand.

this post was submitted on 27 May 2024
20 points (95.5% liked)

Godot

5372 readers
3 users here now

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

!roguelikedev@programming.dev

Credits

founded 1 year ago
MODERATORS