5 Replies
      Latest reply on Jul 13, 2018 11:31 PM by Claude31
      Squirrel Shi Level 1 Level 1 (0 points)

        Player: GKGameModelPlayer {  }

        GameScene: SKNode {

                 var board = Board ( )

                 var strategy: GKMontaCarloStrategist?

        override didMove(to view: SKView) {

                 self.strategist = GKMonteCarloStrategist  ( )   // object strategist call func1 , func2 and func3 automatically

                let playerA = Player ( )

               let playerB = Player ( )

               self.board = [ playerA , playerB ] as [ GKGameModelPlayer]  } 

        oveeride touchBegan(touches: Set<UITouch>, event: UIEvent?) {

               self.board.activePlayer = board . opponent( board.activePlayer!) }

        override update ( delta: TimeInterval) {

               self.board.activePlayer = board . opponent(board.activePlayer!)   }   }

        Board: GKGameModel {

               var activePlayer: GKGameModelPlayer?

              var players: [ GKGameModelPlayer] ?

        func1 setGameModel( ) {.    }

        func2 gameModelUpdates ( ) {

                  guard let players  = self.players else{ fatalError( " players nil") }  / / appear" fatal error: players nil"

                   self.getMoves( )    }

        func3  apply( GKGameModelUpdate) {  }

        func getMoves ( )

        func opponent( GKGameModelPlayer )  }

        i don`t know why appear "fatal error : players nil"

        this is the sample of my entity code, when implement appear" fatal error : players nil", i don`t know why the assigned variable players of Board is nil. thanks for your appriciation.

        • Re: appearing assigned variable nil
          Claude31 Level 7 Level 7 (4,735 points)

          In the code you have posted, players is never initialized. So it is nil.

          var players: [ GKGameModelPlayer] ?

           

          Where do you set the players ?

           

          Please, once again, show the relevant code, or forget about being helped.

           

          In addition, avoid duplicating posts.

            • Re: appearing assigned variable nil
              Squirrel Shi Level 1 Level 1 (0 points)

              the property "players" of Board is initialized under func didMove( to view: SKView) in the GameScene class. but appear "fatal error: players nil" in the func gameModelUpdates(GKGameModelPlayer) when using guard testing. the initializer show below:

               

                           GameScene: SKScene {

                                       var board = Board ( )

                                     var strategy = GKMonteCarloStrategist?

                            override func didMove( to view: SKView) {

                                     self.strategy = GKMonteCarloStrategist( )    / / object strategy call func2 automatically.

                                     self.board.players = [ playerA, playerB ] as [ GKGameModelPlayer ]

                                     }

                           Board: GKGameModel {

                                    var players: [ GKGameModelPlayer] ?

                               func2 gameModelUpdates( GKGameModelPlayer) {

                                     guard let players = self.players else { fatalError( " players nil " ) }   / / appear " fatal error: players nil "

                                     }

              i don`t know why property "players" of Board is nil , i have initialized variable players under func didMove( to view: SKView) in the GameModel class.

                • Re: appearing assigned variable nil
                  Claude31 Level 7 Level 7 (4,735 points)

                  I can see

                                         self.board.players = [ playerA, playerB ] as [ GKGameModelPlayer ]

                   

                  but later, you call

                                         guard let players = self.players else { fatalError( " players nil " ) }   / / appear " fatal error: players nil "

                   

                  so players is not initialized, only board.players, which is not the same.

                    • Re: appearing assigned variable nil
                      Squirrel Shi Level 1 Level 1 (0 points)

                      Board : GKGameModel{

                      func2 gameModelUpdates( GKGameModelPlayer) {

                                weak var scene: GameScene?

                                  guard let players = scene?, board.players else { fatalError(" players nil " )  / / appear exception_Bad_instruction

                                }.        /. / your idea is likely that

                      }

                      func2 is called by object strategy automatically in the GameScene. board. players is initialized in the GameScene.

                      GameScene :  SKNode {
                                var board = Board ( )

                                var strategy = GKMonteCarloStrategist  ( )          didMove ( ) {

                                  self.board .players = [ playerA, playerB ] as [ GKGameModelPlayer ]

                                  self.strategy.gameboard = self.board.  / / strategy calls func2 automatically. e.g  strategy. gameboard.func2

                              }

                      }

                      they have the same self.board, why not the difference property players of Board.