0
votes

I'm developing a SpriteKit Game using the GKStateMachine for transitioning between multiple scenes. I'm using a class that holds an instance of the StateMachine, which has multiple states. The states themselves have access to the current SKView through an initializer property. The States are responsible for presenting scenes, so they have a scene property, which will be presented on the didEnter-Method. Now I have a MainMenuState, which has a scene with 3 buttons. For passing the button events to the State, I wrote a custom delegate. The MainMenuState implements the delegate protocol and sets the scene's delegate property to 'self'. So when a user hits a button the action is forwarded to a delegate method. I wanted to use the delegate method to transition to the next state (e.g. SettingsState). However, when I try to access the GKStates StateMachine property within the delegate func it's always nil. I think that I have a design problem here and I don't know how to solve it.

The Code of the MainMenuState

import Foundation
import GameplayKit

class MainMenuState : GKState, MenuSceneDelegate {

    var view: SKView
    var scene: GKScene?

    init(view: SKView) {
        self.view = view;
        self.scene = GKScene(fileNamed: "MenuScene")
        super.init()
    }

    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is MultiplayerHostState.Type ||
        stateClass is MultiplayerSearchState.Type ||
        stateClass is SettingsState.Type
    }

    override func didEnter(from previousState: GKState?) {
        super.didEnter(from: previousState)
        // Load 'GameScene.sks' as a GKScene. This provides gameplay related content
        // including entities and graphs.
        if let scene = self.scene {

            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as! MenuScene? {

                // Set delegate
                sceneNode.menuDelegate = self

                // Copy gameplay related content over to the scene
                sceneNode.entities = scene.entities
                sceneNode.graphs = scene.graphs

                // Set the scale mode to scale to fit the window
                sceneNode.scaleMode = .aspectFill
                sceneNode.size = view.bounds.size

                // Present the scene
                if let view = self.view as SKView? {
                    view.presentScene(sceneNode)

                    view.ignoresSiblingOrder = true

                    view.showsFPS = true
                    view.showsNodeCount = true
                }
            }
        }
    }



    override func willExit(to nextState: GKState) {
        super.willExit(to: nextState)
    }

    func hostGameClicked() {
        if let stateMachine = self.stateMachine {
            stateMachine.enter(MultiplayerHostState.self)
        }
    }

    func joinGameClicked() {
        if let stateMachine = self.stateMachine {
            stateMachine.enter(MultiplayerSearchState.self)
        }
    }

    func settingsClicked() {
// <-- Here the StateMachine is nil -->
        if let stateMachine = self.stateMachine {
            stateMachine.enter(SettingsState.self)
        }
    }
}
1

1 Answers

0
votes

After some research I found out this behavior was caused by Swifts ARC System. The class holding the reference of the state machine was only declared within a func of the overall ViewController. So after existing the func, the class and state machine were deallocated. I solved it within the View Controller:

class ViewController {
    var classWithStateMachine: ClassWithStateMachine?

    func initializeClassWithStateMachine {
        self.classWithStateMachine = ClassWithStateMachine()
    }
}

This code snippet is just a demonstration of the concept, no real code.