Sign In Register

How can we help you today?

Start a new topic
Answered

Saving user specific information

 I've been working on getting into GameSparks and setting up some of the functionality we need for our game. So far I've set up a system for buying virtual goods through our game (made in Unity), but I need a little more than that. The idea is that you can buy different items and once you've bought them you can equip them in the game. So the list of items you own is different from the items you have equipped. Thus we need to keep track of what items are equipped and we want that information to be stored online, so that when users return or log in from another device, the items they had equipped last time they played will still be equipped.


I thought I could use properties for that, but it seems I can't, since properties are static - at least from what I've read on this board. Is there any other way of doing this? It's quite crucial for us and we also need this functionality in other cases. There's several things we want to store the state of, with regards to a specific user, not only for virtual goods.


Regards

Heino Jørgensen


Best Answer

You can achieve this by storing custom ScriptData on the Player Object. Here's an example:


Setting the Data

var player = Spark.getPlayer();

var equippedItems = {
        "helmet" : "Golden Helmet",
        "armour": "Golden Armour",
        "leggings" : "Golden Leggings",
        "gloves" : "Golden Gloves",
        "boots" : "Golden Boots"
    };

player.setScriptData("equippedItems", equippedItems);


Retrieving the Data

var equippedItems = player.getScriptData("equippedItems");

 

Hope this helps,


Mantas


Answer

You can achieve this by storing custom ScriptData on the Player Object. Here's an example:


Setting the Data

var player = Spark.getPlayer();

var equippedItems = {
        "helmet" : "Golden Helmet",
        "armour": "Golden Armour",
        "leggings" : "Golden Leggings",
        "gloves" : "Golden Gloves",
        "boots" : "Golden Boots"
    };

player.setScriptData("equippedItems", equippedItems);


Retrieving the Data

var equippedItems = player.getScriptData("equippedItems");

 

Hope this helps,


Mantas

That certainly seems to be what we're looking for, Mantas. Is the code snippets you've posted supposed to be Cloud Code? I'm not really familiar with Cloud Code yet, I have to admit. How would I go about getting this information into Unity?

 

Yes this example is using Cloud Code, you can check out a short tutorial on using Cloud Code here. The tutorial shows you how to use Cloud Code with Events, however you can add custom Cloud Code on any of the Requests as well as Responses. In your example, you would store the items on the Player Script Data using an Event executed at some point using logEventRequest (You would use Setting the Data example from my previous answer) and retrieve the Player Script Data by adding the following Cloud Code to the AuthenticationResponse

var player = Spark.getPlayer();
Spark.setScriptData("equippedItems", player.getScriptData("equippedItems"));

At this point you will be able to access equippedItems in the AuthenticationResponse Handler you've setup in Unity.


Regards,


Mantas

Figured it out now! Works nicely. Thanks for your help and fast replies, Mantas.

One last small thing: During the process of me figuring out this stuff, I accidentally created other ScriptData that I don't need, which is now connected to the test user I have. So when I do an AccountDetailsRequest for this test user, I have the both an 'equippedItems' and an 'equippedItemsList'. Is there a way to delete the script data that I don't need? Not that it's a big issue, since the actual users will never have the wrong one created, it'd just be nice in my test to get rid of it.

 

Happy to help.


If you take a look at the SparkPlayer documentation. You will notice removeScriptData Method, this is what you need. You can also see all of the available methods for all Spark Objects in the Cloud Code editor using the dot notation.


Regards,


Mantas

Thanks. That should be all for now then. Thanks again.

 

Login to post a comment