Observing RealityKit Components?

So I can observe RealityKit Components by using the new @Observable or using ObservableObject, both of these require my Component to be a class instead of a struct though.

I've read that making a Component a class is a bad idea, is this correct?

Is there any other way to observe values of an entities' components?

It is best practices to use structs instead of classes for your components. This is one of the use cases where making it a class makes sense. If there is only a few items you want to observe within the component, you could also create and observable class inside the component struct that only holds the values you want to monitor. In either case, you will need to make sure that the view that you are wanting to update based on this component changes calls the observed component value. The component could look something like this:

struct CustomComponent: Component {
    @Observable
    class Storage {
        var A: Float = 0
    }
    private var storage = Storage()
    var A: Float {
        get { storage.A }
        set { storage.A = newValue }
    }
    var B: String = "Foo"
}

This is helpful. Thank you.

Given that Components are tucked away within entities that are loaded into a RealityView. How would you access the observable property Storage in the parent SwiftUI view?

struct ContentView: View {

  @State var A: Float // ???

  var body: some View {
    RealityView { content
      if let scene = try? await Entity(named: "Scene", in: realityKitContentBundle) {
        content.add(scene)
      }
    }
  }
}
Observing RealityKit Components?
 
 
Q