Saving the amount of coins the player has, then getting that value in a different scene

hi everyone! i have a question regarding collection and variable storage. how can i make a script that saves the amount of coins the player has, then getting that value in a different scene? i already have the pickup script, so when ever the player picks up a coin, it adds it to the total value of the text on the ui. but how do i carry that data over to other scenes, so that a script can check the data?(for example, this is for a in-game store.) help would be greatly apreciated!

Hi @quantum,

If you are destroying the existing hierarchy on scene change, and loading a new one, a good way to persist data is in a global object.

A common trick is to add it under the browser window object.

For example:

window.Score = {
   coins: 0
};

Now in any script to write to that you do:

window.Score.coins += 3;

To read from that value:

const coins = window.Score.coins;
3 Likes

thanks! i switched up my plan for it. i am now planning to use playfab.com. any idea how i would add the economy and player login to the game? i am a bit confused on that part…

Ah sorry, I haven’t used it myself.

PlayCanvas scripting being regular JS will work quite easily with their SDK. Try checking their tutorials/example projects.

Hey @quantum,

PlayFab has a fantastic API - REST API Reference - PlayFab | Microsoft Docs. You can use this by using any form of requests; I would recommend using PlayCanvas’s request system - Http | PlayCanvas API Reference. For authenticating a user, I’ve found google login to work seamlessly with PlayCanvas, so I would imagine the other methods would as well, since it’s just simple HTML/JS.

thank you!