Request for a simple example for GKStateMachine?
Asked Answered
K

1

11

I've been researching Apple's state machine for Swift and found several examples, but none of them really dead simple.

Could someone whip up a super simple GKStateMachine, perhaps with two states, in Swift, preferably all in one Swift file? Thanks!

Kandis answered 15/1, 2017 at 1:1 Comment(0)
P
23

Here is an example of a state machine for a traffic light as they work in the USA. A traffic light moves from green -> yellow -> red -> green.

In an app, you might have the didEnter(from:) routines update an onscreen graphic, or allow another actor to move.

import UIKit
import GameKit

class Green: GKState {

    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is Yellow.Type
    }

    override func didEnter(from previousState: GKState?) {
        print("Traffic light is green")
    }
}

class Yellow: GKState {
    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is Red.Type
    }

    override func didEnter(from previousState: GKState?) {
        print("Traffic light is yellow")
    }

}

class Red: GKState {
    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is Green.Type
    }

    override func didEnter(from previousState: GKState?) {
        print("Traffic light is red")
    }
}

class ViewController: UIViewController {

    var stateMachine: GKStateMachine?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create the states            
        let green = Green()
        let yellow = Yellow()
        let red = Red()

        // Initialize the state machine            
        stateMachine = GKStateMachine(states: [green, yellow, red])

        // Try entering various states...            
        if stateMachine?.enter(Green.self) == false {
            print("failed to move to green")
        }
        if stateMachine?.enter(Red.self) == false {
            print("failed to move to red")
        }
        if stateMachine?.enter(Yellow.self) == false {
            print("failed to move to yellow")
        }
        if stateMachine?.enter(Green.self) == false {
            print("failed to move to green")
        }
        if stateMachine?.enter(Red.self) == false {
            print("failed to move to red")
        }

    }

}

Output:

Traffic light is green
failed to move to red
Traffic light is yellow
failed to move to green
Traffic light is red
Pasquinade answered 15/1, 2017 at 2:56 Comment(3)
Thank you for the response. This clarifies GKStateMachine for me :)Kandis
Great! Glad I could help.Pasquinade
I'm working on my first game. youtu.be/5zdQBCM1X5c I am interested in using a state machine to control the player and player animation, so your response helps a lot. I've spent three days reading about GKGameStateMachine and googling for tutorials and examples and most state machine stuff is either not in swift or too complex for my level of programming. Apple's GK state machine guide example on the dispenser was a little over my head. I managed to simplify it down a lot, but I still was left with some stuff I didn't grok. Their chase-flee example isn't in swift. So thanks :)Kandis

© 2022 - 2024 — McMap. All rights reserved.