0

我有一个 Go Struct 实例,并希望使用 GopherLua 将该实例传递给 Lua 方法。

我的 Go 代码是这样的:

dog := new(Animal)

runParam := lua.P{
    Fn:      L.GetGlobal("run"),
    NRet:    1,
    Protect: true,
}

mt := luar.MT(context.AppContext.LuaVM, dog)
userData := &lua.LTable{Metatable: *mt}
userData.Append(&lua.LUserData{Value: dog, Metatable: mt, Env: mt.LTable})

err = L.CallByParam(runParam, lua.LString("One"), userData)
if err != nil {
    fmt.Println("Error while calling lua method: " + err.Error())
}

在我的 Lua 方法中,访问 Animal 参数的属性会尝试索引非表对象时出错。我的 Lua 是这样的:

function run(newName, ent) {
    print(ent.Name)
}

请问我做错了什么?传递其他类型(字符串,int)的参数可以正常工作。

4

1 回答 1

0

在这里,您有一个完整的示例如何访问文档中的结构成员:https ://github.com/yuin/gopher-lua#user-defined-types

于 2019-03-23T18:11:05.557 回答