Search code examples
c#.netlualuainterface

GetFunction returns null luainterface


I have this script loaded in my C# program

function test()
    print ("A")
end

but when I try to invoke it

LuaFunction func = lua.GetFunction("test")

func.call()

I get the problem that func is null.

What do I wrong?


Solution

  • You say you have loaded the test script into the C# program. This is not enough. You have to execute the resulting chunk code so that the global test variable gets assigned.

    Always reminder that

    function test()
        print ("A")
    end
    

    is only a syntactic sugar for:

    test = function()
        print ("A")
    end
    

    When Lua loads some code, it just compiles the source code into bytecode.The affectation test = function() end is only executed at runtime, not at compile time.