'set_texture' (buffer expected, got string)

Hi. I want to change textures of sprite/model during execution, loading them from disk. I loaded textures, it is read, but why I have an error?

image_path = "D:/0001.jpg"

local file = io.open(image_path, "rb")
if not file then
	return
end
local buffer = file:read("*all")
file:close()
local img = image.load(buffer)
local tab = {
	type = resource.TEXTURE_TYPE_2D,
	format = resource.TEXTURE_FORMAT_RGB,
	width = img.width,
	height = img.height
}
pprint (tb)
resource.set_texture(go.get("#sprite", "texture0"), tab, img.buffer)

DEBUG:SCRIPT:
{ --[[0000000002B550C0]]
type = 3553,
format = 2,
height = 1583,
width = 1110
}
ERROR:SCRIPT: /main/t.script:21: bad argument #3 to ‘set_texture’ (buffer expected, got string)
stack traceback:
[C]: in function ‘set_texture’
/main/t.script:21: in function </main/t.script:1>

thanks for your reply! Please tell me how to do it right

you write gui.set_texture, and I want to change the texture of sprite. With regard to the substitution of the textures for the gui this is what I do. But I want to change the textures of sprites and models, loading them from the disk.

Defold’s image module doesn’t work for models, because it packs image data into a string rather than a buffer object.
To load images for models use my extension https://www.defold.com/community/projects/134216/

I used your asset, but it loads only those textures that are in the resources (or am I wrong?), I could not load the texture from the disk

Unfortunately, we haven’t really adopted the buffer objects on a wider scale.
We plan to thouh, to make things like this easier.

If you look at the documentation for resource.set_texture() you see an example that you can use. Copy the data from your string to the buffer.

It’s a bit in efficient, but should at least work. (e.g. we should add some copy function so that you can provide a file you’ve read from disc, as in your example).

Resources are stored on the disk. Or what do you mean? If you have a path to a file, you can load it with my extension.

local data = sys.load_resource("D:/0001.jpg")

local image_resource = imageloader.load{
	data = data
}

resource.set_texture(go.get('#model', 'texture0'), image_resource.header, image_resource.buffer)

So I can load the texture?

Use filename param directly. No need to read it in a string first.

local image_resource = imageloader.load{
	filename = "D:\0001.jpg"
}
2 Likes

And it really works! Thank you very much for your help. Strange why such a simple thing I have not worked

1 Like

Thanks for your help. I love Defold, but until now I’ve had problems loading textures and some projects I’ve had to do on other engines because of it. To be honest, Defold lacks ready-made tools for working with 3D, light sources and shadows. For example, I’m currently working on a 3D clothing Configurator for the site, and instead of creating light and shadow, I use baked light and shadow and combine them with the texture in the Shader. This of course makes the project more optimized, but I would like to have built-in tools for working with 3D.
There is still not enough opportunity in the code to influence the bones for the 3d model(for example, to increase the 3d character body proportions)

2 Likes