-
Re: appearing assigned variable nil
Claude31 Jul 13, 2018 4:31 AM (in response to Squirrel Shi)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 Jul 13, 2018 6:40 AM (in response to Claude31)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 Jul 13, 2018 6:54 AM (in response to Squirrel Shi)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 Jul 13, 2018 5:53 PM (in response to Claude31)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.
-
Re: appearing assigned variable nil
Claude31 Jul 13, 2018 11:31 PM (in response to Squirrel Shi)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.
-
-
-
-