Reloading scene (resetting a script)

Hello,

how is it possible to reload your scene as in unity or to refresh your code without reloading or refreshing the browser page ? (i have read in PlayCanvas website something about hot reloading can any one explain more about it) and if there is any other way to achieve my goal.

Thank you.

Hi @abd_almajeed_saleh,

Are you talking about reloading your scene or code during development?

Indeed Playcanvas provides hot reloading which is pretty cool, to be able to update/change your code and see the effects in the game world without having to restart your app. You can read more about it here:

https://developer.playcanvas.com/en/user-manual/scripting/hot-reloading/

To reload your scene, setting for example all objects to their initial state, that can’t be done automatically. You will have to use hot reloading to reset their state your self in code, which isn’t so hard to do, but you have to take care of doing this in code for each entity/script in play.

1 Like

Thanks for replying.
Yes you are right, what i am looking for is how to reload my scene at some point in the game or by clicking a UI button

Good, I can see two approaches in doing this:

  • In your initialize() method save in variables/properties the initial state of each entity (position, rotation, script attributes etc). When you reset the scene, you set your entity back to those values.
  • Keep your editor entities as a template, switched off (enabled = false), and on runtime spawn a clone of those entities in your game world. When you reset the scene, remove those entities and clone new ones from the template.

The second approach can have a performance penalty (load time) since the garbage collector will have to clean up memory from the destroyed entities. But it is easier to use in larger projects.

1 Like