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.

Hi @squilliop! SKNode conforms to the NSCoding protocol meaning that it can be serialised/deserialised using NSKeyedArchiver/NSKeyedUnarchiver.

We have a few tutorials around this that might help you? You can find them here: https://www.raywenderlich.com/tag/nscoding

Let me know how you get on! :slight_smile:

thank you !

I actually just bought the 2D games book and I plan to dive into it this weekend as it has a chapter about just this I think.

I’m sure this must be a fundamental thing for many games, so it seemed odd that it is hard to find a solution… I’m sure i must just be missing something :slight_smile:

I’ll report back how i get on.
appreciate the post
thanks.

Adam

No worries Adam, let me know how you get on with the book and don’t hesitate to give us a shout if you have any more questions :slight_smile:

Hi,
I worked through the saving games tutorial yesterday. (In the newly updated 2D Apple games book)
(very good tutorial, really appreciate it! - I actually bought the older book some years ago - iOS Games by Tutorials and that also had a similar chapter in it, but the changes in Swift since then meant it was too confusing for me to understand and update for new Swift at the same time!)

I’m wondering now if i should just archive my whole GameScene as is done in the tutorial, rather than just the single node i was thinking of doing at first. I think it would make more sense.

Are there any speed / memory / efficiency losses by saving the whole scene rather than just one node (and its possibly thousands of node children) ?

thanks again.
Adam.

Glad you liked the tutorial :slight_smile: I personally would take the most simple approach to begin with by just archiving the whole scene. Try it out and see what the performance is like on real devices etc, check that its fast (enough) and doesn’t take up too much disk space and if it’s ok then ship it :ship:

If you run into issues then maybe look at only archiving the critical parts but it could get complicated so i’d only suggest to do this if you absolutely had to.

Hope it helps :+1:

This topic was automatically closed after 166 days. New replies are no longer allowed.