enumerateChildNodes

I'm trying to optimize the draw calls in an existing scene with flattenedClone(). From what I can tell enumerateChildNodes would be a good way to go through the scene tree and add the nodes to be flattened to a parent node.

I saw something like this in a tutorial but it is saying that 'withName' is an extra argument.

gameScene.rootNode.enumerateChildNodes(withName: "//*"){ (node, stop) in
                 if (node.name == "Large Tree"){
                     flattenParent.addChildNode(node)
                     node.removeFromParentNode()
                 }
       
 }

any guidance on this usage? Also would a switch statement or multiple || be optimal when searching through several different mesh nodes?

Replies

Hello,

I saw something like this in a tutorial but it is saying that 'withName' is an extra argument.

any guidance on this usage?

enumerateChildNodes(withName:) is a method on SKNode, not SCNNode, so that will not work for SceneKit.

You could implement something similar yourself, performing the necessary comparisons against some target string in the base enumerateChildNodes closure, or you could use childNodes(passingTest:), and then utilize the returned array of nodes.

Thank you for the reply. I tried doing something like this, it seems to find the node but regardless of wether I provide the name of the 3d mesh itself or its reference it throws a 'unexpectedy found nil which implicitly unwrapping an optional value' fatal error. Quite new to swift nomenclature and optionals etc, not really sure why it's getting nil if it does in fact find the node / reference.

     gameScene.rootNode.enumerateChildNodes { (node, stop) in
      if node.name == "SmallTree reference" {
        print(node.name)
        flattenParent.addChildNode(node)
//        node.removeFromParentNode()
         
      }

Hmm, which line in your snippet is the error coming from? I’m thinking possibly your flattenParent node is an implicitly unwrapped optional that was never assigned a non-nil value.