question

deaverinc avatar image
deaverinc asked

having trouble getting information on the second request

i feel like there is something i am doing wrong but i dont know what it is. this is the second time i am having trouble making a request for data that it fails either on the first try or the second try. i am currently trying to retrieve user data and it works the first time but when i change the data and save it i cannot retrieve the new data. i get the old data like it is being stored somewhere even though i can see it has been updated in playfab. i am trying to get key/value data. i understand this is probably going to take so time to fully explain but any help would be appreciated. here is my code:

i initially get the data here:

public static void UpdateInventoryInfo() {

for (int i = 0; i < InventoryContents.Count; i++) {

if (i < AccountInfo.Instance.Info.UserInventory.Count) {

if (AccountInfo.Instance.Info.UserInventory[i].ItemClass == GameConstants.ITEM_CARDS) {

InventoryContents[i].GetComponent<ItemSlot>().Card = AccountInfo.Cards[i];

InventoryContents[i].SetActive(true);

InventoryContents[i].transform.GetChild(0).GetComponent<Image>().sprite = AccountInfo.Cards[i].Icon;

InventoryContents[i].transform.GetChild(2).GetComponent<Text>().text = AccountInfo.Cards[i].Name;

InventoryContents[i].transform.GetChild(1).GetComponent<Text>().text = string.Format("Cost: {0}", AccountInfo.Cards[i].Cost); }

else { InventoryContents[i].SetActive(false); } }

else { InventoryContents[i].SetActive(false); } } }

this gives me an inventory that i can drag and drop from to create a hand.

public void OnDrop(PointerEventData eventData) {

if(full == 8) { return; }

ItemSlot d = eventData.pointerDrag.GetComponent<ItemSlot>();

if (d != null) {

d.parentToReturnTo = this.transform; }

if(full <= 7) {

AccountInfo.Deck.Add(card); GameObject droppedObject = eventData.pointerDrag;

AccountInfo.Deck[full] = droppedObject.GetComponent<ItemSlot>().Card;

droppedObject.tag = "Deck1";

MainSceneController.LastHand = false;

MainSceneController.CurrentCost += AccountInfo.Deck[full].Cost;

full++; } }

i save the info and it writes to the key/value, Deck/String

public static void AddToDeck() {

string deckContents = "";

foreach (CardStats item in Deck) {

deckContents += item.Name + ","; }

Dictionary<string, string> data = new Dictionary<string, string> { {

GameConstants.DATA_DECK, deckContents } };

UpdateUserDataRequest request = new UpdateUserDataRequest() {

Data = data };

PlayFabClientAPI.UpdateUserData(request, GotData, GameFunctions.PlayFabAPIFailure); }

private static void GotData(UpdateUserDataResult result) {

Debug.Log("Account Info Got Data Updated Data"); }

this works and i get the debug statement in the console everytime i save. i try to retreive the data like this:

public static void UpdateTheDeck() {

UserDataRecord temp;

if(Instance.Info.UserData.TryGetValue(GameConstants.DATA_DECK, out temp)) {

Instance.deckInfo = temp.Value.Split(',');

for (int k = 0; k < Instance.deckInfo.Length - 1; k++) {

for (int j = 0; j < Instance.Info.UserInventory.Count; j++) {

if (Instance.deckInfo[k] == Instance.Info.UserInventory[j].ItemId) {

Deck.Add(Database.GetCardInfo(Instance.Info.UserInventory[j], j));

break; } } } }


MainSceneController.UpdateDeckInfo(); }

it works the first time but if i change the data and resave it, then try to retreive it i get the old data and not the new data like it is somehow being stored somewhere even though i can see in playfab that the key/value has been updated each time i save. also as a note if i quit the app and restart i get the new data like it has been there all along. i will be glad to provide any code that i have left out and again i think there is something that i dont understand when retreiving key/data. any help will be appreciated. thanks.

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.

deaverinc avatar image deaverinc commented ·
0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ commented ·

I noticed that you have this line “if(Instance.Info.UserData.TryGetValue(GameConstants.DATA_DECK, out temp)) {“ where you try to get the user data from Instance.Info.UserData, how is this value getting changed?

Could you please also try the API on Postman and see if you can reproduce this issue?

0 Likes 0 ·
deaverinc avatar image
deaverinc answered

ok, so i went back to the tutorial and i think i have found my problem. i was trying to use "trygetvalue" instead of "getuserdata". i thought try get value would work since the dictionary string was already written but apparently thats not the case. it works everytime with get user date. if you could shed some light on the difference between the two i would appreciate it. thanks.

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.

Rick Chen avatar image Rick Chen ♦ commented ·

The GetUserData is a PlayFab API defined in the PlayFab UnitySDK, it is used to retrieves the title-specific custom data for the user which is readable and writable by the client. While the TryGetValue is a dictionary method in C#, you can use this function to get a value with a key from a dictionary.

In other words, you use the GetUserData to get the data from the server, and it will return the user data in the format of dictionary, then you use the TryGetValue method to get the value from the dictionary.

1 Like 1 ·
deaverinc avatar image
deaverinc answered

I am not currently familiar with postman but i will look into it. the line,

if(Instance.Info.UserData.TryGetValue(GameConstants.DATA_DECK, out temp)) {

is a check against the key/value and should be updated with this code:

public static void AddToDeck()

{

string deckContents = "";

foreach (CardStats item in Deck)

{

deckContents += item.Name + ",";

}

Dictionary<string, string> data = new Dictionary<string, string>

{

{GameConstants.DATA_DECK, deckContents }

};

UpdateUserDataRequest request = new UpdateUserDataRequest() {

Data = data };

PlayFabClientAPI.UpdateUserData(request, GotData, GameFunctions.PlayFabAPIFailure); }

private static void GotData(UpdateUserDataResult result) {

Debug.Log("Account Info Got Data Updated Data"); }

10 |1200

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

deaverinc avatar image
deaverinc answered

so, your saying i need to use both when trying to get the data since the value is a string?

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.