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?
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 ")
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.
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.
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")