question

bunzaga@gmail.com avatar image
bunzaga@gmail.com asked

How to initialize user data per-account?

I saw this old thread:

https://community.playfab.com/hc/en-us/community/posts/206710847-Adding-custom-user-data-on-creation-of-new-account

But I wasn't sure if it would be appropriate to bump that up after so long, so I'll ask in a new topic.

I was able to create several keys and set the initial data under Players->[select user]->Player Data->User Read Only Title Data, but then when I go to other players, these values are just empty.

So I'm assuming I would have to do some kind of cloud script to check if the keys have been created, if not created, then create them, populating them with initial values, then return them.

With this system, I'd have to check if every single key I ever want to create has been created and initialize it with its initial value through code, every time the user logs in.

Is there a better way to do this?  Is there a better place to put this data?  Things like equipped gear, achievements, progress, various account 'unlocks', etc.

10 |1200

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

brendan avatar image
brendan answered

Yes, that would be the way to approach this - have a script which updates the data when the user signs in. The other thing a script like this would be useful for is adjusting values or removing/replacing items which need to change as you update your game over time. So, you could have a version value on the player which you check and, if the player's account isn't on the latest version, apply the appropriate updates.

10 |1200

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

bunzaga@gmail.com avatar image
bunzaga@gmail.com answered

Here's an example of what I am doing now, let me know if this could be better or changed.

var defaultUserData = {
"s0":"helm_none",
"s1":"tunic_none",
"s2":"gloves_none",
"s3":"pants_none",
"s4":"boots_none"
};
handlers.initUserData = function ()
{
var oldData = server.GetUserReadOnlyData({PlayFabId:currentPlayerId, Keys:null});

var count = 0;
var newData = {};

for(var key in defaultUserData){
if(defaultUserData.hasOwnProperty(key)){
if(oldData.Data[key] === null || oldData.Data[key] === undefined){
newData[key] = defaultUserData[key];
count++;
}
}
}

if(count > 0){
var updateUserDataResult = server.UpdateUserReadOnlyData({
PlayFabId: currentPlayerId,
Data:newData,
"Permission": "Public"
});
}

return server.GetUserReadOnlyData({PlayFabId:currentPlayerId, Keys:null});
}

This does everything I need: Creates the keys if they don't exist, which allows me to add more over time.  It doesn't add anything if it isn't needed.  It also returns the data, so I can populate the needed UI elements in the game.  The only thing which it doesn't do, is allow me to delete old keys which may no longer be in use, but I can add that (I didn't think of it before).

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.