Save / archive SKSpriteNode in Swift

Hi,


For a while now i've been trawling the internet trying to find a way to save a SpriteNode (and all the children of the node) to 'disk' so when I exit my GameScene and then return, the sprites are exactly how I left them.


I don't need to archive the whole scene, just one node (and the many children it has)


I work in SpriteKit and Swift.


Can anyone please point me at a tutorial or some documentation that explains how to do this??


Many thanks for your help!!!


regards

Adam.

Replies

Hiya!


For you to keep in memory the SKSpriteNode within your GameScene, you would have to keep the whole scene active. So it would really not be wise. A much smarter way to do this would be to have the setup logic of your GameScene that always builds up the same content. Then if you need to save positioning, movement, children, etc. you would store those in either global variables or proper local persistent storage (the difference here would be whether or not you want to save the same state beyond launches).


Perhaps it would help the issue if you would explain the context of what you wish to accomplish. I'm sure it's a pretty simple solution.

As a little experiment, print out your SKSpriteNode, like write print( myNode) and look at what comes out in the Output window. It might demystify what's actually defining your SKSpriteNode, and help you understand how to rebuild that same exact thing at runtime.

hi


ok so some more info.


my gamescene sets up all the things that are static at each run. thats fine


however - in my game, spritenodes are generated as part of the level up. Each time you level up, some new nodes are created. The position, angle and some other factors are randomly generated at this time.

This builds up a kind of 'tree' of nodes. Built on top of each other.


So I need to save this whole node 'tree' exactly as it is. I can't re-create each of the (possibly hundreds) of nodes as i need to keep the exact positions of each item.


What I want to do is take the single base parent node and save it and all the children, then re load this at each run.


surely that is possible ?

If its random though, you obviously don't care initially how the level is generated. Like you aren't checking that RockA is in front of RockB at a certain distance. Why do you care that the user replays the same random level on subsequent runs? Just seems to me that defeats the purpose of it being random to begin with.


It seems like you should really be building the exact level you want in the Scene Editor.

sorry, it is hard to explain without discussing the nature of my game (and i don't want to do that in an open forum)


the elements are random initially - but subsequently they need to remain exactly where they are put.



in another forum, someone has confirmed my idea. SKNode can be serialized with NSCoding. written to disk and then read back. Thats what I need to do.

I think i have found a tutorial in the recently updated 2D Games book from Ray Wenderlich.


I'll give that a go this weekend. I think that will be the answer.


thanks for your thoughts.

Yeah you might even be able to save the object in the Standard User Defaults too. Probably just a few lines of code. I was just trying to understand the why of it, but sounds like that’s the secret sauce of the game :-)