Resourceloader.load() wont load png at User:// as texture

Godot Version

4.2

Question

I am trying to implement a function that allows me to add ingame screenshot on save slot when saving game. Now the first part is done but the second part of loading the screenshot on the respective save slot when reopening the game did not turn out as I expected.
here is my code:

	var find_pic_at = "user://"+str(self.get_index()) + ".png"
	print("user://"+str(self.get_index()) + ".png")
	$save_display.texture = ResourceLoader.load(find_pic_at)
    print(DirAccess.get_files_at("user://"))
	print(ResourceLoader.load(find_pic_at))

the print statement in the above code produce this
image
so my code is at the correct location but somehow resourceloader just wont find the image. I have a similar function somewhere else in my project which find texture from res:// and works perfectly, which makes things more confusing. Please help me out.

Are you sure the file exists in that location?

hmm… the print statement I added in my code did say that files at “user://” includes 0.png, 1.png and 2.png and that the path I am searching for is user://0.png. so my file should exists

1 Like

Oh, that is only a string that you print, check if it is exist

when I use file exists or dir exists it give me an error called “Parser Error: Cannot call non-static function “dir_exists()” on the class “DirAccess” directly. Make an instance instead.” and when I use dir_exists_absolute it returns false.

Its means the file not exist in that location. Do you saved this by codes in that location?

yes, I saved them using save_png method which save the screenshot to a png

I am not sure, but try to use load function?

Note: Files have to be imported into the engine first to load them using this function. If you want to load Images at run-time, you may use Image.load().

from the load function docs. Try using Image.load instead. For reference Godot’s importing process includes converting images into webp files inside the hidden .godot folder.

var find_pic_at = "user://"+str(self.get_index()) + ".png"

var image: Image = Image.new()
var load_err := image.load(find_pic_at)
assert(load_err == OK, "Failed to load pic at: " + find_pic_at)

var texture: Texture2D = ImageTexture.create_from_image(image)
1 Like

just tried that and I think its a valid approach, although there is still something wrong with my code.
At first I changed it into this

	var find_pic_at = "user://"+str(self.get_index()) + ".png"
	$save_display.texture =  Image.load(find_pic_at)

which gives me error called “Parser Error: Cannot call non-static function “load()” on the class “Image” directly. Make an instance instead.”
I then modify the code into this:

	var find_pic_at = "user://"+str(self.get_index()) + ".png"
	var pic = Image.new()
	$save_display.texture = pic.load(find_pic_at)

which gives me another error called "Invalid set index ‘texture’ (on base: ‘TextureRect’) with value of type ‘int’."but surely I have input a string?

I updated my post with an example, there are a few steps to process the image into a texture. Image.load returns a error code (int).

I have zero idea about how this code works but it solved my issue :joy:. ty so much!

1 Like

It could be shortened, I like the error checking. The technical difference between an Image and a Texture is that one is on the GPU and the other in CPU memory. Here’s the shortened version, with comments.

# Loads the image into CPU memory
var image := Image.load_from_file(find_pic_at)

# Creates a texture, copies the image from memory into the GPU memory
var texture := ImageTexture.create_from_image(image)
1 Like

now I get it, ty again!

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