Events for view registration/unregistration

As far as I can tell, there is no way to be notified when a view is registered/unregistered (RegisterView/UnregisterView). What I was hoping to find were events like Engine.onBeforeViewRegisteredObservable/onAfterViewRegisteredObservable/onBeforeViewUnregisteredObservable/onAfterViewUnregisteredObservable. The reason I want these is that the place in my code where I instantiating the engine and starting the render loop is very far away from the part in the code where views are registered/unregistered, and I only want to run the render loop if at least one view is registered. I think events like this could allow me to do this. If there is another way to achieve the same thing, I’m not sure what it is. Thoughts?

Hey Ryan,

great to see you here :slight_smile:

You could register a render loop that checks the views array and only renders the scene if the array is populated. this way the function will do nothing apart from waiting for a new view to register. Would that work?

There are currently no observables that do that, if you want to create an issue for us, so we can implement it in the next version (4.2)

Thanks for the suggestion @RaananW. However, this is effectively what my render loop function already does:
engine.runRenderLoop(() => {
if (engine.activeView?.camera !== null) {
engine.activeView?.camera?.getScene().render();
}
});
In this case though, I’m running a render loop that is effectively just polling for an active view. My intuition is that this isn’t great from a resource standpoint, especially on a battery constrained mobile device, and so my hope was to just suspend the render loop entirely in this case. Let me know if you have other thoughts, otherwise I will create an issue.

I agree that it is redundant and will use a bit of resources (which are limited on many mobile devices)

You can always extend the engine’s functionality using meta programming:

https://playground.babylonjs.com/#E8LGKI#1

This is also allowed in typescript (probably needs a bit of cleaning up thou), and can be applied to both register and unregister functions. You don’t have to use Babylon’s observables, it is just an example of how it can be used.

Please do create an issue, we will incorporate this probably in 4.2 (we are in code freeze right now).

1 Like