So I’m trying to port something across from Mat Buckland’s excellent (and thoroughly recommended) Game AI by Example. All of the code is written in C++, which hasn’t been an issue until now.
Essentially I’m trying to port the triggering example. The class hierarchy is as follows:
BaseGameEntity
|
Trigger
|
Trigger_LimitedLifeTime
|
Trigger_SoundNotify
As far as I can tell this isn’t multiple inheritance, but merely a hierarchy of inheritance so I’ve just employed the standard lua OOP approach:
main.lua
[lua]
local soundTrigger = Trigger_SoundNotify.new(1)
[/lua]
[lua]
Trigger_SoundNotify.new = function(source, range)
local o = Trigger_LimitedLifeTime:new(8)
o.m_pSoundSource = source
o:AddCircularTriggerRegion(o:Pos(), o:BRadius())
setmetatable(o, Trigger_SoundNotify_mt)
return o
end
[/lua]
[lua]
Trigger_LimitedLifeTime.new = function(lifetime)
local o = Trigger.new(BaseGameEntity:GetNextValidID())
–
o.m_iLifetime = lifetime
setmetatable(o, Trigger_LimitedLifeTime_mt)
return o
end
[/lua]
[lua]
local Trigger = {}
local Trigger_mt = {__index=Trigger}
…class method declarations…
Trigger.new = function(id)
local o = BaseGameEntity:new(id)
o.m_pRegionOfInfluence = TriggerRegion:new()
o.m_bRemoveFromGame = false
o.m_bActive = false
--GraphNodeIndex is useful to hook into pathfinding, so AI can discover triggers
o.m_iGraphNodeIndex = -1
setmetatable(o, Trigger_mt)
return o
return Trigger
[/lua]
[lua]
function BaseGameEntity:new(id)
local o = o or {}
self.m_dBoundingBoxRadius = 0
self.m_vPositon = {3,2}
self.m_vScale = {1,1} --Vector2D
self.m_iType = default_entity_type
self.m_bTag = false
self:SetID(id)
setmetatable(o, self)
self.__index = self
return self
end
[/lua]
The problem that I’m facing is that ultimately not all of the various class methods are being returned to Trigger_SoundNotify. I’ve noticed all of those attached to BaseGameEntity are there, however when I try to call a method from Trigger (e.g :AddCircularTriggeRegion) I’m getting an “attempt to call method ‘’ (a nil value) error”
I suspect this is because I’m attempting to set the metatable (__index) repeatedly as I progress down the hierarchy, and I assume you can only do this once; i.e it set when it gets down to the BaseGameEntity class, and subsequent attempts silently fail?
If anybody could confirm this or perhaps shed some further light on the subject then that would be really appreciated.