0

I have an instance of a Go Struct and will like to pass the instance to a Lua method using GopherLua.

My Go code goes like this:

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())
}

In my Lua method, accessing the properties of the Animal parameter gives error attempt to index a non-table object. My Lua goes like this:

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

Please what am I doing wrong? Passing other type (string, int) of parameter works fine.

Sulaiman Adeeyo
  • 485
  • 6
  • 19

1 Answers1

0

Here you have a full example how to access struct members in the documentation : https://github.com/yuin/gopher-lua#user-defined-types

antham
  • 408
  • 4
  • 15