#Module Script Type Checking

1 messages · Page 1 of 1 (latest)

narrow cypress
#

Does anyone know how to properly do Module Script type checking when using Object Oriented Programming? I'm trying to figure out if there's a proper way to do it rather than spamming a bunch of : or :: or if you can have type checks without exporting the types?

#

It shows all of the typings for the StateMachine but it doesnt show me the methods I've set using setmetatable

#
export type StateMachine = {
    __index : StateMachine,
    States : {State},
    CurrentState : State,
    LastState : State
}

export type State = {
    Name : string,
    OnEnter : () -> {}?,
    OExit : () -> {}?,
    Condition : () -> boolean
}

local StateMachine = {}

function StateMachine.new(states : {State}, initialState : string)
    local self = setmetatable({}, StateMachine) :: StateMachine
    
    self.States = states
    self.CurrentState = self:GetState(initialState)
    self.LastState = self.CurrentState
    
    return self
end

function StateMachine:GetState(name : string) : State?
    return nil
end

return StateMachine
old field
#
export type StateMachine = {
    __index : StateMachine,
    States : {State},
    CurrentState : State,
    LastState : State;
    GetState: (self:StateMachine, name:string) -> State?
}```
narrow cypress
narrow cypress
#

that makes ense thank you

#

what about the typings within the getstate method

old field
#

if you have it on strict youll get a type error casting from setmetatable directly to your statemachine type

narrow cypress
#

how can I define the statemachine type for self

old field
#

also as I see it you dont have the __index method assigned for it

narrow cypress
#

waht do you mean by that? StateMachine.__index = StateMachine?

narrow cypress
#

oh okay

#

so i wouldnt have to define __index inside StateMachine type

narrow cypress
#

ohh okay

#

and how can i differentiate the functions inside types

#

I have get state defined

#

but I want it to be used as :GetState

#

would i have to swithc it to a function and pass in the state machine table manualy?

old field
#

no

narrow cypress
#
function StateMachine.GetState(self : StateMachine, name : string)
    
    return nil
end
old field
#

you can define it with the rest of the stuff in your table

#

self is just first argument thats automatically passed when you call a function with :

narrow cypress
#

how could I define it as a method instead of a function?

old field
#

if you wanna look at one that ive made before for inspiration here you go

#

@narrow cypress

#

anyway if you wanna define it as a method just define it with :

#

and then define self in your type

narrow cypress
#

ohh okay i get it now

#

let me check

#
export type StateMachine = {
    __index : StateMachine,
    
    -- Variables
    States : {State},
    CurrentState : State?,
    LastState : State?,
    
    -- Methods
    GetState : (self : StateMachine, name : string) -> State?,
    ChangeState : (self : StateMachine, name : string) -> boolean
}
#

liek so?

#

like*

narrow cypress
#

ohh okay thanks

narrow cypress
#

I am getting a new error saying the value is unknown

--!strict

export type StateMachine = {
    __index : StateMachine,
    
    -- Variables
    States : {State},
    CurrentState : State?,
    LastState : State?,
    
    -- Methods
    GetState : (self : StateMachine, name : string) -> State?,
    ChangeState : (self : StateMachine, name : string) -> boolean
}

export type State = {
    Name : string,
    OnEnter : () -> {}?,
    OnExit : () -> {}?,
    Condition : () -> boolean
}

local StateMachine = {}
StateMachine.__index = StateMachine

function StateMachine.new(states : {State}, initialState : string)
    local self = setmetatable({}, StateMachine) :: StateMachine
    
    self.States = states
    self.CurrentState = self:GetState(initialState)
    self.LastState = self.CurrentState
    
    return self
end

function StateMachine:GetState(name : string)
    for _, state in self.States do
        if state.Name == name then
            return state
        end
    end
end

return StateMachine

This is my current script

old field
#

that way you can directly index the map