Learn iOS SpriteKit with Examples, Code Snippets and Video Tutorials

Parsing a Property List using Swift 3 and Sprite Kit

How to Parse a Property List using Swift 3

Although sorting through a Property List (.plist) file doesn’t have anything specific to do with Sprite Kit (we’ll just be using Swift code to parse it),  this is an important article for game developers.

Why use Property Lists at all?

Property Lists are a perfect place to put data for your game outside of the code. The plist below has a Settings dictionary with just three properties a Number, Boolean and String  (a dictionary is essentially just a named grouping of properties).

Parsing property lists with Swift 3 and Sprite Kit

Now obviously the values of each of those properties could be “hard coded” into your GameScene.swift file or wherever. But take a look at the Property List file from a role playing game….

Complicated Property List

The plist file has a Dictionary for each Level, and within that you’ll see many other dictionaries. One for level specific properties, one for NPC’s (non player characters), one for Items in the level, and so on. In the Items dictionary, the Chest dictionary is unfolded to show some of the properties within there. The name “Chest” correlates to an identically named sprite in the SKS file.

Chest in SKS Scene File

So as clever game developers, what we strive for is making classes that are flexible enough to be many different things.  For example, a Chest, Key, Diamond, & Castle might all seem like very different things, but in the SKS file they are each Items, and share a common subclass of SKSpriteNode. What the Property List does is provide specific data for each of them. Otherwise we would be writing classes for very specific things and repeating code all over the place!


Creating a Property List

First we need a plist file to actually poke through. Go to File > New…> File and find Property List. Give it a name of GameData.plist. Every plist has a Root dictionary. Hit the + sign to add a new entry. Call it Settings and set it’s type as Dictionary. Within that, do the same thing again with these entries (it should ultimately look like the first screenshot above)…

  • MaxLevels (Key name) – 20 (value type of Number)
  • WalkDiagonal (Key name)  – YES (value type of Bool)
  • PlayerStartingClass (Key name) – Peasant (value type of String)

Parsing a Property List

Head over to your GameScene.swift file and add this…

The code above finds the GameData.plist, makes a dictionary out of the entire root, then checks to see if within that is a key named “Settings”. We then check to see if the value for that key is  [String : Any]. Code-wise [String : Any] is a Dictionary.

If you Run the project now, you should see this…

[“PlayerStartingClass”: Peasant, “WalkDiagonal”: 1, “MaxLevels”: 20]

Now lets go through and find those specific properties by checking if the key equals either PlayerStartingClass, WalkDiagonal, or MaxLevels. In each case, we’ll check to see if the value is a specific type of class, either String, Bool or Int.

 

 

Check out our premium Swift and Sprite Kit Tutorial content!

Visit CartoonSmart.com