question

brendan avatar image
brendan asked

Save a Specific User's int Variable (Unity)

Politicalpeacestudios
started a topic on Wed, 26 August 2015 at 4:37 PM

I'll just try to paint you a mental picture of my problem. Imagine the whole Unity, Android app is 2 scenes, A Register/Login Scene, and another scene that has 1 button. The button has a int variable that is set to 0, 0 meaning the button is LIVE and ready to push. When the button is pressed the 0 becomes a 1, 1 meaning the button is DEAD and can not be pushed anymore.

Ok that works fine. But if you close the app, log back in, the button you pressed that should have an int of 1, is back to 0. Of course because I never saved the int. But I need that int to be super secure so I can't save it on the device. Thats where Playfab kicks in.

How do I get this specific users button int, save it to Playfab, and also load it through Playfab using C#. So when the user presses the button, the int turns to 1, turning the button off, saving on playful his int so that the button stays off when the user logs out and logs back in.

Hope this makes sense, thanks in advance to anyone who decides to throw me a bone.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
brendan avatar image
brendan answered

1 Comment
Brendan Vanous said on Wed, 26 August 2015 at 5:32 PM

If it needs to be super-secure, as you say, you should use User Read-Only Data for this, and update the value in Cloud Script. After all, if you give the client the authority to write the data, cheating it becomes trivial for a technically-minded person. Because of that, part of the solution is necessarily JavaScript, since that's the language you'll use for the Cloud Script. The default script is automatically added to all new titles, so when you click on the Servers tab, you'll see a good example of how to use Cloud Script. The call you'll want to make there to write the data is server.UpdateUserReadOnlyData. The docs for it are here: https://api.playfab.com/Documentation/Server/method/UpdateUserReadOnlyData. But the key thing to know is that you'll be writing a key/value pair out in the data, and you need to specify the PlayFabId of the player, as shown in the Cloud Script example. If you set the Permission to Public, you'll enable other users to also be able to read that data, if you need that as well.

On the client, you'll read the data using https://api.playfab.com/Documentation/Client/method/GetUserReadOnlyData. The call will look something like this:

GetUserDataRequest request = new GetUserDataRequest();

request.Keys = new List<string>();

request.Keys.Add(buttonState);

PlayFabClientAPI.GetUserReadOnlyData(request, OnGetButtonStateLoaded, OnGetButtonStateError);

Where buttonState is the name of the key you use to store this data. Of course, you could just leave the Keys parameter out entirely, and get back the full list of all User Read-Only Data for the user.

Then, your OnGetButtonStateLoaded would look something like this:

void OnGetButtonStateLoaded(GetUserDataResult result) {

    UserDataRecord dataRecord;

    result.Data.TryGetValue(buttonState,out dataRecord);

    if (dataRecord != null) {

        // do some logic associated with whether the value is 0 or 1

    }

}

Brendan

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.