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
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
To solve the problem, your component needs to override variable supportsSecureCoding
and it must return true.
This worked for me:
override class var supportsSecureCoding: Bool {
return true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With