1

I have a GameScene.sks a custom class GameScene.swift which is connected via the Custom Class Inspector (on the right in Xcode). This is working fine.

Now in the GameScene.swift where I want to reference another scene that I want to reuse in many scenes in the future (oversimplified code):

class GameScene: SKScene {
  // ...
  override func didMove(to view: SKView) {
    //...

    guard let gameMenuPath = Bundle.main.path(
      forResource: "GameMenu", ofType: "sks") else { return }

    let url = NSURL(fileURLWithPath: gameMenuPath) as URL
    let gameMenu = SKReferenceNode(url: url)

    addChild(gameMenu)

    //...

where I am loading a reusable GameMenu.sks.

Everything works fine till here. Now the custom class issue for the GameMenu.sks (the scene to be reused). For the GameMenu.sks I also have a GameMenu.swift which is referenced in the custom class inspector as custom class. So exactly as for the GameScene. The problem is that it seems like the GameMenu.swift is not found/loaded. I get no error - so it looks like I misspelled the custom class - but I checkt it many times and also cleared the Xcode cache.

Any ideas how to set custom class for a scene (.sks) that is referenced in another scene?

I would think this is something that is done for reusable scene parts suche as the GameMenu in my case that is visible in every GameScene.

Thank you for any hints.

  • Why are you doing it this way? Just do `SKReferenceNode(fileNamed:”GameMenu”)` – Knight0fDragon Sep 22 '19 at 19:21
  • Also, how do you know the swift class is different? – Knight0fDragon Sep 22 '19 at 19:24
  • Yes, `SKReferenceNode(fileNamed:”GameMenu”)` works, but it does not help with the `custom class` (`GameMenu.swift`) which is still not loaded. I get the feeling that custom classes cannot be attached to scenes that are loaded via `SKReferenceNode`. – Adam Musial-Bright Sep 27 '19 at 18:34
  • What do you mean custom classes cannot attach? I know you are doing something wrong, I just can’t explain to you how you are wrong. Tell me how you verify you do not have the right class – Knight0fDragon Sep 27 '19 at 18:36
  • Hi. As I described in the question imagine two scenes both with a `.sks`AND a `swift` file. The `swift` file is connected via the `attributes inspector` `custom class` (right side). Both scenes are working as they should and I can override `func didMove` to do custom functions. Now with exactly the same two scenes: As soon as I reference one scene via `SKReferenceNode` this references scene `custom class` is not working any more. – Adam Musial-Bright Sep 28 '19 at 21:39
  • How do you know it is not referencing it, I need to know your diagnostic. Tell me exactly what is happening for you to say “Hmm, my custom class is not being used” – Knight0fDragon Sep 28 '19 at 21:41
  • The code is not executed in the references swift file: `override func didMove(to view: SKView) { super.didMove(to: view); print("should be executed but is not") } ` – Adam Musial-Bright Sep 28 '19 at 22:24
  • 1
    That will not get called. That only gets called when an `SKView.presentScene` is called – Knight0fDragon Sep 28 '19 at 22:27
  • You need to treat this like any other node being added to your scene – Knight0fDragon Sep 28 '19 at 22:27
  • OK, thank you. This explains why the scene behaves "differently" after being references within other scenes. I will do the scene behaviour without (outside) the `custom class` for referenced scenes. – Adam Musial-Bright Sep 28 '19 at 22:36
  • You just need to manually call the didview – Knight0fDragon Sep 28 '19 at 22:37
  • Cannot do `didView` on a `SKReferenceNode`. In my case `gameMenu.didView...` if I understand you right. – Adam Musial-Bright Sep 28 '19 at 22:39

2 Answers2

1

Is it necessary to add GameMenu in GameScene via code? You can also simply use the inspector. In your GameScene.sks drop the 'Reference' (SKReferenceNode) object. Select it from the hierarchy and select GameMenu as in the 'reference' dropdown.

enter image description here

iqra
  • 1,061
  • 1
  • 11
  • 18
  • Yes, I can do that and I also tried this. It still does not pull the `custom class` and does not improve the problem. Since I have many scenes I rather reference the scene (it happens automatically through inheritance). – Adam Musial-Bright Sep 22 '19 at 15:06
0

SKReferenceNodes behave as nodes, not as scenes, which may cause confusion since you are creating a scene file.

This means none of the functions that a view calls on a scene will be called.

If you need some of these functions to call, you need to do it manually.

Look at this example to get your didMoveToView to fire:

override func didMove(to view: SKView) {

    let gameMenu = SKReferenceNode(fileNamed:"GameMenu") as? SKScene // (or your custom class)

    addChild(gameMenu)
    gameMenu.didMove(to:view)
 }
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44