Bad argument #6 to resource.create_atlas

I’m trying to create an atlas at runtime, here’s my code:

local file = assert(io.open("main/sketch.png", "rb"))
local data = assert(file:read("*a"))
local image = image.load(data)
print("Type: " .. image.type)
print(image.width, image.height)

local tex_params = {
	width = image.width,
	height = image.height,
	type = resource.TEXTURE_TYPE_2D,
	format = resource.TEXTURE_FORMAT_RGB
}
local texture_resource = resource.create_texture("/sketch.texturec", tex_params, image.buffer)

local atlas_params = {
	texture = texture_resource,
	animations = {
		{
			id = "anim",
			width = image.width,
			height = image.height,
			frame_start = 1,
			frame_end = 2
		}
	},
	geometries = {
		vertices = {
			0, 0,
			0, image.height,
			image.width, image.height,
			image.width, 0
		},
		uvs = {
			0, 0,
			0, image.height,
			image.width, image.height,
			image.width, 0
		},
		indices = {0, 1, 2, 0, 2, 3}
	}
}
local atlas_resource = resource.create_atlas("/sketch_atlas.texturesetc", atlas_params)

I’ve read through it several times and can’t seem to find any issue with the code. Also why does it error on argument 6 when create_atlas only has 2 arguments?

Here’s the error:

ERROR:SCRIPT: main/main.script:43: bad argument #6 to 'create_atlas' (number expected, got string)
stack traceback:
  [C]:-1: in function create_atlas
  main/main.script:43: in function <main/main.script:1>

The image type that gets printed is “rgb”. The texture itself seems to be created properly, just not the atlas.

The create_atlas function also checks all of the atlas_params values so there’s got to be something in there that it doesn’t like. Can you share a project where this happens?

Here’s the empty project I was testing in. Just press build.

CreateTextureRepro.zip (131.6 KB)

I’m using Defold version 1.4.5 on macOS 13.3.1.

Ah, you were missing the inner table in geometries:

   -- WRONG
        geometries = {
            vertices  = {
               ...
            },
            uvs = {
                ...
            },
            indices = {
                ...
            },
        }

-- RIGHT
        geometries = {
            {
                vertices  = {
                   ...
                },
                uvs = {
                    ...
                },
                indices = {
                    ...
                },
            }
        }
1 Like