No storage has been registered for component type

Hello

I'm a complete noob in Swift and RealityKit so I apologize if it's too basic.

I'm getting a runtime error when I try to add my entity to the anchor. EXC_BAD_ACCESS and "No storage has been registered for component type 4937111887299015975(i1.CardComponent)" in the log.


Attached a snippet of the code


Thanks a lot !



import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let anchor = AnchorEntity(plane: .horizontal)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        arView.scene.addAnchor(anchor)
        let bottom = CustomBox.init()
        anchor.addChild(bottom)
    }
}

class CustomBox: Entity, HasModel, HasCollision {
  required init() {
    super.init()
    self.components[ModelComponent] = ModelComponent(
        mesh: .generateBox(size: [0.1, 0.01, 0.1]),
        materials: [SimpleMaterial(color: UIColor.gray, isMetallic: false)]
    )
    self.components[CardComponent] = CardComponent()
  }
}

struct CardComponent: Component, Codable {
    var revealed = false
}

Replies

Found the issue. Apparently I have to register my component. See attached fixed snippet (registerComponent was missing)


import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let anchor = AnchorEntity(plane: .horizontal)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CardComponent.registerComponent()
        arView.scene.addAnchor(anchor)
        let bottom = CustomBox.init()
        anchor.addChild(bottom)
    }
}

class CustomBox: Entity, HasModel, HasCollision {
  required init() {
    super.init()
    self.components[ModelComponent] = ModelComponent(
        mesh: .generateBox(size: [0.1, 0.01, 0.1]),
        materials: [SimpleMaterial(color: UIColor.gray, isMetallic: false)]
    )
    self.components[CardComponent] = CardComponent()
  }
}

struct CardComponent: Component, Codable {
    var revealed = false
}

I am registering my component before use and I am still getting an EXC_BAD_ACCESS and the old "


No storage has been registered for component type 3445452219525568944(ARSDKPrototypePlatform.ManagedARObjectComponent)


"


I have a component and protocol

struct ManagedARObjectComponent: Component, Codable, Equatable {
    var isDeployed = false
    var id: Int
   
    public static func == (lhs: ManagedARObjectComponent, rhs: ManagedARObjectComponent) -> Bool {
        return lhs.id == rhs.id
    }
}

protocol HasManagement {
    var managedObject: ManagedARObjectComponent { get set }
}


and when I instantiate the ARView I register this component


func makeUIView(context: Context) -> ARView {
       
        let arView = sessionDelegate.arView
        
        // Configure the AR session for horizontal plane tracking.
        let arConfiguration = ARWorldTrackingConfiguration()
        arConfiguration.planeDetection = .horizontal
        arView.session.run(arConfiguration)
       
        ManagedARObjectComponent.registerComponent()
       
        return arView      
}



I have a class that conforms to the `HasManagement` protocol


class AREntity: Entity, HasModel, HasCollision, HasAnchoring, HasManagement {
    var managedObject: ManagedARObjectComponent {
        get { components[ManagedARObjectComponent] ?? ManagedARObjectComponent(id: -1) }
        set { components[ManagedARObjectComponent] = newValue }
    }
  
    init(id: Int) {
        super.init()
        self.managedObject = ManagedARObjectComponent(id: id)
        // ... more initialization code
     }
}

I tried registering it in the function where I first instantiate the `AREntity`

@discardableResult
func generateBox() -> AREntity {
        ManagedARObjectComponent.self.registerComponent()
        let entity = AREntity(id: UUID().hashValue)
        arView.scene.addAnchor(entity)
      
        return entity
}



I still get the error I still do not see this component when I `po` the components on the object itself.


With such scant documentation, I can't seem to find what I am doing wrong. Any help would be appreciated.

You should file a bug report for this. As a workaround, try registering the component earlier (i.e. in application(_:didFinishLaunchingWithOptions:))