assigned variable get nil

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.

Replies

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?

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.

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.

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 .

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.

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: ).

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.