Refactoring Components in SpriteKit

As I’ve been working with Entity Component Systems in SpriteKit, I’ve come to realise that Apple’s implementation of the pattern is subtly different to what I understand is the ‘pure’ form of this pattern.

The idea of ECS (as far as I understand) is that an entity is nothing more than a unique ID. You could represent an entity as a raw integer.

Components are a collection of properties (or state) that give that ID some meaning in the world. You should be able to accurately represent both entities and components in a relational database.

Finally, Systems are what change the properties on the components to create the game. A physics system might change some position properties or an animation system might change a texture property.

The important thing is that the components do not change their properties, the systems do.

SpriteKit’s Implementation

Reading through the docs and sample code, there isn’t this strict separation in SpriteKit.

The framework does have a class called GKComponentSystem which does define the order in which components are updated. This is an important aspect of systems as the order in which systems are applied matters: a movement system will need to be run before a render system to make sure that the sprite’s position properties have been updated before the sprite is redrawn.

However, GKComponentSystem is only designed to work on a single component type. When initialising a GKComponentSystem, you pass it a GKComponent type as an argument and that system then ensures that all of the components of that type are updated in the order you specify.

The docs state that the actual logic of the component systems should then reside in the components themselves:

The component system then forwards to the update(deltaTime:) method of all the GKComponent subclass instances it manages, allowing those objects to perform per-frame update logic.

Their demo apps have also implemented things this way.

Refactoring Components

In order to help my understanding of ECS, I have started using Swift extensions on my components. The main class declarations contain all the properties then an extension is declared that contains the methods if I want to give that component an attached system.

My rule of thumb is that, if I’m overriding the update(deltaTime:) method of a component, then that’s a good sign that I’m actually creating a system and it should go in the extension.

I still use the GKComponentSystem classes but strictly as a way of dictating the order in which my systems are updated.

For example, I’ve created a PositionComponent with a single property:

This component simply gives the entity a position in the world.

Then if I add a SpriteComponent that looks like this:

This component gives an entity a visual presence in the world.

To combine the two and apply the position to the sprite I should have some sort of render system.

In order to make the fact that this is a system explicit, I create an extension that looks like this:

I store this extension in a separate file called RenderSystem to make the distinction between the component and the system even clearer.

Adding Additional Systems

Taking the MoveComponent from my previous post, I break it up into MoveComponent.swift (which just contains the properties):

And MovementSystem.swift:

With this, we begin to see the real power of components. Now, movements are no longer restricted to just sprites as they were before. Any entity that has a PositionComponent can now be made to move by adding a MoveComponent to the same entity.

The movement system updates the position component’s properties and that’s all it does. How that property is then applied to change something on screen is left up to an entirely different system (in this case, the render system).

Finally, putting it all together, in the main scene file I create an array of systems that dictate the order in which things will be updated:

In the update(_:) method, I loop through the systems and forward on the delta time:

This ensures that the move system is called before the render system.

Separating out the systems from the components like this while still using the classes that GameplayKit provides has given me a deeper understanding of how ECS works and the advantages of it. AdventureKit is already reaping the rewards—features that I thought would have to wait for future versions have become trivially easy to implement just by combining different components.

  • Share:

Get all of my latest posts direct to your inbox! Pop in your email address here: