When I add a CustomComponent (GKComponent)
to an entity in Xcode SpriteKit scene editor and try to load that .sks
file using a GKScene.init
, GKScene.rootNode
is not set. Even stranger, this happens only on iOS 13 and not on iOS 12.
I have a small sprite kit github project setup that demonstrates this issue clearly. Just run the app on an iOS 13 emulator to reproduce the issue. https://github.com/hdsenevi/answers-gkscene-rootnode-nil-bug
If I remove CustomComponent
from SpriteKit scene editor entity/sprite, then it runs fine. ie: loads SKScene
into GKScene.rootNode
- Is there any other special modifications that needs to happen when adding GKComponents from Xcode SpriteKit scene editor?
- Am I missing something obvious here?
- And why would this code work without an issue on iOS 12 and not iOS 13?
- Has SpriteKit functionality changed with regards to this in iOS 13?
For reference
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
} else {
print("Error. No GameScene was found on GKScene.rootNode")
}
} else {
print("Error loading GKScene file GameScene")
}
}
}
import SpriteKit
import GameplayKit
class CustomComponent: GKComponent {
override init() {
super.init()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func didAddToEntity() {
guard let gkSkNodeComponent = self.entity?.component(ofType: GKSKNodeComponent.self) else {
print("Error. Cannot obtain a reference to GKSKNodeComponent")
return
}
if let sprite = gkSkNodeComponent.node as? SKSpriteNode {
sprite.texture?.filteringMode = .nearest
}
}
}
Update
Few details on my setup
- macOS Mojave 10.14.6
- Xcode 11.0 (tried on 10.1 and 10.3, same behaviour)
build
folder 2) deletedderived data
folder 3) reseted both iOS 12 and 13 simulators 4) tried different device simulators (6, 8, X, 11). You can try it for your self by just checking out the repo that I've mentioned in the question and running that sample project on an iOS 12 and iOS 13 simulator. Even the iOS 13 one works if you remove theCustomComponent
attached to thered sprite
node inGameScene.sks
file. But obviously I would like to attach components from the SpriteKit scene editor. – Colossians