Engine / Scene with multiple registered views fires `<any>Observable` for each of the registered view


I am not sure if it’s a bug or intended but:

Scene with multiple registered views fires *Observable for each of the registered view.

this._scene.registerBeforeRender(() => {
      // If you have 4 views registered, the console log will show the same timestamp 4 times

I handle char movement in this observer and the result is 4 times the speed :slight_smile: Is this intended? If so, how to prevent it? I can see that the observer is created only once.


Hi, want to reproduce that on the playground so we can be 100% we understand what views are and what the issue is?


That sounds like a lot of work (especially the character movement part) and a lot of time I don’t have. Is there nothing you can do without a playground?

A reproduction is just to show us what works incorrectly. I don’t want to see the character movement. I want to see the callback being called 4 times for 4 views.

How can I add HTML in the playground? document.createElement does not seem to do anything. I need to add multiple <canvas elements

And here is the issue, right? We have no idea what you mean when you say views, so it needs to be reproduced and/or very well explained.
Long story short - if you have multiple canvases and (i assume) multiple engines (?) then this function will be executed Many times. It also depends on your render loop and many different things.

If you can share the project or reproduce this, I can help further. If not, I hope there is someone else in the community that is able to do that.

BTW - the playground is web based. everything that is working locally will work in the playground. You just need to consider the playground’s architecture when doing that.

Multiple canvases, one engine.

I found a codeppen online and added a console log and you can see that every timestamp is logged twice.

Let me know if you can see it :slight_smile:

1 Like

This is the expected behavior.
The render function function is being called for every canvas/view you add. The onBeforeRender is being called before each render function is called and will therefore be executed N times for N canvases.

What you can do is check is this.activeView when running your function and see if it is the first one and run your function only then.

Maybe we could add onBeforeViewRendered observable, and then instead of using the scene’s onBeforeRender you could use the view’s. @Deltakosh, any objection here?


Ok thanks, I can get the active view from the engine using the HTML element. Is there any other place that might cause any strange behaviours when using multiple views?

Also how about this.scene.*any scene observable*(() => {}, view?: EngineView)

Which would only run that observable for the defined view?

No all good for me

Since the scene will render twice, those observables that are related to the scene rendering process will be triggered twice. If you are able to use viewports instead you won’t have this problem (as there are different cameras that render inside a single render loop).

Otherwise, you will have to check for the current view in every callback.

I made a few changes to EngineView:

1 Like