#Modules script (what the good way to do this??????)

1 messages · Page 1 of 1 (latest)

silver yoke
#

I want to call the function of the module from the self return by the init function, but idk if that the good way to do it properly (i've try with a metatable but din't work well with the types)

hazy helmBOT
#

studio** You are now Level 4! **studio

silver yoke
#
local PlayerStatus = {}

type InformationContainer = {
    
    _Status: {string},
    
    Add: (self: InformationContainer, status: string) -> (),
    Del: (self: InformationContainer, status: string) -> (),
    DelAll: (self: InformationContainer, status: string) -> (),
    Reset: (self: InformationContainer) -> (),
    CanI: (self: InformationContainer, status: string) -> boolean,
}

type PlayerStatus = {
    Init: () -> InformationContainer,
}

-- Add one occurence of a status
function PlayerStatus:Add(status: string): ()
    table.insert(self._Status, status)

end

-- Del one occurence of a status 
function PlayerStatus:Del(status: string): ()
    for i, value in ipairs(self._Status) do
        if value == status then
            table.remove(self._Status, i)
            break
        end
    end
end

-- Del each occurence of a status 
function PlayerStatus:DelAll(status: string): ()
    local i = 1
    while i <= #self._Status do
        if self._Status[i] == status then
            table.remove(self._Status, i)
        else
            i += 1
        end
    end
end

-- Clear all satus
function PlayerStatus:Reset(): ()
    table.clear(self._Status)
end

-- Check if a status can be called from the actual status
function PlayerStatus:CanI(status: string): boolean
    -- To do
    return false
end

function PlayerStatus.Init(): InformationContainer
    
    local self: InformationContainer = {
        
        _Status = {},
        
        Add = PlayerStatus.Add,
        Del = PlayerStatus.Del,
        DelAll = PlayerStatus.DelAll,
        Reset = PlayerStatus.Reset,
        CanI = PlayerStatus.CanI,
    }
    
    return self
end

return PlayerStatus
silver yoke
#

for now I just do this, because I din't succeed with a setmetatable between the self and the PlayerStatus (there was problem with the type that I could fix with any but that still couldn't fix the call of :Add() after an init, the error was that the Add wasn't referenced, at this time the function Add and other function types definition where in the PlayerStatus type)

  Add = PlayerStatus.Add,
  Del = PlayerStatus.Del,
  DelAll = PlayerStatus.DelAll,
  Reset = PlayerStatus.Reset,
  CanI = PlayerStatus.CanI,