Freezes for seconds when loading a single image from an external path

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gamer1998

What I’m trying to do is change the texture of the TextureRect node by uploading a new 600x600 jpg image from a folder in the device that contains a lot of images of the same size (about ten thousand images) when the button is clicked, using the database (sqlite) and the image_index variable, while there is a VideoPlayer node It works as an animated background.
The problem is when I press the button, it freezes for two or three seconds before loading the next image.

I would be grateful if examples are provided to fix the problem, since I’m actually new to godot


extends Sprite

const SQLite = preload("res://addons/godot-sqlite/bin/gdsqlite.gdns")
onready var my_database = SQLite.new()
var image_index = 0

func _ready():
	my_database.path = "res://images_data/images.db"
	my_database.open_db()
	my_database.query("SELECT id FROM datas ;")

func _on_Button_pressed():
	var test_image = Image.new()
    var test_texture_image = ImageTexture.new()
    image_index +=1
    test_image.load("/home/admin/Downloads/images/"+str(my_database.query_result[image_index]["id"])+".jpg")
    test_texture_image.create_from_image(test_image)
    get_node("../TextureRect").texture = test_texture_image

Do you know which part of the above code is slow? For instance, is it:

  • The sqlite query?
  • The image.load() call?
  • The create_from_image() call?

I’d probably split that sqlite query out as an independent line of code (so it can be easily analyzed separately) and then see where the delay is actually coming from. Once you know that, it’ll give a better idea of how to improve it.

jgodfrey | 2022-07-19 16:46

Sorry for the late reply, and thank you very much.
I took your advice, and after several attempts I came to a solution that is not perfect but acceptable.

It works fine, but the video in the video player node is getting a decrement in frame rate every time I click the load image button.

I’m going to ask a new question about that because it might be a different topic.

thank you very much.


extends Sprite

const SQLite = preload("res://addons/godot-sqlite/bin/gdsqlite.gdns")
onready var my_database = SQLite.new()
var image_index = 0


 func _ready():
     my_database.path = "res://images_data/images.db"
     my_database.open_db()
     my_database.query("SELECT id FROM datas ;")
     var database_result = my_database.query_result
     my_database.close_db()

func _on_Button_pressed():
     var test_image = Image.new()
     var test_texture_image = ImageTexture.new()
     image_index +=1
     test_image.load("/home/admin/Downloads/images/"+str(database_result[image_index]["id"])+".jpg")
     test_texture_image.create_from_image(test_image)
      get_node("../TextureRect").texture = test_texture_image

gamer1998 | 2022-07-22 20:59