How do I register the system in RealityKit ECS with a custom component

344 Views Asked by At

I'm trying to create custom components in Reality Composer Pro and make it do something in the update loop.

I currently added a component in Reality Composer Pro like this.

enter image description here

I created a new system for this component in the Component script.

import RealityKit

// Ensure you register this component in your app’s delegate using:
// RotateAroundComponent.registerComponent()
public struct RotateAroundComponent: Component, Codable {
    // Name used to uniquely identify the component type. It can differ from the struct name.
    public static var componentName: String { "RealityKitContent.RotateAroundComponent" }

    // This is an example of adding a variable to the component.
    var count: Int = 0

    public init() {
            
    }
}

class MySystem : System {


    // Define a query to return all entities with a MyComponent.
    private static let query = EntityQuery(where: .has(RotateAroundComponent.self))


    // Initializer is required. Use an empty implementation if there's no setup needed.
    required init(scene: Scene) {
        
    }


    // Iterate through all entities containing a MyComponent.
    func update(context: SceneUpdateContext) {
        context.scene.performQuery(Self.query).forEach { entity in
            // Make per-frame changes to each entity here.
            print(entity)
        }
    }
}

Now I have to register both the RotateAroundComponent and MySystem using registerComponent() and registerSystem() calls according to the documentation.

However, I don't understand where I have to call these functions since these classes are never in scope in the App swift files.

Tried to reference them in the App file

enter image description here

Tried to create an AppDelegate to see if that fixes it

enter image description here

Any suggestions would be appreciated. I'm kind of new to Swift programming.

1

There are 1 best solutions below

0
Quicksillver On

I found that we can reference the classes as long as they are public by using,

RealityKitContent prefix.

enter image description here