Everyday I learn new things about Lua, and today I learnt how to utilize the :method() syntax and rebuilt the Include example to demonstrate how to use it.
Include
In Maiden, look at the tenth example file of this tutorial.
-
Navigate to the tenth example with
code > tutorial > A_include.lua.
-
Run the script by clicking on the play button to the top right of the Maiden window.
-
Rotate the knobs to change the position values of the included file.
-
Press the key to select a different view.
Including files with local view = include('lib/view'), will first look in the directory of the current script. This allows using relative paths to use libraries local to the script. The returned value of the included script will be available in your main script file.
-- lib/target.lua
return {
value = 5
}
-- main script
local target = include('lib/target')
print(target.value)
In lua, you can create new objects and methods as described the following snippet, notice how the self parameter is omitted when using the colon character before the method name.
obj = { c = 4 }
obj.add = function(self,a,b)
return a + b + self.c
end
obj:add(2,3) -- 9