There is unexpected behavior in my project. I am new to PlayCanvas and the problem certainly can be a fault in my code. The full code is included below.
Questions
- How can I change my sourcecode to meet my expected behavior?
- Does this demonstrate a PlayCanvas bug? If yes, when will it be fixed?
Demo
I created a demo project to test reloading the current scene. I attempt to use the recommendations I see on the forum.
Expected Behavior: During ONE session, it works the first time and EVERY time after the first.
Actual Behavior: During ONE session, it works the first time. Then breaks the 2nd time sometimes. It breaks the 3rd time always.
https://playcanvas.com/editor/scene/919848
Image
Log showing a) expected, b) expected, c) not expected
Here is my fixed fork: https://playcanvas.com/editor/scene/919880
When listening for events on object whose lifetime is not the same as the listenerās, it has to be removed at some point (in this case, the listenerās entity destroy event).
As it wasnāt removed, it was calling the callback twice the second time round.
1 Like
Excellent. That workaround performs well.
It is unmanageable to handle a complete game this way.
As a core API request, I recommend a few mutually exclusive features
- API allows to load new scene non-addititively where nothing in runtime persists except the new scene (Unity calls this āSingleā https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Single.html)
- API treats destroy as a first class citizen of script (e.g. like āinitializeā and āupdateā) and does not require manual listening.
- API allows for a version of observing events [e.g. "blah.on(āblahā, this.handle, this)] which will release the event subscription from blah if āthisā is destroyed.
- API allows for app.Restart() or similar which mimicks the first-run of the application which is somehow faster for developers than refreshing the page (takes about 5 seconds for me)
Thanks!
Great info per 1/2/3. Per #4ā¦I donāt know how to answer the question. Here is a video of my (relatively small?) project as I refresh the browser.
Interesting. As you are using physics, that delay could be due to the Ammo physics library being parsed and started (it is a pretty hefty file and using WASM) 
Edit: Doesnāt seem like it using my own project. I would look into the Chrome Devtools to see what could be causing it to take so long after a standard refresh. This a recent example project with Physics
Great info.
Yes, this is a physics project using Ammo. In the video I shared, its about 5 seconds from clicking refresh to scriptās initialize(). That was the latest firefox. I just tried in the latest chrome. Its about 4 seconds.
Iām very jealous of your < 1 second refresh 
Iām new to JS & browser development. What can I disable in Chrome reduce the refresh time?
Is the project public? It be interesting to see if I can repro it on my local laptop. That will help isolate if itās an engine issue vs maybe something to do with the local setup.
Edit: I do recommend looking at using the browser devtools as they are pretty awesome: https://developers.google.com/web/tools/chrome-devtools
They are able to give full breakdowns of CPU time, load time etc as well as the usual code debugging (breakpoints etc). 