# Multiple files and Lua

This topic is 3733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to compile some simple OOP samples from the Lua wiki, and there appears to be some problems involving scope. Here's the code: System.lua:
module("system", package.seeall)

--
function extends(base_class)
local new_class = { }
local class_mt = { __index = new_class }

--
function new_class:create()
local new_inst = { }
setmetatable(new_inst, class_mt )
return new_inst
end

if base then
setmetatable(new_class, { __index = base_class } )
end

return new_class
end


Object.lua:
require "system"

module("object", package.seeall)

object = extends(nil)

--
function object:set_name(name)
_name = name
return
end

--
function object:get_name()
return _name
end


Boot.lua:
require "object"

--
local function boot()
local test

test = object:create()
test:set_name("Test_Object_01")

print(test:get_name())
return
end


The error I get back from attempting to run this program (starting at boot()) is that an attempt to call method 'create' failed because of a nil value. I'm thinking this is a result of how I'm linking / 'requiring' the modules. Any ideas?

##### Share on other sites
I don't think I'm being insane here, but I think that 'extends' in System ends up in the 'system' table; which means this might well be causing things to fail when you import the 'object.lua' as you are calling 'extends' not 'system.extends'.