#Module Script Type Checking
1 messages · Page 1 of 1 (latest)
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
youre casting it to your statemachine type and the function isnt included in said type
export type StateMachine = {
__index : StateMachine,
States : {State},
CurrentState : State,
LastState : State;
GetState: (self:StateMachine, name:string) -> State?
}```
ohh okay so I would have to define all of my methods inside said type?
mhm
if you have it on strict youll get a type error casting from setmetatable directly to your statemachine type
how can I define the statemachine type for self
cast the StateMachine table to your type
also as I see it you dont have the __index method assigned for it
waht do you mean by that? StateMachine.__index = StateMachine?
you still do
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?
no
function StateMachine.GetState(self : StateMachine, name : string)
return nil
end
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 :
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
like in this ^
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*
ye
ohh okay thanks
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
try using a dictionary where the key is the state
that way you can directly index the map