Utilising PlayCanvas Observers in App

Hey All,
This is more of a discussion than anything else to get some thoughts on an approach I am planning on taking witha new project. The project requires me to efficiently handle Scene updates on data changes within a JS object + Undo/Redo would be preferred at some point + A reliable and a sure shot way of adding/removing entities from scene when a JS object is updated without failure.

I can surely implement my own class to achieve this functionality but looks like Playcanvas Observer is quite powerful in itself, not to mention it powers the whole editor UI and is proven to work as expected. Will surely save me a lot of time as well.

My question is, does the playcanvas team recommend that we as developers use the Observer class in projects (definitely by localising it to avoid any changes in the future)? If so, are there downsides that I should be familiar with before going down this path? My biggest hesitation is with the fact that this is not a part of the engine core and as a result not just available as a part of the engine API itself (pc.Observer), that could have been done in order to reduce the engine size but not entirely sure.

Would really appreciate any help with this one. If this is a possibility I would like to make a demo project that demonstrates + helps me figure this out. Also hope that it would someone else in the future as well.

Regards
-Tanay

Observer is quite powerful and suitable in many cases, where you need to know if and when an object field was changed. Max made it as a standalone library that you can use here:

2 Likes

This is a lifesaver!
Thank you so much @LeXXik and Max. This gives me a great starting point!!

Whoah, hold on. It’s already a standalone library:

Max’s library is his own implementation of the observer pattern.

The PlayCanvas observer NPM package is 100% intended to be used by any developer. That is the main reason why we open-sourced it.

The API reference for the Observer is included alongside the Engine API (and other APIs):

And lastly, it doesn’t make sense for the observer to ship in the Engine. It’s there to underpin web application front-ends (PCUI in particular). For example, the Editor, SuperSplat, the glTF Viewer, the Texture Viewer and so on. There’s an argument it could live in the PCUI library, but splitting it out gives you the flexibility to use it in conjunction with other front-end solutions.

3 Likes

Thanks for sharing that @will. I eventually ended up using this package when @LeXXik recommended the other package. It just seemed more sensible to use the one provided + used by the playcanvas team.

Really appreciate the help on this forum :slight_smile:

2 Likes