#Help structuring a StateMachine

20 messages · Page 1 of 1 (latest)

teal vigil
#

I currently have

type Game struct {
    UnitStats    []UnitStats
    CurrentState func(game *Game)
    StateTime    float64
    Level        Level
    KeyPressed   int32
    CurrentMenu  Menu
}```
for which i have
```golang
func (game *Game) Transition(state func(game *Game)) {
    game.CurrentState = state
    game.StateTime = 0
}```
it's not ideal as soon as i want another state machine outside of game... so i decided to create
```golang
type StateMachine struct {
    StateTime    float64
    CurrentState func(*StateMachine)
}

func (stateMachine *StateMachine) Transition(state func(*StateMachine)) {
    stateMachine.CurrentState = state
    stateMachine.StateTime = 0
}```
this would be great, wasn't it for the fact that i now cant assign custom properties later in case i need to use it like
```golang
type Game StateMachine

and since golang doesnt have inheritance, ofc, it's not OOP, i was wondering what i could possibly do here to make it work where i can add custom properties to my state machine struct and still have the transition function, the state time variable and the current state
Thanks

stark sparrow
#

unsure how much this will help

teal vigil
#

im not sure where i'm supposed to be looking

#

where is your implementation of a state machine?

stark sparrow
#

I read game so I assumed you are trying to keep track of a game state. I have a game interface that has methods for relaying information about the current state. Then I have an input interface for various user inputs to change a given games state. My b if that’s not what u were looking for. Main loop should be in engine/

#

But in general interfaces might be what ur lookin for

teal vigil
#

there's interfaces in go?

#

damn i thought... idk i thought they didnt exist...

stark sparrow
#

They are just a set of methods. If a struct implements those methods, you can call it an interface

teal vigil
#

yeah that's precisely what i'm looking for...

stark sparrow
teal vigil
#

cute dog ig

#

but.. wait the implementation must be re written for each struct?

#

or sorry, for each type?

stark sparrow
#

Yeah. It’s kinda weird. But u can make it work. But definitely not the type of OOP stuff that ur used to

teal vigil
stark sparrow
#

Someone else may have input about this as well so keep an eye on this chat

teal vigil
#

any way to get around this maybe?

#

already better...