Saved image from path won't display

So in my code I get a list of images from a list of folders, and display those on screen, you can also import your own images for this

I use this code for that:

func _load_images():
	var dir = DirAccess.open("res://Games")
	folders = dir.get_directories()
	for folder in folders:
		var spr = Sprite2D.new()
		spr.texture = load("res://Games/" + folder + "/progress.png")
		$images.add_child(spr)
		
		var x_pos = 0
		var y_pos = 0
		
		var needed_scale =  float(480) * float(spr.scale.x) / float(spr.texture.get_width() ) 
		spr.scale = Vector2(needed_scale, needed_scale)
        spr.position=Vector2(x_pos, y_pos)

This runs correctly on startup.
But after I switch scenes to add an image and save it to itā€™s own folder in res://Games/, the code crashes on the line

var needed_scale =  float(480) * float(spr.scale.x) / float(spr.texture.get_width() ) 

The error states:

ā€œAttempt to call function ā€˜get_widthā€™ in base ā€˜null instanceā€™ on a null instanceā€

I presume this is because there is no .import file, since it does get added when I launch it again

Is there any way to fix this, either by making an .import file, or calling and saving the image without needing an .import?

Probably the issue is due to the image not being fully imported or available when you switch scenes.

Check for Null Texture: Ensure that the texture is not null before accessing its properties:

if spr.texture:
    var needed_scale = float(480) * float(spr.scale.x) / float(spr.texture.get_width())
    spr.scale = Vector2(needed_scale, needed_scale)
    spr.position = Vector2(x_pos, y_pos)
    $images.add_child(spr)
else:
    print("Failed to load texture: ", texture_path)

Use ResourceLoader.load(): This can help with loading resources more reliably:

var texture = ResourceLoader.load(texture_path, "", false)

This prevents the code from crashing, however I do want these images to show up.

The if statement error result is this

No loader found for resource: res://Games/2024_8_6_12_43_30/progress.png (expected type: )

can I manually add a loader?

also the Resource loader canā€™t take the third argument

Cannot pass a value of type ā€œboolā€ as ā€œResourceLoader.CasheModeā€

It looks like the error happens because Godot isnā€™t recognizing the image file as a valid resource yet. This usually happens when the image isnā€™t fully imported into the engine.

To fix this:

Disable Resource Caching: Use ā€œResourceLoader.load()ā€ without caching to force Godot to load the image directly from disk:

var texture = ResourceLoader.load(texture_path, "", ResourceLoader.CacheMode.DISABLE)

Ensure Import: Sometimes, Godot needs a little nudge to recognize new files. You can try reopening the project, or adding a short delay between adding the image and trying to load it.

like this I guess

var texture = ResourceLoader.load("res://Games/your_folder/progress.png", "", ResourceLoader.CacheMode.DISABLE)

if texture:
    # Your code to add and display the image cuz I'm to lazy to hack your pc and find the files 
else:
    print("We failed successfully ")

ResourceLoader.CacheMode.DISABLE

also does not exist.

In the docs it says ResourceLoader has three options: Ignore, reuse and replace.
I already tested it, but neither of these three work

1 Like

My badā€”ResourceLoader.CacheMode.DISABLE doesnā€™t exist. The actual options are ā€œIGNOREā€, ā€œREUSEā€, and ā€œREPLACEā€, but since those didnā€™t work for you, the issue might require a different approach.

Try manually re-importing the image or restarting the project to ensure itā€™s fully recognized. If the problem continues, you might need to look into forcing a directory scan or using Godotā€™s editor commands to refresh assets.

I cannot find a function on the docks on how to manually create an import file, is this even possible?

1 Like

You can try triggering a reimport by restarting the editor, using the ā€œReimportā€ option, or refreshing the file system. Unfortunately, thereā€™s no built-in function to manually create import files, so these workarounds are your best bet.

1 Like

Ok, for some reason I got it working now in a far simpler way.
All I needed to do was load the image before I set it to a texture, and it works without needing to restart after I save a new image

func _load_images():
	var dir = DirAccess.open("res://Games")
	folders = dir.get_directories()
	for folder in folders:

		var spr = Sprite2D.new()
		var image = Image.new()
        var x_pos = 0
		var y_pos = 0

		image.load("res://Games/" + folder + "/progress.png")		
		spr.texture = ImageTexture.create_from_image(image)	

        #Scaling and repositioning code so they get displayed nicely		
		
		if spr.texture:				
			spr.position=Vector2(x_pos, y_pos)
			$images.add_child(spr)
		
		else:
			print("failed to load")
		

Itā€™s always those little things

1 Like

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