#Understanding parapoly

4 messages · Page 1 of 1 (latest)

vestal oriole
#

Trying to build a rough and ready state machine using parapoly, but I've gone wrong. I get "'sm' of type '^StateMachine' has no field 'previousState'", presumably because I need to attach the specific state type maybe (that's what it implies when I pass by copy, but I do need pass by reference). I've been following the Overview for this but I've interpreted it wrong, any advice appreciated

package main

import "core:fmt"

MyStates :: enum 
{
    Start,
    Middle,
    End
}

StateMachine :: struct($State: typeid)
{
    currentState:       State,
    previousState:      State,
    transitionProc:     proc(s: State) -> State
}

NextState :: proc(sm: ^StateMachine) 
{
    sm.previousState = sm.currentState
    sm.currentState = sm.transitionProc(sm.currentState)
}

SetState :: proc(sm: ^StateMachine, $State: typeid) 
{
    sm.previousState = sm.currentState
    sm.currentState = State
}

main :: proc() 
{
    sm := StateMachine(MyStates){
        currentState = MyStates.Start,
        previousState = MyStates.End,
        transitionProc = proc(state: MyStates) -> MyStates {
            switch state 
            {
                case MyStates.Start:    return MyStates.Middle
                case MyStates.Middle:   return MyStates.End
                case MyStates.End:      return MyStates.Start
            }

            return MyStates.End
        }
    }

    NextState(&sm)
    fmt.printf("%v", sm)
}
#

Also, I formatted with odin but I dunno why it doesn't syntax highlight

indigo stratus
#

discord doesn't support highlighting for every lang. use cpp, go, c or rust.

#

you just need to also declare the para poly params in the proc param type NextState :: proc(sm: ^StateMachine($State) (it doesn't have to share the same name it just has to be there).
i'm at a miss of the words to explain why rn though, i am sorry.