为什么我的lua类实现不起作用?
我已经在lua环境中实现了OOP,但它似乎不起作用 我认为这与我如何处理_索引以及我对require和module的不当使用有关,但我不是100%确定 代码如下:为什么我的lua类实现不起作用?,lua,Lua,我已经在lua环境中实现了OOP,但它似乎不起作用 我认为这与我如何处理_索引以及我对require和module的不当使用有关,但我不是100%确定 代码如下: Class = function( prototype ) local derived = {} local derivedMT = { --When indexing into a derived class, check the base class as well __in
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
这里是我如何使用它的,nil引用是对基类函数的调用,这是我遇到的错误/问题,因为基类似乎没有被引用
require "Core.Camera.FreeCamera"
local T = Core.Camera.FreeCamera.FreeCamera(0,0,-35)
c = T:getObjectType() -- nil reference
print(c .." --Type" )
这是Camera.lua的基类
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
最后是尝试继承摄影机的自由摄影机
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
localrequire=require
本地_G=_G
模块(…)
需要“核心。摄像机。摄像机”
本地M=\u G.Class(\u G.Core.Camera.Camera)--格罗斯、拉姆可能是罪魁祸首
自由摄影机=M
函数M:_构造函数(x,y,z)---哎呀,这确实被调用了。。。另一个没有
self.Active=false
self.x=x
self.y=y
self.z=z
self.ID=\u G.EngineManager:getCamera()
--_G.print(“获得的Id为:“…self.Id”)
self:setCameraPosition(x,y,z,self.ID)
_G.打印(“”)
终止
我的点子快用完了。任何帮助都将不胜感激。而不是:
local M = _G.Class( _G.Core.Camera.Camera )
您需要:
local M = _G.Class( _G.Core.Camera.Camera.Camera )
第一个Camera
是目录名,第二个是模块名,第三个是类名
编辑:您可以像这样清理它:
local CameraModule = require "Core.Camera.Camera"
local M = _G.Class( CameraModule.Camera )
抱歉,免费摄像机构造器确实会被调用…摄像机不工作。。。那太恶心了。。。非常感谢你的帮助。。。我希望有办法把它清理干净。