https://github.com/thatfactory/cgkstatemachine
A custom GKStateMachine subclass that allows GKState changes to be observed via Combine 🔀
https://github.com/thatfactory/cgkstatemachine
combine gameplaykit state-machine swift
Last synced: 6 months ago
JSON representation
A custom GKStateMachine subclass that allows GKState changes to be observed via Combine 🔀
- Host: GitHub
- URL: https://github.com/thatfactory/cgkstatemachine
- Owner: thatfactory
- Created: 2021-07-17T21:20:29.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-12-09T12:52:39.000Z (6 months ago)
- Last Synced: 2025-12-10T13:04:07.811Z (6 months ago)
- Topics: combine, gameplaykit, state-machine, swift
- Language: Swift
- Homepage:
- Size: 21.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://developer.apple.com/swift/)
[](https://developer.apple.com/xcode/)
[](https://developer.apple.com/documentation/xcode/swift-packages)
[](https://en.wikipedia.org/wiki/List_of_Apple_operating_systems)
[](https://en.wikipedia.org/wiki/MIT_License)
[](https://github.com/thatfactory/gcoverseer/actions/workflows/release.yml)
# CGKStateMachine 🔀
A **custom** [`GKStateMachine`](https://developer.apple.com/documentation/gameplaykit/gkstatemachine) subclass that allows [GKState](https://developer.apple.com/documentation/gameplaykit/gkstate) changes to be observed using [Combine](https://developer.apple.com/documentation/combine).
## Usage Examples
```swift
// Create a state machines of `CGKStateMachine` type.
let cgkStateMachine = CGKStateMachine(
states: [
FirstState(),
SecondState()
]
)
// Create states of `CGKState` type.
class FirstState: CGKState {}
class SecondState: CGKState {}
// Sink `publishedState`.
var cancellables = Set()
cgkStateMachine.publishedState
.sink { state in
// React here 👈🏻👈🏻👈🏻
}
.store(in: &cancellables)
// Enter states and get notified.
cgkStateMachine.enter(FirstState.self)
cgkStateMachine.enter(SecondState.self)
```
## Notice
Apple implemented a `publisher(for:)` on `NSObject`.
This publisher allows for subscribing to KVO changes on classes that inherit from `NSObject`.
`GKStateMachine` inherits from `NSObject`.
So in theory one could try using:
```swift
gkStateMachine.publisher(for: \.currentState).sink { state in
...
}
```
... however it doesn't seem that `GKStateMachine` is KVO-compliant.
In my experience, with the above approach only the first state gets published.
That's why `CGKStateMachine` was created.
## Integration
### Xcode
Use Xcode's [built-in support for SPM](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app).
*or...*
### Package.swift
In your `Package.swift`, add `CGKStateMachine` as a dependency:
```swift
dependencies: [
.package(
url: "https://github.com/thatfactory/cgkstatemachine",
from: "0.1.0"
)
]
```
Associate the dependency with your target:
```swift
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(
name: "CGKStateMachine",
package: "cgkstatemachine"
)
]
)
]
```
Run: `swift build`