The abstract superclass for creating objects that add specific gameplay functionality to an entity.
- iOS 9.0+
- macOS 10.11+
- Mac Catalyst 13.0+Beta
- tvOS 9.0+
GKComponent is the abstract superclass for custom component classes you create when building a game with Entity-Component architecture. In this architecture, an entity is an object relevant to the game, and a component is an object that handles specific aspects of an entity’s behavior in a general way. Because a component’s scope of functionality is limited, you can reuse the same component class for many different kinds of entities.
You create components by subclassing
GKComponent to implement reusable behavior. Then, you build game entities by creating
GKEntity objects and using the
add method to attach instances of your custom component classes.
At runtime, a component-based game needs to dispatch periodic logic—from an update/render loop method such as
update(_:) (SpriteKit) or
renderer(_: (SceneKit), or a
CADisplay (iOS) or
CVDisplay (macOS) timer in a custom rendering engine—to each of its components. GameplayKit provides two mechanisms for dispatching updates:
Per-entity. Call each entity’s
update(deltamethod, which will then forward to the
update(deltamethod of each component. This option can be quickly implemented in games with a small number of entities and components.
Per-component. Use a
GKComponentobject to handle all instances of a specific component class. When you call a component system’s
update(deltamethod, it forwards to the
update(deltamethod of all the component objects it manages. Because a component system needs no knowledge of your game’s entity/component hierarchy, this option works well for games with complex object graphs.