开发者

why doesnt my lua class implementation work?

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( .开发者_StackOverflow中文版.. )
            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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜