• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement