GKEntity and GKComponent

suggest change

An entity represents an object of a game, such as a player figure or an enemy figure. Since this object does not do much without arms and legs, add components to it. Apple provides the GKEntity and GKComponent classes for this.

Let's assume the following classes for the chapters below. PlayerControlComponent and delta are used later and are not declared on this page.

class Player: GKEntity {}
class PlayerSpriteComponent: GKComponent {}

GKEntity

An entity is a collection of components and offers several functions to add, remove and interact with components of it.

While we could just use GKEntity, it is common to subclass it for a specific type of game entity.

An entity can hold only one component of a given class. Adding a second component of the same class replaces the existing component inside the GKEntity.

let otherComponent = PlayerSpriteComponent()
var player = Player()
player.addComponent(PlayerSpriteComponent())
player.addComponent(otherComponent)
print(player.components.count) //will print 1
print(player.components[0] === otherComponent) // will print true

The lookup method is component(ofType:), which returns the component of a specific type.

let component = player.component(ofType: PlayerSpriteComponent.self)

In addition to the components-methods it has an update method which is used to delegate the delta time or current time of the game logic to it’s components.

var player = Player()
player.addComponent(PlayerSpriteComponent())
player.update(deltaTime: 1.0) // will call the update method of the PlayerSpriteComponent added to it

GKComponent

A component represents something of an entity for example the visual component or the logic component.

If the update method of an entity is called, it forwards that call to all of its components. Override this method to update an entity.

class PlayerSpriteComponent: GKComponent {
    override func update(deltaTime seconds: TimeInterval) {
        //move the sprite depending on the update time
    }
}

In addition to this it is possible to override the method didAddToEntity and willRemoveFromEntity to inform other components about it’s removal or add.

To manipulate another component from inside a component, get the GKEntity the component was added to.

override func update(deltaTime seconds: TimeInterval) {
    let controller = self.entity?.component(ofType: PlayerControlComponent.self)
    //call methods on the controller
}

While this is possible it is not a common pattern since it wires the two components together.

GKComponentSystem

While we just talked about using the update delegate mechanism of the GKEntity to update the GKComponents there is a different way to update GKComponents which is called GKComponentSystem.

It is used in case it is needed that all components of a specific type need to be updated in one go.

A GKComponentSystem is created for a specific type of component.

let system = GKComponentSystem(componentClass: PlayerSpriteComponent.self)

To add a component you can use the add method:

system.addComponent(PlayerSpriteComponent())

But a more common way is to pass the created entity with it’s components to the GKComponentSystem and it will find a matching component inside of the entity.

system.addComponent(foundIn: player)

To update all components of a specific type call the update:

system.update(deltaTime: delta)

In case you want to use the GKComponentSystem instead of a entity based update mechanism you have to have a GKComponentSystem for every component and call the update on all of the systems.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents