tags:

views:

207

answers:

1

I have implemented OOP in my lua environment but it doesnt seem to be working.

I think it has something to do with how i am handling the __index and my improper use of require and module but I'm not 100% sure.

Here is the code:

    Class = function( prototype )
    local derived = {}
    local derivedMT = {
        --When indexing into a derived class, check the base class as well
        __index = prototype,

        --When invoking a class, return an instance
        __call = function( proto, ... )
            local instance = {}
            local instanceMT = {
                --When indexing into an instance, check the class hierarchy as well.
                __index = derived,
                --Calling instances is a no-no!
                __call = function() 
                    print( "WARNING! Attempt to invoke an instance of a class!" )
                    print( debug.traceback() )
                    return instance
                end,                
            }
            setmetatable( instance, instanceMT )

            if ( instance.__constructor ) then
                instance:__constructor( ... )
            end

            return instance
        end,
    }
    setmetatable( derived, derivedMT )
    return derived  
end

And here is how I use it, the nil reference is a call to a base class function, that is the error/problem im having is it seems like the base class isn't being referenced.

require "Core.Camera.FreeCamera"

local T = Core.Camera.FreeCamera.FreeCamera(0,0,-35)

c = T:getObjectType() -- nil reference
print(c .." --Type" )

and here is Camera.lua the base class

local _G = _G
module(...)



local M = _G.Class()
Camera = M

function M:__constructor(x,y,z) --This never gets called.
    --self.Active = false
    --self.x = x
    --self.y = y
    --self.z = z
    --self.ID = EngineManager:getCamera()
    print("InCameraInstance_-_-_-_-_-__-_--_-__-_-_-_--_-_-_-_-_--_-_-_--_--__-_---_--_---__-")
end

function M:getObjectType()
    return "camera"
end

And Finally Free Camera which attempts to inherit Camera.

local require = require
local _G = _G

module(...)

require "Core.Camera.Camera"

local M = _G.Class( _G.Core.Camera.Camera ) --Gross, lame might be the culprit
FreeCamera = M

function M:__constructor(x,y,z) ---WHOOPS, this does get called... the other one doesnt
self.Active = false
    self.x = x
    self.y = y
    self.z = z
    self.ID = _G.EngineManager:getCamera()
    --_G.print("got Id of:" .. self.ID)
    self:setCameraPosition(x, y, z, self.ID)
    _G.print("<<<Camera in lua>>>")
end

I'm running out of ideas. any help would be appreciated.

+5  A: 

Instead of:

local M = _G.Class( _G.Core.Camera.Camera )

you need to have:

local M = _G.Class( _G.Core.Camera.Camera.Camera )

First Camera is the directory name, second is the module name, third is the class name.

Edit: You can clean it up a bit like this:

local CameraModule = require "Core.Camera.Camera"
local M = _G.Class( CameraModule.Camera )
interjay
Wow... that is nasty... thanks so much for the help... i wish there was a way to clean it up.
Joe
@Joe, there is a way -- see the edit.
interjay
Sweet, thanks a bunch.
Joe