Some APIs can not use

I create a new empty project and try to write a new library to parse glb file, and i use API json and bit, but when call these in lua file, always have errors remind
“attempt to index ? (a nil value)”


local M = {}

local glb = {
    magic = "",
    version = 0,
    length = 0,
    chunk0length = 0,
    chunk0type = "",
    chunk0data = "",
    json = json.decode(""),
    chunk1length = 0,
    chunk1type = "",
    chunk1data = "",
    bin = {}
}

some other api work will like math , string…
Do i need add some requires before i use it?

Please share the full error (specifically, the line that’s causing it).

thanks for reply, here is the whole.

ERROR:EXT: Loading /glb2convexshape/glb2convexshape.editor_script failed: @glb2convexshape/glb2convexshape.lua:10 attempt to index ? (a nil value)

glb2convexshape.lua

local M = {}

local glb = {
    magic = "",
    version = 0,
    length = 0,
    chunk0length = 0,
    chunk0type = "",
    chunk0data = "",
    jsonfile = json.decode(""),
    chunk1length = 0,
    chunk1type = "",
    chunk1data = "",
    bin = {}
}

local accessors_type = {
    ["SCALAR"] = 1,
    ["VEC2"] = 2,
    ["VEC3"] = 3,
    ["VEC4"] = 4,
    ["MAT2"] = 4,
    ["MAT3"] = 9,
    ["MAT4"] = 16
}

local function read(path)
    local file = assert(io.open(path, "rb"))
    local data = file:read("*a")
    file:close()
    return data
end

local pos = 1
local num = {[1] = 0, [2] = 0, [3] = 0, [4] = 0}

local function u32_unpack(s, p)
    local u32num = 0
    for i = 1, 4, 1 do
        num[i] = string.byte(s, pos)
        pos = pos + 1
        u32num = u32num + math.pow(0x100, i - 1) * num[i]
    end
    return u32num, pos
end

local function str_unpack(s, p, l)
    return string.sub(s, p, p + l - 1), p + l
end

function M.generate_convexshape(glb_path)
    print(glb_path)
    local glb_file = read(glb_path:sub(2))
    -- print(glb_file)
    
    -- check magic
    glb.magic, pos = str_unpack(glb_file, pos, 4)
    assert(glb.magic == "glTF", "Wrong file type!")
    
    -- check version
    glb.version, pos = u32_unpack(glb_file, pos)
    assert(glb.version == 2, "GLB version incompatibility!")

    --check length
    glb.length, pos = u32_unpack(glb_file, pos)
    -- print(glb.length, pos)
    -- print(string.len(glb_file))
    assert(glb.length == string.len(glb_file), "Incomplete file!")
    
    -- chunk 0 (JSON)
    glb.chunk0length, pos = u32_unpack(glb_file, pos)
    glb.chunk0type, pos = str_unpack(glb_file, pos, 4)
    assert(glb.chunk0type == "JSON", "Chunk 0 type is not JSON") --type check
    -- print(glb.chunk0length)
    glb.chunk0data, pos = str_unpack(glb_file, pos, glb.chunk0length)
    -- print(glb.chunk0data)
    glb.json = json.decode(glb.chunk0data)
    assert(string.sub(glb.json.asset.version, 1, 1) == "2", "json.asset.version not match")
end

return M

glb2convexshape.editor_script

local glb2convexshape = require("glb2convexshape.glb2convexshape")

local M = {}

local function get_extension(path)
    return path:match("%.(%w+)$")
end

function M.get_commands()
    return {
        {
            label = "Generate convexshape file",
            locations = {"Edit", "Assets"},
            query = {
                selection = {type = "resource", cardinality = "one"}
            },
            active = function(opts)
                return get_extension(editor.get(opts.selection, "path")) == "glb"
            end,
            run = function(opts)
                local path = editor.get(opts.selection, "path")
                glb2convexshape.generate_convexshape(path)
            end
        }
    }
end

return M

Ah, that’s important context. Editor scripts run in a whole different environment and normal Defold APIs aren’t available. You’ll only get the regular Lua functions as well as the editor module. Though since the Lua version used in the editor is 5.2, you should also get the bit32 module to replace bit from LuaJIT.

Very thanks for exact explanation, that’s worked, i use bit32 instead of bit, and use third party lib lunajson for json file parse.

1 Like