While LUA is not an object oriented programming language, you can mimick your code to act like one. The key here is with the setmetatable() function.
Here is a .lua file containing a simple class.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
local Object = {} local Object_mt = { __index = Object } function Object:new() local obj = { variable1 = nil, variable2 = 'sample', variable3 = false } return setmetatable(obj , Object_mt) end |
That’s it! That is all there is to it. Save it as object.lua and now you can simply create a new instance of your class like this:
|
1 2 3 |
local Object = require('object') local myobj = Object:new() print('the value of variable2 is ' .. myobj.variable2) |