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:
the print statement in the above code produce this
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.
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
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.
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)
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?
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)