[SOLVED] How to carry data between scenes

Hi! I know there are other topics out there regarding this, but I can’t understand them well due to how they load scenes differently from my project. I want to transfer health and ammo between scenes, this is my project.

Hi @Cryptonaph,

Several ways to do that, for example the easiest but least elegant attach your data to the window global object:

window.gameData = {
   ammo: 10,
   health: 100

Now this object is accessible from any JS script.

Another way is to load your scenes additively from a master scene. That master scene can have a script that is responsible for holding the required data. Check this example on how you can add/remove scenes from a base scene:


1 Like

Could you say that again? I’m having trouble understanding this. Do you mean it can be accessed from anywhere? Or is that not possible? I only need it to be accessed by one script.

Assuming you meant “Now”, can I do window. followed by any name to have data linked to different things? Or can it only be window.gameData?

1 Like

Also, can I add to game data in different scripts? Like, let’s say my ammo and health are in separate scripts, can this still work?

And lastly, how do I call from this global?

Wait! I might have figured it out on my own, one moment!

Nevermind, I still need to know how to add more values in different scripts.

I decided to take a different approach, I just have multiple gameDatas, one called window.vitals and one called window.gunData

1 Like