question

thejamiryu avatar image
thejamiryu asked

Help about Entity

I did as described from https://api.playfab.com/docs/tutorials/entities/getting-started-entities but still I'm getting error(OnPlayFabError).

using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
using PlayFab.AuthenticationModels;
using PlayFab.DataModels;
using EntityKey = PlayFab.DataModels.EntityKey;


public class KongregateHandler : MonoBehaviour


    public string entityId;
    public string entityType;


PlayFabClientAPI.LoginWithKongregate(new LoginWithKongregateRequest
        {
            KongregateId = kongregateId,
            AuthTicket = authTicket,
            CreateAccount = true
        }, OnLoggedIn, OnFailed);
        PlayFabAuthenticationAPI.GetEntityToken(new GetEntityTokenRequest(),
            (entityResult) =>
        {
             var entityId = entityResult.Entity.Id;
             var entityType = entityResult.Entity.Type;
        }, OnPlayFabError);


private void OnPlayFabError(PlayFabError obj)
    {
        Debug.Log("Entity Error");
    }
private void OnLoggedInn(EntityTokenResponse obj)
    {
        entityId = obj.EntityToken;
    }

Can you guys also check SetObjects and GetObjects looks okey ?

public void EntitySave()
    {
        var data = new Dictionary<string, object>()
    {
          {"sHp", karakter.karakterStartHp},
          {"HpMulti", karakter.healthMultiply},
          {"Dmg", karakter.damage},
          {"DmgMulti", karakter.damageMultiply},
          {"AliveB", karakter.EnemyAliveBonus},
          {"Rng", karakter.range},
          {"Rate", karakter.fireRate},
          {"CritC", karakter.critChance},
          {"CritM", karakter.critMultiply},
          {"Steal", karakter.lifeSteal},
          {"Penet", karakter.penetration},
          {"Redu", karakter.damageReduction},
          {"PoiC", karakter.poisonChance},
          {"PoiMulti", karakter.poisonDamageMultiply},
          {"LastS", karakter.lastStandDamageMultiply},
          {"LightC", karakter.lightChance},
          {"LightMulti", karakter.lightDamageMultiply},
          {"Hp", karakter.karakterHp}
    };
          var dataList = new List<SetObject>()
    {
          new SetObject()
    {
            ObjectName = "PlayerData",
            DataObject = data
    },


    };
        PlayFabDataAPI.SetObjects(new SetObjectsRequest()
        {
            Entity = new EntityKey { Id = entityId, Type = "character"},
            Objects = dataList,
        }, (setResult) => {
            Debug.Log(setResult.ProfileVersion);
        }, OnPlayFabError);
    }
    public void EntityLoad()
    {
        var getRequest = new GetObjectsRequest { Entity = new EntityKey { Id = entityId, Type = "character" } };
        PlayFabDataAPI.GetObjects(getRequest,
            result => { var objs = result.Objects; },
            OnPlayFabError
        );
    }

Thank you.

entitiesdata
10 |1200

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

Hernando avatar image
Hernando answered

There are several problems in your code:

  1. You can get EntityID and EntityType from LoginWithKongregate result, so you don't need to call GetEntityToken.
  2. The method "OnLoggedInn" seems like having a spelling mistake, and the parameter is not correct. (Line 37)

In summary, you can modify the method LoginWithKongregate to get the entityId and entityType by referring to the following code.

public string entityId;
public string entityType;

PlayFabClientAPI.LoginWithKongregate(new LoginWithKongregateRequest
{            

	KongregateId = kongregateId,

	AuthTicket = authTicket,

	CreateAccount = true

},OnLoggedIn,
OnPlayFabError);


private void OnLoggedIn(LoginResult result)    {

	entityId = result.EntityToken.Entity.Id;         
	entityType = result.EntityToken.Entity.Type;
}

As for SetObjects and GetObjects, at line 36&44, you should use the field "entityType" as replace to hardcode string.

Please refer to this code:

var getRequest = new GetObjectsRequest { Entity = new EntityKey { Id = entityId, Type = entityType } };
10 |1200

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

thejamiryu avatar image
thejamiryu answered

First of all, thank you so much for your help. Everything is working right now. Only thing left is get saved values. I have set up 3 entity objects for my game data. All key/value pairs are correct when I check it on Game Manager. I believe GetObjectsRequest is not enough by itself. How should I get these saved pairs.

public void EntitySave()
    {
        var data = new Dictionary<string, object>()
    {
          {"L", levelup.level},

    };
          var dataList = new List<SetObject>()
    {
          new SetObject()
    {
            ObjectName = "PlayerData",
            DataObject = data
    },


    };
        PlayFabDataAPI.SetObjects(new SetObjectsRequest()
        {
            Entity = new EntityKey { Id = entityId, Type = entityType },
            Objects = dataList,
        }, (setResult) => {
            Debug.Log(setResult.ProfileVersion);
        }, OnPlayFabError);

public void EntitySave2()
    {
        var data2 = new Dictionary<string, object>()
    {

            {"S1", skilltree.survivorSkill1Level},
            
        };
        var dataList = new List<SetObject>()
    {
          new SetObject()
    {
            ObjectName = "PlayerData2",
            DataObject = data2
    },


    };
        PlayFabDataAPI.SetObjects(new SetObjectsRequest()
        {
            Entity = new EntityKey { Id = entityId, Type = entityType },
            Objects = dataList,
        }, (setResult) => {
            Debug.Log(setResult.ProfileVersion);
        }, OnPlayFabError);
    }

public void EntitySave3()
    {
        var data3 = new Dictionary<string, object>()
    {
            
            {"16", forge.forge16Level},
            
        };
        var dataList = new List<SetObject>()
    {
          new SetObject()
    {
            ObjectName = "PlayerData3",
            DataObject = data3
    },


    };
        PlayFabDataAPI.SetObjects(new SetObjectsRequest()
        {
            Entity = new EntityKey { Id = entityId, Type = entityType },
            Objects = dataList,
        }, (setResult) => {
            Debug.Log(setResult.ProfileVersion);
        }, OnPlayFabError);
    }


Thank you very much.

1 comment
10 |1200

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

Hernando avatar image Hernando commented ·

Below is a simple code that demonstrates how to get a value from Object "PlayerData" after calling the GetOjects API.

        PlayFabDataAPI.GetObjects(getRequest,
        result => {
            JsonObject jsonResult = (JsonObject)result.Objects["PlayerData"].DataObject;
            object karakterStartHp;
            if (jsonResult.TryGetValue("sHp", out karakterStartHp))
            {
            	Debug.Log(karakterStartHp.ToString());
            }
        },
        OnPlayFabError)
0 Likes 0 ·
garrygaber avatar image
garrygaber answered

Hi Hernando -

What are you using to get the type JsonObject in the above code?

Kindest Regards,
Garry

10 |1200

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

garrygaber avatar image
garrygaber answered

After some research, I found the answer: just add using PlayFab.Json; to the top of the script.

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.