Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data to SCNNode and archiving

I am weighing my options to add custom data to SCNNode instances.

One way I have been thinking of is using associated objects.

The other is to use an SCNNode subclass.

Concerning associated objects, I am wondering about the possibility to archive the scene with NSKeyedArchiver.archiveRootObject(_:toFile:) and retain data.

Concerning an SCNNode subclass, that would mean my scene graph would be made of that subclass instances as opposed to SCNNode instances. I was wondering if that could cause trouble.

I have made a request to Apple in the bug reports to add a userData property to SCNNode similar to that of SKNode in SpriteKit, but in the meantime, I need to find a way with what we got.

like image 247
Karl Sigiscar Avatar asked Sep 02 '25 06:09

Karl Sigiscar


2 Answers

Just like CALayer, SCNNode is a key-value coding compliant container class and allows you to use KVC for arbitrary keys. SCNNode conforms to the NSSecureCoding protocol and will automatically archive these additional keys.

like image 171
mnuages Avatar answered Sep 04 '25 22:09

mnuages


A downside to subclassing is that you won't be able to use the Xcode Scene Editor with your scene graph. That might not matter to you.

If you're not using it for anything else, the name property might help you. You could store a unique key there, and use it to index the custom data.

like image 23
Hal Mueller Avatar answered Sep 04 '25 21:09

Hal Mueller