Search code examples
c#.netlualuainterface

How do you pass an object from c# to lua's function?


I'm using Lua interface on c# to pass an object I created to lua's function. It successfully calls the function, but lua is keep throwing an error:

LuaInterface.LuaException: /hook.lua:32: attempt to index local 'objj' (a nil value)

This is the c# code:

public class PerObj
{
    public string name;
    public PerObj() 
    {
    }
}

PerObj obj = new PerObj();
LuaFunction lf = lua.GetFunction ("item.HookMe");
lf.Call(obj);

And here's the lua code:

function item:HookMe(objj)
    objj.name= "lalala"
end

The function is actually being called, but I'm not sure it's not working...


Solution

  • Change the function definition to:

    function item.HookMe(objj)
        objj.name= "lalala"
    end
    

    The colon in the original definition means that the function has also the self parameter. Those function are called like this: object:HookMe(). But you want to call it directly, so the colon is not applicable.

    Edit:
    If you want to keep the function defininition and retain self, call it like this:

    lf.Call(null, obj);
    

    To call it passing also the self object:

    lf.Call(lua["item"], obj);