Reputation: 474
According to the Sprite Kit documentation for entities and components
When you add entities (and their components) to a scene in the Xcode SpriteKit Scene Editor, Xcode automatically archive those entities alongside the SpriteKit scene content.
This implies that you can add entities using the scene editor, but I can't find any way to do that. I can add components using the node components inspector, but not entities. How is this done?
Upvotes: 5
Views: 1591
Reputation: 293
An entity is automatically created and added to the array of entities in the SKScene
when a component is added to a SKNode
in the scene editor:
GKComponent
.GKComponent
subclass.You can see an entity is created by adding the following snippet to the scene's update(_:)
method. The snippet accesses each entity, searches for a component for GKSKNodeComponent
(which was added by the scene editor when you added a component to a SKNode
), and then accesses its node (your item(s) in the scene with components added to them):
for entity in self.entities {
if let node = entity.component(ofType: GKSKNodeComponent.self)?.node {
print("node: \(node)")
}
}
Since iOS 13 there has been a bug where there would be problems loading the rootNode
from the GKScene
so GameplayKit projects and the above technique would not work... UNLESS you add the following to your custom GKComponent
subclass:
override class var supportsSecureCoding: Bool {
return true
}
Upvotes: 3
Reputation: 3051
Your entities are not added in the SpriteKit scene - they are added in the abstract GKScene.
When you are using the SpriteKit editor, you can think of your nodes as your entities; if you add a component to a sprite, say, and then load up the GKScene, you'll have an entity with two components: the component you added to the sprite, and a GKSKNodeComponent that points to the node.
What the Sprite Kit scene editor is allowing you to do is to have a nice mapping from a GKScene (which has no particular knowledge of a SpriteKit scene or its structure) and a SpriteKit scene by automatically creating entities representing an SKScene's nodes, and giving you a way to attach components to the entities that are associated with their sister nodes in the SKScene, so that you don't have to do it programmatically.
You can, of course, add your own abstract entities and attach components to them beyond the ones that the SpriteKit scene editor supplies. But currently, you'll have to do that programmatically outside of the scene editor, because the scene editor has no concept of an entity that is not associated with a node. (However, I suspect you'll find that you can just use an empty SKNode to act as a container for those types of components, and it will be a lot easier to manage with very little overhead. In other words, I don't think you'd get much value from manually managing those entities and components in code compared to being able to work with them alongside everything else in the SpriteKit scene editor.)
Upvotes: 7