7 Replies
      Latest reply on Jul 12, 2018 11:51 PM by Squirrel Shi
      Squirrel Shi Level 1 Level 1 (0 points)

        Player: GKGameModel {  }

        GameScene: SKNode {

                 var board: Board

                 let playA = Player( )

                let playB = Player( )

                board.players = [playA, playB] as [GKGameModelPlayer]

               }

        Board: GKGameModel {                              

             var players: [GKGameModelPlayer]?              

          func gameModelUpdates(for player: GKGameModelPlayer) -> [GKGameModelUpdate]?{

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

                }

          

        i don`t know why the assigned variable is nil, i want conveying player to Board`s function.  thanks for your appriciation.

        • Re: assigned variable get nil
          QuinceyMorris Level 8 Level 8 (5,690 points)

          There's something missing here. You have:

           

                   var board: Board

           

          and you have:

           

                  board.players = [playA, playB] as [GKGameModelPlayer]

           

          but where is the value of "board" set? Also, where is gameModelUpdates(for:) called, and with what parameter?

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

              GameScene: SKNode {

                           var board: Board

                          self.board = Board( )

                          let playA = Player ( )

                          let playB = Player ( )

                          board.players = [ playA, playB ] as [GKGameModelPlayer]

                         }

              Board: GKGameModel {

                        var: players: [GKGameModelPlayer] ?

                        func gameModelUpdates( for player: GKGameModelPlayer ) -> [GKGameModelUpdate]?{

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

                              }

              why can not convey information ?  func  gameModelUpdate( ) called by GKStrategy object in the GameScene automatically.

                • Re: assigned variable get nil
                  QuinceyMorris Level 8 Level 8 (5,690 points)

                  I don't think your questions can be answered if you keep showing partial code. The parts you leave out may be important to finding the problem.

                   

                  Can you make a small sample that compiles and runs in a playground?

                   

                  >> why can not convey information … automatically

                   

                  Because it isn't automatic. You have to put the information in the correct instance (object) of the correct class.

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

                      other code has no relation with assigning board.players , I have assigned the board.players "[playerA, playerB] as [GKGameModelPlayer]", but assigned variable board.players appear nil in the func gameModelUpdates( for player: GKGameModelPlayer ).

                      I must solve this problem, otherwise, i can not continue .

                        • Re: assigned variable get nil
                          QuinceyMorris Level 8 Level 8 (5,690 points)

                          This code won't compile:

                           

                          GameScene: SKNode {
                                       var board: Board
                                      self.board = Board( )
                                      let playA = Player ( )
                                      let playB = Player ( )
                                      board.players = [ playA, playB ] as [GKGameModelPlayer]
                                     }

                           

                          At least, lines 03 and 06 must be inside a function or initializer. So, we don't know what you're really doing.

                           

                          Also, you don't show your call of function gameModelUpdates(for:), so don't know whether you're using the correct instance of Board.

                           

                          You have a problem in some other code that you don't show. We can't help without more information.

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

                              Lines from 03 to 06 is compiled under func didMove( to view: SKView) {  }

                              but property players of board is nil through guard to verify.

                              the func gameModelUpdates(for player: ) together with func setGameModel(gameModel: ) and func apply(gameModelUpdate: )  called by GKMonteCarloStrategist object in GameScene automatically.

                              other code has no relation with assigning property players of variable board,  through compile from 03 to 06, variable players of board is nil by guard testing in func gameModelUpdate( for player: ).

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

                                  Player: GKGameModelPlayer {  }                                                 GameScene: SKScene {

                                  Board: GKGameModel {                                                                         var board = Board( )

                                       var players: [GKGameModelPlayer] ?                                                 var strategy: GKMonteCarloStrategist?

                                      var activePlayer: GKGameModelPlayer?                                      override func didMove( to view: SKView) {

                                  func1 setGameModel(gameModel: GKGameModel) {  }                       self.strtegy = GKMonteCarloStrategist( ) / /call func1, func2 and func3

                                  func2 gameModelUpdates( for player: GKGameModelPlayer) {                let playerA = Player( )                                  automatically

                                         guard let players = self.players else { fatalError("players nil")}             let playerB = Player ()

                                    / / appear "fatal error: players nil "                                                        self.board.players = [ playerA , playerB] as [ GKGameModelPlayer] }

                                        if self.activePlayer.playerID == self.players![0].playerID{            override func touchBegan(touches: Set<UITouch>, event: UIEvent?){

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

                                  func3 apply(gameModelUpdate: GKGameModelUpdate){ }           overide func update( delta: TimeInterval) {

                                  func getMoves ( ) { }                                                                                  self.activePlayer = board.opponent(self.activePlayer!)    }.   }

                                  func opponent( GKGameModelPlayer) { }

                                  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.