Hello guys.
I need help with my OOP structure in roblox.
The problem I’m having is that my CharacterEntity Module which inherits of of my Entity Module cannot access MT methods of itself (does not autocomplete) still haven’t debugged to see if they cannot be called at all. So sth like :SetNextAction is not possible but methods out of the MT of Entity are accessible.
f.e in the Entity Module any MT method can be called on self from other MT methods but this is not the case for CharacterEntity. Whenever I try to index any MT methods from the module the only ones that get autocompleted are those of the Entity MT
The problem seems to stem from setting the metatable of MT to {__index = Entity.MT}
but removing only that does not fix it I also need to remove my annotation of self as {} & Entity.Entity. When I remove both only then can I call :SetNextAction
CharacterEntity Module:
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local ServerScriptService = game:GetService("ServerScriptService")
local ChooseEncounterAction = ReplicatedStorage.Remotes.Events.ChooseEncounterAction
local GlobalTypes = require(ReplicatedStorage.SharedServices.Utilities.GlobalTypes)
local FunctionUtils = require(ReplicatedStorage.SharedServices.Utilities.FunctionUtils)
local t = FunctionUtils.t
local GenerateRandoms = FunctionUtils.GenerateRandoms
local Entity = require(script.Parent._Entity)
local PlayerStatsService = require(ServerScriptService.Server.Services.PlayerStatsService)
local InventoryService = require(ServerScriptService.Server.Services.InventoryService)
local CharacterService = require(ServerScriptService.Server.Services.CharacterService)
local ActionService = require(ServerScriptService.Server.Services.ActionService)
local Items = require(ReplicatedStorage.Shared.Tables.Items)
local isPlayer = t.instanceIsA("Player")
type self = {
_nextAction: GlobalTypes.EncounterAction,
_characterData: GlobalTypes.Character?,
_equipment: {InventoryService.PlayerItem}?
} & Entity.Entity
local CharacterEntity = {}
local MT = {}
MT.__index = MT
setmetatable(MT, { __index = Entity })
export type CharacterEntity = Entity.Entity & typeof(setmetatable({} :: self, MT))
CharacterEntity.MT = MT
local CharacterEntityCache = {} :: {CharacterEntity}
local function onClientActionReceived(player: Player, entityId: string, action: any?)
local entity = CharacterEntity.new(player)
if not entity then
return
end
if entity._owner ~= player then
return
end
if action and entity._actionRequestConn then
entity._actionRequestConn:Disconnect()
end
end
function CharacterEntity.new(player: Player): CharacterEntity
local entity = Entity.new()
local self = setmetatable(entity :: self, MT) :: CharacterEntity
self._type = "CharacterEntity"
self._owner = player
self._stats = PlayerStatsService.GetPlayerStats(self._owner :: Player)
self._characterData = nil
self._equipment = {}
table.insert(CharacterEntityCache, self)
return self
end
function CharacterEntity.GetEntityByUniqueId(uniqueId: string): CharacterEntity
local character = Entity.GetEntityByUniqueId(uniqueId)
return character :: CharacterEntity
end
function MT.TakeTurn(self: CharacterEntity): GlobalTypes.EncounterAction
if isPlayer(self._owner) then
warn("Make owner take turn:", self._owner)
end
return self._nextAction
end
function MT.SetNextAction(self: CharacterEntity, action: GlobalTypes.EncounterAction)
self._nextAction = action
end
function MT._AskOwnerForAction(self: CharacterEntity)
self._actionRequestConn = self._trove:Connect(ChooseEncounterAction.OnServerEvent, onClientActionReceived)
ChooseEncounterAction:FireClient(self._owner, self._actions)
end
function MT._Init(self: CharacterEntity)
if isPlayer(self._owner) then
local player = self._owner :: Player
self._characterData = CharacterService.GetCharacterData(player)
self._equipment = InventoryService.GetPlayerEquipment(player)
self._actions = ActionService.GetAllPlayerActions(player)
end
end
return CharacterEntity
Entity Module:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local FunctionUtils = require(ReplicatedStorage.SharedServices.Utilities.FunctionUtils)
local ModuleUtils = require(ReplicatedStorage.SharedServices.Utilities.ModuleUtils)
local GlobalTypes = require(ReplicatedStorage.SharedServices.Utilities.GlobalTypes)
local Trove = ModuleUtils.Trove
local t = FunctionUtils.t
local GenerateRandoms = FunctionUtils.GenerateRandoms
local isPlayer = t.instanceIsA("Player")
type alignment = GlobalTypes.EncounterEntityAlignment
type self = {
_uniqueId: string,
_type: string,
_tookTurn: boolean,
_stats: GlobalTypes.StatTable,
_alignment: alignment,
_index: number,
_owner: Player?,
_actions: {},
_actionRequestConn: RBXScriptConnection,
_trove: ModuleUtils.TroveType
}
local Entity = {}
local MT = {}
MT.__index = MT
export type Entity = typeof(setmetatable({} :: self, MT))
local entityCache = {} :: {Entity}
Entity.MT = MT
function Entity.new(): Entity
local self = setmetatable({} :: self, MT)
self._trove = Trove.new()
self._uniqueId = GenerateRandoms.GenerateTimestampedGUID()
self._type = "Entity"
self._alignment = "Neutral"
self._index = 0
self._owner = nil
self._stats = {
Health = 10,
Armor = 10,
Speed = math.random(1, 10),
Strength = 10,
CritPower = 10,
CritChance = 10,
Dexterity = 10,
Intelligence = 10,
MovementSpeed = 10,
JumpPower = 10
}
self._tookTurn = false
self._actions = {}
table.insert(entityCache, self)
return self
end
function Entity.GetEntityByUniqueId(uniqueId: string): Entity?
for _, entity in entityCache do
if entity._uniqueId == uniqueId then
return entity
end
end
return nil
end
function MT.TakeTurn(self: Entity): boolean
warn("FINISHED TURN FOR SELF")
return true
end
function MT.SetAlignment(self: Entity, alignment: GlobalTypes.EncounterEntityAlignment)
self._alignment = alignment
end
function MT.GetPossibleActions(self: Entity)
return self._actions
end
function MT.GetTimeoutAction(self: Entity)
end
function MT.Destroy(self: Entity)
self._trove:Destroy()
table.remove(entityCache, table.find(entityCache, self))
end
return Entity