How can I load the images asynchronously?

It has a list of nodes with level information, it has a name, duration and the path with the saved level file, they have a “thumbnail” or background image and it takes a long time to load, I want to know how I can load these images asynchronously.

heres my node child generation code:

extends VBoxContainer

func _ready():
	generarLista()

func generarLista() :
	DATABASE.db.query("SELECT * FROM Localsongs")
	var res = DATABASE.db.query_result
	for x in res :
		var nodo = preload("res://objects/Interfaz/cancion.tscn").instantiate()
		var title : String = x["NOMBRE"] + " [" + x["NOMBREDIFF"] + "]"
		if len(title) >= 30 :
			title = title.left(30) + str("...")
		nodo.get_child(4).get_child(1).text = title
		nodo.get_child(0).set_meta("cancion",x["ARCHIVO"])
		nodo.get_child(1).text = x["MODO"] + "k"
		nodo.get_child(2).text = x["DIFF"]
		
		var seg = float(x["DURACION"]) / 1000.0
		var tiempof = ""
		var segundos_entero = int(seg)
		if int(segundos_entero / 3600) != 0 :
			tiempof += str(int(segundos_entero / 3600)).pad_zeros(2) + ":"
		tiempof += str(int((segundos_entero % 3600) / 60)).pad_zeros(2) + ":"
		tiempof += str(int(segundos_entero % 60)).pad_zeros(2)
		
		nodo.get_child(3).text = tiempof
		nodo.get_child(4).get_child(0).texture = await getFondo(x["ARCHIVO"])
		
		add_child(nodo)

func getFondo(ruta):
	var archivo = FileAccess.open(ruta, FileAccess.READ)
	var info = archivo.get_var()
	archivo.close()
	var img = Image.new()
	img.load(info["Fondo"])
	var itex = ImageTexture.new()
	itex.set_image(img)
	
	return itex
	
func recargarLista() :
	for v in get_children() :
		remove_child(v)
	generarLista()

idk eng, the ‘getFondo’ its the func for load the images

I’ll try to help, but this isn’t my expertise.

I don’t think Godot’s await keyword is meant to be used for this sort of async code, it’s more like when you want your code to wait until something happens. Using that in the for loop is just making it wait until the function is done. You could try having a function that changes the image, but I can’t think how to set that async.

Maybe instead you could use threads? I’ve not used them before, but they seem like something that could be useful for this - Threads

Sorry if this is not helpful!

TY!, it works with thread, I used a strange solution I think, but it works.

here is the solution in case it helps someone

extends VBoxContainer

var thread := Thread.new()
var coso = []
func _ready():
	generarLista()

func generarLista() :
	DATABASE.db.query("SELECT * FROM Localsongs")
	var res = DATABASE.db.query_result
	for x in res :
		var nodo = preload("res://objects/Interfaz/cancion.tscn").instantiate()
		var title : String = x["NOMBRE"] + " [" + x["NOMBREDIFF"] + "]"
		if len(title) >= 30 :
			title = title.left(30) + str("...")
		nodo.get_child(4).get_child(1).text = title
		nodo.get_child(0).set_meta("cancion",x["ARCHIVO"])
		nodo.get_child(1).text = x["MODO"] + "k"
		nodo.get_child(2).text = x["DIFF"]
		
		var seg = float(x["DURACION"]) / 1000.0
		var tiempof = ""
		var segundos_entero = int(seg)
		if int(segundos_entero / 3600) != 0 :
			tiempof += str(int(segundos_entero / 3600)).pad_zeros(2) + ":"
		tiempof += str(int((segundos_entero % 3600) / 60)).pad_zeros(2) + ":"
		tiempof += str(int(segundos_entero % 60)).pad_zeros(2)
		
		nodo.get_child(3).text = tiempof
		var rng = randi()
		nodo.name = str(rng)
		add_child(nodo)
		coso.push_back([x["ARCHIVO"],nodo])
	thread.start(setFondo.bind(coso, thread))

func setFondo(cosos, hijo):
	hijo.set_thread_safety_checks_enabled(false)
	for x in cosos:
		if x[1]:
			var archivo = FileAccess.open(x[0], FileAccess.READ)
			var info = archivo.get_var()
			archivo.close()
			var img = Image.new()
			img.load(info["Fondo"])
			var itex = ImageTexture.new()
			itex.set_image(img)
			x[1].get_child(4).get_child(0).texture = itex
		else :
			print("Error")

func _exit_tree():
	thread.wait_to_finish()

func recargarLista() :
	for v in get_children() :
		remove_child(v)
	generarLista()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.