Difference between revisions of "TsT Module definition"

(Created page with "How to define a LÖVE module ? Exactly like a LUA module. How to define a LUA module ? There is different way to do. In lua 5.1, a module() function exists : Please do not use...")
 
m (Added link to module discussion.)
 
(2 intermediate revisions by one other user not shown)
Line 6: Line 6:
  
 
There is different way to do.
 
There is different way to do.
In lua 5.1, a module() function exists : Please do not use it. It will be deprecated in next version. See this discuss for detail TODO:link
+
In lua 5.1, a module() function exists : Please do not use it. It will be deprecated in next version. [http://lua-users.org/wiki/LuaModuleFunctionCritiqued See this discuss for details.]
  
 +
 +
== simple module definition ==
  
 
A "private"(?) module (not affecting the global environment)
 
A "private"(?) module (not affecting the global environment)
  
 
in file "mymod.lua" :
 
in file "mymod.lua" :
<code>
+
<source lang="lua">
 
local M = {}
 
local M = {}
  
Line 27: Line 29:
  
 
return M
 
return M
</code>
+
</source>
  
 
sample of use in main file :
 
sample of use in main file :
<code>
+
<source lang="lua">
 
local mymod = require("mymod")
 
local mymod = require("mymod")
 
mymod.blah()
 
mymod.blah()
 
mymod.bar()
 
mymod.bar()
</code>
+
</source>
  
  
 
or with global module
 
or with global module
<code>
+
<source lang="lua">
 
mymod = require("mymod")
 
mymod = require("mymod")
 
mymod.blah()
 
mymod.blah()
 
mymod.bar()
 
mymod.bar()
</code>
+
</source>
  
  
 
variant 2
 
variant 2
 
in file "mymod.lua" :
 
in file "mymod.lua" :
<code>
+
<source lang="lua">
 
local M = {}
 
local M = {}
  
Line 62: Line 64:
  
 
mymod = M -- create the global "mymod" variable
 
mymod = M -- create the global "mymod" variable
</code>
+
</source>
  
 
sample of use in main file :
 
sample of use in main file :
<code>
+
<source lang="lua">
 
require("mymod")
 
require("mymod")
 
mymod.blah()
 
mymod.blah()
 
mymod.bar()
 
mymod.bar()
</code>
+
</source>
 +
 
 +
 
 +
== more complicated module definition ==
 +
 
 +
<source lang="lua">
 +
local M = {} -- module namespace
 +
local func = {}
 +
 
 +
local function blah()
 +
 
 +
end
 +
 
 +
local function bar()
 +
 
 +
end
 +
 
 +
func.foo = blah
 +
func.bar = bar
 +
 
 +
M.func = func
 +
 
 +
M.foo = function(...) return func.foo(...) end
 +
M.bar = function(...) return func.bar(...) end
 +
 
 +
return M
 +
</source>
 +
 
 +
TODO: take sample from luajail/lovejail project

Latest revision as of 21:19, 6 April 2012

How to define a LÖVE module ?

Exactly like a LUA module.

How to define a LUA module ?

There is different way to do. In lua 5.1, a module() function exists : Please do not use it. It will be deprecated in next version. See this discuss for details.


simple module definition

A "private"(?) module (not affecting the global environment)

in file "mymod.lua" :

local M = {}

local function blah()

end

local function bar()

end

M.foo = blah
M.bar = bar

return M

sample of use in main file :

local mymod = require("mymod")
mymod.blah()
mymod.bar()


or with global module

mymod = require("mymod")
mymod.blah()
mymod.bar()


variant 2 in file "mymod.lua" :

local M = {}

local function blah()

end

local function bar()

end

M.foo = blah
M.bar = bar

mymod = M -- create the global "mymod" variable

sample of use in main file :

require("mymod")
mymod.blah()
mymod.bar()


more complicated module definition

local M = {} -- module namespace
local func = {}

local function blah()

end

local function bar()

end

func.foo = blah
func.bar = bar

M.func = func

M.foo = function(...) return func.foo(...) end
M.bar = function(...) return func.bar(...) end

return M

TODO: take sample from luajail/lovejail project