In my game I’m trying to create a series of systems based around a pseudo-class-based structure – metatables with .__index and whatnot to create a system that acts like it’s class-based. I don’t think it’s too uncommon?
Regardless, at the moment I have the following structure. Everything but ServerEntities is a modulescript:
ServerEntities (regular script that tracks entities and passes remote/bindable events to them)
BaseEntity (handles health and general tracking)
StorageEntity (can store items and move them between itself and other inventories)
ProductionEntity (can make items out of ingredients inside itself)
The basis of the pseudo-class system is generally fine and working. What I’m having trouble with is inheritance. I cannot seem to get ProductionEntity to properly inherit from StorageEntity.
This is the stuff in ProductionEntity that deals with inheritance:
local BaseProducer = {}
BaseProducer.__index = BaseProducer
local StorageEntity = require(script.Parent)
function BaseProducer:New(model, ID, ...)
local BlankTable = StorageEntity:New(model, ID, ...)
local NewProducer = setmetatable(BlankTable, BaseProducer)
NewProducer.__index = function(t, key)
return NewProducer[key]
end
StorageEntity & BaseEntity are set up in the exact same way. Eventually the function returns NewProducer after doing some Producer-specific things that work fine. But the function cannot call any methods from StorageEntity. It retains values set to the table in the :New() function of StorageEntity, but attempts to call methods, like this one in StorageEntity:
function BaseStore:AddListener(player) table.insert(self.Listeners, player) end
…just end up with that function not existing. I call the functions of entities with this code in ServerEntities (so I don’t have to hardcode in each Remote/Bindable Event):
SpecificEntity[entityFunction](SpecificEntity, ...)
SpecificEntity is the entity object, which is certain. entityFunction is the function I want to call, like AddListener, SpecificEntity is provided for the self variable to work, and … is self explanatory.
I don’t suppose anyone has any ideas as to why I can’t get this working? I’ve tried tinkering with the code a lot and asking around my coder friends/acquaintances, but I just can’t seem to get it to function right.