I keep getting an "attempt to index global 'Foo' (a nil value)" error.
Say I want to create a class using a module named 'Foo'. To create an instance of the Foo class, I want to call Foo:new() from main.lua. I know I have to work with metatables and all that, but the error appears to precede that stuff.
If foo.lua is basically
1 2 3 4 5 6 7 8 9 10 11 | module(...,package.seeall) Foo = {} Foo_mt = {__index = Foo} function Foo:new(params) local self = {} setmetatable(self, Foo_mt) ... return self end |
Nope, should be
1 | foo.Foo:new() |
on ph, but hopefully this helps
file main.lua
.........
require("foo")
testobj = foo:new()
.....
file foo.lua
.......
module(...,seeall)
function new()
local somevar =1
fooobject = display.newRect(params here)
fooobject.newvar = somevar
return fooobject
end
.....
hopefully that made sense, i am using iphone and can see 4 lines :) and cant check, all from memory, and im a noob.
the module lua is referred to as foo in main.lua, so foo:new() calls the new function in foo.lua that returns a modified display object
regards :)
I'll give this a try, thanks. I'd be interested in what you learned about 'require', too.
thx
check out my particle fx module source code i uploaded last night, uses class modules and config objects
Done. Read it here.