2

I have the following Reality Composer project that loads properly. As you can see, when the animation completes, it should notify with the keyword "attackComplete".

How do I get this notification?

enter image description here

import RealityKit
import ARKit

class ViewController: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let boxAnchor = try! Experience.loadOrcAttack()
        arView.session.delegate = self
        arView.scene.anchors.append(boxAnchor)
        print("done")
    }
    
    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        print(anchors)
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Mason Ballowe
  • 1,847
  • 2
  • 12
  • 23

1 Answers1

4

With Reality Composer's notifications you can implement two scenarios:

Action listener

This is your case and it's easy to implement using

public var onAction: ((RealityKit.Entity?) -> Swift.Void)?.

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let scene = try! Experience.loadScene()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        arView.scene.anchors.append(scene)
        
        scene.actions.attackCompleted.onAction = notificationID   // listener
    }
 
    fileprivate func notificationID(_ entity: Entity?) {        
         print(scene.actions.attackCompleted.identifier)
    }
}

enter image description here

Here is one more example of how .onAction completion handler can be used.


Trigger for action

When you need to notify Reality Composer's scene to play an action use the following scenario:

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let scene = try! Experience.loadScene()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        arView.scene.anchors.append(scene)
    }
    
    @IBAction func press(_ sender: UIButton) {
        scene.notifications.spinner.post()            // trigger for action
    }
}

or use a subscript for [NAME.NotificationTrigger]:

@IBAction func press(_ sender: NSButton) {
    scene.notifications.allNotifications[0].post()
}

enter image description here

Here's one more example of how .post() instance method can be used.

P. S.

If you need more info, read this post.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Your case you set it up to specifically print that statement after a time interval. I need something like "didReceiveNotification" or something like a listener function, or is that not available? I mean, I can send the notification ,so how do I receive it? – Mason Ballowe Feb 14 '22 at 23:52
  • I think the answer is an implementation of the following: let newUpdate = arView.scene.subscribe(to: SceneEvents.Update.self – Mason Ballowe Feb 15 '22 at 00:10
  • 1
    IE, how do I get an input to xcode of "attackComplete" – Mason Ballowe Feb 15 '22 at 00:18
  • 1
    It did seem to work in preliminary tests. Thank you. – Mason Ballowe Feb 16 '22 at 19:23