Hi I’m trying import images and store them in an array.
But I’m failing. I googled myself stupid.
ResourceLoader does also not wor.
Can anyone help me?
var textures = []
func _ready() -> void:
var dir = DirAccess.get_files_at("res://images/")
for file in dir:
var new_texture = ResourceLoader.load("res://images/" + file)
textures.append(new_texture)
i import them in a Global Script and them use a other script to set a box texture:
func _ready() -> void:
var textures = $"/root/Global".textures
var new_material = StandardMaterial3D.new()
new_material.albedo_texture = textures[randi() % textures.size()]
$box.material = new_material
Probably is not working because you’re getting the null variables that will not work, you need to filter your results before add them to the list:
var textures = []
func _ready() -> void:
var dir = DirAccess.get_files_at("res://images/")
for file in dir:
var new_texture = ResourceLoader.load("res://images/" + file)
# Check if the loaded file is indeed a texture
if new_texture is Texture2D:
# Now we can add them for the array
textures.append(new_texture)
func _ready() -> void:
var textures = $"/root/Global".textures
var new_material = StandardMaterial3D.new()
# Use Array.pick_random()
new_material.albedo_texture = textures.pick_random()
$box.material = new_material
It seems like you set a wrong type to the texture.
Try like the following.
var new_material = StandardMaterial3D.new()
var new_textures = textures[randi() % textures.size()]
new_material.albedo_texture = ImageTexture.create_from_image(new_textures)
# ...do whatever you want with your box...
WARNING: SDFGI can only be enabled when using the Forward+ rendering backend.
at: environment_set_sdfgi (servers/rendering/storage/environment_storage.cpp:696)
ERROR: No loader found for resource: res://images/chiseled_stone_bricks.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/coarse_dirt.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/cobblestone.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/cracked_stone_bricks.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/diorite.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/dirt.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/granite.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/gravel.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/mossy_cobblestone.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/mossy_stone_bricks.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/rooted_dirt.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
ERROR: No loader found for resource: res://images/stone.png.import (expected type: )
at: _load (core/io/resource_loader.cpp:291)
[<Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>, <Object#null>]
Ok, that explains everything, when you export your game, Godot rename and remap all the files inside the project, so you can’t find them like that. For this case you can use ResourceLoader.list_directory (Godot 4.4 dev 5 or more is required)
As alternative, you can try remove the .import from the files names but i’m not sure if that will work for every file type:
var textures = []
func _ready() -> void:
var dir = DirAccess.get_files_at("res://images/")
for file in dir:
file = file.replace(".import", ""))
var new_texture = ResourceLoader.load("res://images/" + file)
# Check if the loaded file is indeed a texture
if new_texture is Texture2D:
# Now we can add them for the array
textures.append(new_texture)
I was about to post about a similar issue. I’m making a breakout clone (in version 4.3) with what will eventually be 100 stages, and I created “stage selector” menu with TextureButtons that have a thumbnail of the stage set as its texture_ normal. I was doing this:
func load_thumbnails_old():
for stage in range(Global.total_stages):
var image_path = "res://assets/images/thumbnails/stage" + str(stage) + ".png"
var image = Image.new()
image.load(image_path)
var texture = ImageTexture.create_from_image(image)
thumbnails.append(texture)
This was the only way I could figure out how to get my thumbnails loaded into a list without it causing an error, but my debug output was filling up with warnings saying that this would not work if I exported my project, so I changed it to this using your advice and it works great, much appreciated!
func load_thumbnails():
var dir = DirAccess.get_files_at("res://assets/images/thumbnails/")
for file in dir:
file = file.replace(".import", "")
var new_texture = ResourceLoader.load("res://assets/images/thumbnails/" + file)
# Check if the loaded file is indeed a texture
if new_texture is Texture2D:
# Now we can add them for the array
thumbnails.append(new_texture)
I guess as long as I don’t put anything else in that thumbnails folder and they’re named in order like that it wont be a problem. I have a separate “utility” scene called thumbnailer where it loads in each stage in front a camera attached to a SubViewport and then dumps the images out one by one into that folder using a timer to make sure everything loads up correctly. Glad I found this.
Ahh it seems like I spoke too soon. If I do a web export of my game the thumbnails are getting all moved out of order. I’ll have research why this is happening and then I post back when I figure it out.
Ok I figured it out, it was because I was creating my thumbnails 1 thru 9 like this stage1.png, stage2.png, instead of stage01.png, stage02.png, and so those were getting mixed up in the sort order.
I also upgraded to 4.4 and my new function looks like this:
func load_thumbnails():
thumbnails.clear()
var dir = ResourceLoader.list_directory("res://assets/images/thumbnails/")
for file in dir:
if file.ends_with(".png"):
var new_texture = ResourceLoader.load("res://assets/images/thumbnails/" + file)
if new_texture is Texture2D:
thumbnails.append(new_texture)
I’ve only been working in Godot for about 2 weeks now and I’m loving it so far, this was really the first thing I had really go look for help on that I couldn’t immediately figure out from reading the docs.
Good idea. I’m finding out the hard way that the 4.4 export templates are not exactly production ready yet. Everything works fine within the editor but when I export to Web or Windows Desktop my menus are all broken and timers are messed up, so I’m in process of downgrading my project back to 4.3 right now. Doesn’t look like its going to be too bad.
Update here - I may have been mistaken here about why these problems with the 4.4 export were occurring. It could have been because I didn’t empty my browser cache before doing a web export in 4.4 and for some reason it was still using the cached 4.3 version of the wasm file.
So you still have this issue right? Now that I’ve downgraded back to 4.3 I’m having the same problem. Works fine in the editor but when I do a web build none of the images are found. Let me know if you find a solution and I’ll do the same.
This seems like it would be an extremely common issue, I’m surprised its so unintuitive. You load some images from your resources folder, put them in a array and then apply them to buttons or whatever objects at runtime.
Everything works fine, but then you publish to the web and all your images can’t be found and you get errors in the web console. I find it hard to believe this is even an issue. Loading images and using them in a game is one of the most fundamental aspects of creating a game. I don’t understand why this from end users perspective is so confusing, I still don’t fully understand why its failing.
If the engine wants to package up these files, rename them, compress them into an archive, whatever it wants to do, why isn’t there a connection being made between the original files you’re using in your project and the data that needs to be loaded when the project gets loaded from an exported build? Why should the end user have to jump through hoops, find workarounds or whatever just to get this to work? Seems like the default behavior should be to preserve what your code does when its working in the editor and not something different when you publish to a different platform.