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...")
 
(source tag)
Line 12: Line 12:
  
 
in file "mymod.lua" :
 
in file "mymod.lua" :
<code>
+
<source lang="lua">
 
local M = {}
 
local M = {}
  
Line 27: Line 27:
  
 
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 62:
  
 
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>

Revision as of 16:55, 12 May 2011

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 detail TODO:link


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()