How to make namespace in lua?
11,931
Solution 1
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
Solution 2
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.
Related videos on Youtube
Comments
-
codevania over 3 years
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.
-
Goles almost 13 yearsRemember that if you declared: Foo.Func = function(this) ... end you can call it: Foo:Func() ( notice the ':' ) , this will pass the Foo table as the first function parameter too.