Search Unity

Question Loading GF with a claimed reward fails

Discussion in 'Game Foundation' started by Sukender_OB, Jan 7, 2021.

  1. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Hi there,

    I added a very basic reward claim (using prefabs of v0.8). and it works as expected when claiming and saving data. My save file (local persistence) contains stuff like:

    "rewardManagerData":{"rewards":[{"key":"MY_REWARD_ID","claimedRewardItemKeys":["some GUID"],"claimedRewardItemTimestamps":[some timestamp]}]}

    But when I re-run the game, GF fails at initializing, with the following error:

    Game Foundation : GameFoundationSdk - Failed to initialize the SDK: System.ArgumentException: An item with the same key has already been added. Key: MY_REWARD_ID
    at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert
    at System.Collections.Generic.Dictionary`2[TKey,TValue].Add
    at UnityEngine.GameFoundation.RewardManagerImpl.GetData
    at UnityEngine.GameFoundation.RewardManagerImpl.InitializeData
    at UnityEngine.GameFoundation.ManagerImplementation.Initialize
    UnityEngine.GameFoundation.Components.GameFoundationInit:Awake



    Any idea?
    Thanks!

    Cheers,
     
  2. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    Hi @Sukender_OB,

    I'll see if I can figure out what's causing this. Unfortunately I wasn't able to reproduce it yet.

    If you rename or delete the save file, does Game Foundation then initialize just fine?

    Also, if you open up your catalog asset in the Project window, are there somehow two reward definitions with the same key? (maybe a problem with the reward editor if so)
     
  3. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Thank you @richj_unity for your quick answer.

    Indeed, deleting the save file makes GF loads perfectly. Anything else in the save file is okay too (currencies, inventory items). By the way, GF Debugger is helpful, thanks for this feature! And of course I already checked for duplicates. So maybe I missed something?

    If you want, here are my game saves and catalog [EDIT: removed temporary download link].
     
    Last edited: Jan 12, 2021
  4. richj_unity

    richj_unity

    Unity Technologies

    Joined:
    Sep 23, 2019
    Posts:
    40
    Unfortunately I wasn't able to get the download to work (I tried a few times last week). Since the save is just a text file, maybe you can post that as text, and the reward definition as a screenshot?
     
  5. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Ho. Maybe your proxy / antivirus did not manage to analyze a 7-zip archive? I'll post as requested, but can you try copying-pasting this one please? http://sukender.free.fr/vrac/GF.zip Thanks.
     
  6. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Here. My definition is dead simple: daliy only gives 50 gold each day. No static properties. No tags. Nothing special (see screenshot below). And no duplicates, of course.

    And about my save files:
    1. It works when it contains "rewards":[]
    2. But fails with "rewards":[{"key":"daily","claimedRewardItemKeys":["51149bcb-20ae-456b-8e92-8c66e13c339a"],"claimedRewardItemTimestamps":[637456813060575376]}]

    upload_2021-1-12_6-53-35.png

    Here is my save file, formatted, and pruned from items and useless currencies:
    Code (csharp):
    1.  
    2. {
    3.    "inventoryManagerData":{
    4.       "items":[SOME ITEMS],
    5.       "itemLists":[],
    6.       "itemMaps":[]
    7.    },
    8.    "walletData":{
    9.       "balances":[
    10.          {"currencyKey":"gold","balance":50},
    11.         MORE_CURRENCIES
    12.       ]
    13.    },
    14.    "rewardManagerData":{
    15.       "rewards":[
    16.          {
    17.             "key":"daily",
    18.             "claimedRewardItemKeys":["51149bcb-20ae-456b-8e92-8c66e13c339a"],
    19.             "claimedRewardItemTimestamps":[637456813060575376]
    20.          }
    21.       ]
    22.    },
    23.    "version":0
    24. }
    25.  
    As posted before, full data is here: http://sukender.free.fr/vrac/GF.zip

    Thanks a lot.
     
  7. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Going debug!

    1. Parsing error (bug)
    I found MiniJson.cs to try converting end-of-file character (ie. -1) to a char. This results in the following exception:

    Code (text):
    1. System.OverflowException: Value was either too large or too small for a character.
    2.   at System.Convert.ToChar (System.Int32 value)
    3. in <9577ac7a62ef43179789031239ba8798>:0 at UnityEngine.GameFoundation.MiniJson.Json+Parser.get_peekNextChar ()
    4. in [...]\\com.unity.game-foundation@0.8.0-preview\\Core\\Runtime\\Utils\\MiniJson.cs:401 at UnityEngine.GameFoundation.MiniJson.Json+Parser.EatWhitespace ()
    5. in [...]\\com.unity.game-foundation@0.8.0-preview\\Core\\Runtime\\Utils\\MiniJson.cs:392 at UnityEngine.GameFoundation.MiniJson.Json+Parser.get_getNextToken ()
    6.    [...]
    This happens because we actually reach EOF in MiniJson.JSon.Parser.EatWhitespace(). Simply checking before entering loop is enough to work around this:

    Code (CSharp):
    1. void EatWhitespace()
    2. {
    3.     if (m_Json.Peek() == -1) return;        // Added
    4.     while (char.IsWhiteSpace(peekNextChar))
    5.     // [...]


    2. Rewards initialization error (bug???)
    I'm clearly not confident with this one, but it seems there's a typo (or a bad copy-paste) in RewardManagerImpl.GetData(). Indeed, during initialization it seems to try getting a reward instead of a reward definition if possible (if I'm not mistaken). It reads:

    Code (CSharp):
    1. foreach (var eachRewardData in data.rewards)
    2. {
    3.     if (rewardData.key.Equals(rewardDefinition.key))
    4.     {
    5.         rewardData = eachRewardData;
    6.     }
    7. }
    But this searching loop is suspicious, since it doesn't make use of eachRewardData in the condition. It uses the upper-scope variable rewardDefinition instead. I thus changed it to:

    Code (CSharp):
    1. foreach (var eachRewardData in data.rewards)
    2. {
    3.     if (rewardData.key.Equals(eachRewardData.key))        // Changed
    4.     {
    5.         rewardData = eachRewardData;
    6.     }
    7. }
    ... and now the duplicate insertion in Dictionary, happening line 214 (m_Rewards.Add(rewardData.key, reward);) does not happen anymore.

    Everything seems fine now... But please double check my answer to tell me if I didn't break anything! Thanks a lot.
    Cheers,
     
    richj_unity and erika_d like this.
  8. U_AdrienPDB

    U_AdrienPDB

    Unity Technologies

    Joined:
    Aug 14, 2019
    Posts:
    8
    Hi, I was just about to tell you that we fixed the issue about the reward initialization and it will be out for next release.
    But I see you found the fix too. ^^

    About the parsing error I'm curious to know when does it happen ? We use it a lot for our back-end adapter but never faced this issue before.
     
    richj_unity and erika_d like this.
  9. Sukender_OB

    Sukender_OB

    Joined:
    Nov 14, 2019
    Posts:
    18
    Woa! Nice timing! :D

    Well, I spotted that just because of debugging. Maybe that this exception is caught silently and "that doesn't mind" since parsing reached the end-of-file??? Anyway, it happened with my "offending" save game. Not tested with others. In any case, line I proposed is harmless - maybe worth adding it.

    Looking forward for next GF release! Thanks for your work!
     
    richj_unity and erika_d like this.