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 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