I’m trying to write a simple Unit class that uses a Selectable class for my RTS game.
Whilst trying to code this I’m using Luau types, which for some reason make it so that I can either only use the functions and methods from one class, or neither. And I cannot figure out why.
I have tried to look for solutions to this problem, but none that I can find share the issue of the methods not showing up when I do the : operator.
Selectable class:
--!strict
local Selectable = {}
Selectable.__index = Selectable
export type Selectable = {
selectable: boolean,
new: (self: Selectable, value: boolean) -> Selectable,
setSelectable: (self: Selectable, value: boolean) -> (),
isSelectable: (self: Selectable) -> boolean,
}
function Selectable.new(self: Selectable, value) -- Constructor
local self = setmetatable({}, Selectable) :: Selectable
self:setSelectable(value)
return self :: Selectable
end
function Selectable.setSelectable(self: Selectable, value) -- Setter
self.selectable = value
end
function Selectable.isSelectable(self: Selectable) -- Getter
return self.selectable
end
return Selectable :: Selectable
Unit class:
--!strict
-- CLASSES
local super = require(script.Parent)
-- CODE
local Unit = setmetatable({}, super)
Unit.__index = super
export type Unit = super.Selectable & {
new: (self: Unit) -> Unit,
test: (self: Unit) -> (),
}
function Unit.new(self: Unit) -- Constructor
self -- this includes methods from both classes when using the : operator
local self = setmetatable({}, Unit)
self -- this doesn't include methods from either.
return self :: Unit
end
function Unit.test(self: Unit) -- test function to see if it works
end
return Unit :: Unit
