question

nicolasmozes avatar image
nicolasmozes asked

Saving RTS game state

Hi there,

Hoping someone has any good tips on this.

What I have done so far is saving the player's resources on the Player Statistics, but now I'm trying to figure out how to save the units and buildings information.

I tried to save it as objects in Player Data, but when getting the Vector3 position passed on as JSON, its coming up empty.

Not sure what the best way to do this would be. Any recommendations are appreciated. : )

10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

Data in the Player Data will be stored as JSON String. To get it on the client, you need to deserialize it first. For example, here is the data I created.

And below is the code how I get the Position x data in Unity.

public class Building {
        public Dictionary<string, int> Position;
        public int Level;
        public List<string> Updates;
    }


PlayFabClientAPI.GetUserReadOnlyData(new PlayFab.ClientModels.GetUserDataRequest { Keys = new List<string> { "Buildings" } },
                    result => {
                        var Data = JsonConvert.DeserializeObject<Dictionary<string, Building>>(result.Data["Buildings"].Value);
                        Debug.Log(Data["PowerStation"].Position["x"]);
                        PlayFabClientAPI.UpdateUserData(new PlayFab.ClientModels.UpdateUserDataRequest { Data = new Dictionary<string, string> { { "PowerStation", Data["PowerStation"].ToString() } } }, result => { }, error => { });
                    },
                    error => {
                        Debug.LogError(error.GenerateErrorReport());
                    });

If you are using a different platform or the data structure doesn’t meet your requirement, please feel free to let me know.


1.png (15.2 KiB)
10 |1200

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

nicolasmozes avatar image
nicolasmozes answered

Thanks! I will look at your code. I tried using JsonUtility and SimpleJson, but both came back empty into playfab when adding a Vector3.

So, saving simple ints and strings is working ok, but the Vector3 is where I have issues.

Ill get bacvk to you later : )

2 comments
10 |1200

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

Gosen Gao avatar image Gosen Gao commented ·

If you still have the issue about Vector3, can you give us the code how you store the Vector3 data to PlayFab? That will help us dig into it. Please remove any sensitive info.

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao commented ·

BTW, please don't use SimpleJson directly, it may cause unexpected issues. It is recommended to use PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).DeserializeObject to handle the JSON object.

0 Likes 0 ·

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.