TLTR:
Issue: SKScene
subclass's init(fileNamed:)
returns nil
Reason: The .sks
file does not exist at the time of second initialisation call. The issue somehow related to ODR implementation AND/OR behaviour.
DESCRIPTION:
A game built with SpriteKit
. My SKScene
subclass's init(fileNamed:)
returns nil
. I am trying to understand "why?" and "how?" to debug this issue.
My code for initialising SKScene
subclass:
/// the hierarchy: SKScene -> TBTBaseScene -> TBTLevelScene
let sceneType: TBTBaseScene.Type = TBTLevelScene.self
let fileName = "Level1"
let scene = sceneType.init(fileNamed: fileName)
Few things to note:
- This code works at some point during runtime, but does not at another.
- When the target is built via Xcode, going to Products -> AppName.app, right click, "Show in Finder", there are NO
Level1.sks
. Perhaps it is OK, because the app uses ODR andLevel1.sks
is marked with "Level1" tag, even though it is in Initial Install Tags - When a breakpoint is set to
let sceneType:...
line, I could easily create other game scenes via respective subclasses and fileNames, butexpression TBTLevelScene(fileName:"Level1")
returnsnil
. - I have tried to check is there
Level1.sks
in the main bundle, but the code below ALWAYS says "FILE NOT AVAILABLE", for all scenes, so the approach seems to be wrong. - The game's architecture for scene loading is driven from DemoBots Sample Code. You can see similar approach in
LoadSceneOperation.swift
. The initialisation there, though, works fine in all cases.
The code I use to check the "Level1" or "Level1.sks" exist, based on this answer:
let path = NSBundle.mainBundle().bundlePath
let url = NSURL(fileURLWithPath: path)
let filePath = url.URLByAppendingPathComponent(fileName).absoluteString
let fileManager = NSFileManager.defaultManager()
if fileManager.fileExistsAtPath(filePath) {
print("FILE AVAILABLE")
} else {
print("FILE NOT AVAILABLE")
}
UPDATE:
Approach for checking files provided by Michael Dautermann does work and it proofs the reason: The Level1.sks
does not exist during second attempt to load it.
The Level1.sks
file in the project is added to "Copy Bundle Resources" area and has "Level1" ODR tag. The tag is in Initial Install Tags prefetched group.
There is definitely some bug in ORD implementation which disposes the resource (Level1.sks
) when it is not needed and does not load it back again at the time it is required. I will keep moving to figure it out. (Seems like, SO community has nothing to do with that so far)