How to make namespace in lua?
I want to bind static class function to lua. As you know, static class function is something difference with class function. So function call code in lua should be like this...
开发者_如何学运维
//C++
lua_tinker::def(L, "Foo_Func", &Foo::Func);
//Lua
Foo_Func()
But I want to call function in lua like this
//Lua
Foo.Func()
Is there any way to use like that? Lua table might be helpful. But I cannot find any references.
Yes, that would be done with a table and is in fact how most modules work when you import them with require
.
Foo = {} -- make a table called 'Foo'
Foo.Func = function() -- create a 'Func' function in stored in the table
print 'foo' -- do something
end
Foo.Func() -- call the function
I think you'll find PiL chapter 26.2 most interesting. If you compile your library to the same name as the table (so filename == modulename) then you can simply require() the module.
精彩评论