appearing assigned variable nil

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.

Replies

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.

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.

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.

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.

Sorry, you give so little information and just repeating the same, and it is always as badly formatted

what is this, with 2 instructions on the same line

var strategy = GKMonteCarloStrategist ( ) didMove ( ) {


So, I give up, sorry.