Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Currency Initial Allocation not saving

Discussion in 'Game Foundation' started by Renea, May 6, 2020.

  1. Renea

    Renea

    Joined:
    Nov 12, 2017
    Posts:
    9
    Hello,
    I've started going through the getting started tutorials and when working the currency portion of the tutorial, I get a warning and incorrect information during runtime. Below are pictures of what I'm seeing.

    Currency Window showing my created item
    Currency window.PNG

    Warning at start of runtime
    Warning.PNG

    Debug log messages. The first one that is highlighted is the initial steps of the currency code, where the second shows that the currency changed. I used the default value of 50 that was used in the tutorial. With this I would expect the initial balance to be 100 and the new balance to be 150, but instead I see an initial balance of 0 and a new balance of 50.
    Balance log entries.PNG
     
  2. antoine-agthe_unity

    antoine-agthe_unity

    Unity Technologies

    Joined:
    Jun 6, 2019
    Posts:
    7
    Hello Renea.
    Thanks for your documented feedback.

    TL;DR: Can you tell me if you load an existing player profile, or if you have this bug when you start a fresh profile?

    The first warning you see is not something you should be afraid of.
    It is displayed when Game Foundation loads a profile which doesn't contain any entry for a
    Currency
    .
    A warning is displayed, but GF just initializes the value to
    0
    .
    Internally, in the team, we're thinking of turning this warning into a simple log.
    At the moment, this info looks like more than a log.
    What do you think?

    Now, why initializing the value to
    0
    , instead of the
    initial allocation
    ?
    It is because this allocation is used only when a new player profile is created.
    If you player profile already exists, it doesn't apply.
    This behaviour fits with our ChilliConnect backend: https://docs.chilliconnect.com/guide/catalog/#currencies
    If you want to know more about ChilliConnect, you'll find a dedicated sample in the Package Manager (Sample 9).

    Can you share a code snippet of the way you initialize Game Foundation?
    It can help me figuring out what happens.
     
  3. Renea

    Renea

    Joined:
    Nov 12, 2017
    Posts:
    9
    Hello Antoine,

    Thank you for responding so quickly.

    When you say "player profile" are you talking about Unity Editor player or an in game player character? If you mean game player character profile, I fear I may have missed something in the tutorials about creating a player profile. I say this based on the explanation you have about the warning message. I initially thought it to be just as you said that it is something that GF displays before it gets to the code for the currency information, but when I went through the tutorial and it showed that I should be seeing the initial allocation in the first log message it concerned me and wandered if the warning message had anything to do with this behaviour. See below snipit.

    Side Note: the link to the "next tutorial" at the end of this page that is supposed to go to the debugger window tutorial is broken.

    currency tutorial.PNG


    I created a brand new Unity 2019.3.12f1 3D project and imported the latest Game Foundation (0.4.0) from package manager and immediately started the tutorials listed here:
    https://docs.unity3d.com/Packages/com.unity.game.foundation@0.4/manual/index.html

    Attached is my full GameManager.cs file for your review. I've completed up through tutorial 8 in the above link, just after the stats tutorials (just to give you an idea of where I'm at in the code). Here is a snip of the awake method showing my GF initialization.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.GameFoundation;
    6. using UnityEngine.GameFoundation.DataAccessLayers;
    7. using UnityEngine.GameFoundation.DataPersistence;
    8.  
    9. /// <summary>
    10. /// Following the tutorials at https://docs.unity3d.com/Packages/com.unity.game.foundation@0.4/manual/index.html
    11. /// This is how the GameManager is set up.
    12. /// The only exception is the Singleton Base class which was retrieved from: https://wiki.unity3d.com/index.php/Singleton#Singleton.cs
    13. /// </summary>
    14. public class GameManager : Singleton<GameManager>
    15. {
    16.     private void Awake()
    17.     {
    18.         // this data layer will not save any data, it is usually used for examples or tests
    19.         IDataAccessLayer dataLayer = new MemoryDataLayer(GameFoundationSerializableData.Empty);
    20.  
    21.         GameFoundation.Initialize(dataLayer, onInitializeSucceeded, onInitializeFailed);
    22.        
    23.     }
    24.  
    25.     private void onInitializeFailed(Exception obj)
    26.     {
    27.         Debug.LogException(obj);      
    28.     }
    29.  
    30.     private void onInitializeSucceeded()
    31.     {
    32.         Debug.Log("Game Foundation is Successfully Initialized!");
    33.  
    34.         #region Inventory
    35.  
     

    Attached Files:

    mingz-unity likes this.
  4. antoine-agthe_unity

    antoine-agthe_unity

    Unity Technologies

    Joined:
    Jun 6, 2019
    Posts:
    7
    OK, I see.
    The problem comes from line 19.

    You are initializing your
    MemoryDataLayer
    explicitly with an
    Empty
    data.
    That means you want to have nothing in your profile.
    That is why you were having the warning log.
    GameFoundation finds no record for your currency, and initializes to
    0
    .

    But if you use this statement instead...
    Code (CSharp):
    1. var dataLayer = new MemoryDataLayer(); // default constructor
    ... then you should be OK.

    Behind the scene, the
    MemoryDataLayer
    asks the
    GameFoundationDatabase
    to create a default player data, including the initial allocations.

    The documentation is probably not very clear about using
    GameFoundationSerializableData.Empty
    .
    I put a pin on it.

    Now regarding the player profile, as you use the
    MemoryDataLayer
    , then you didn't miss anything.
    By player profile, I mean a
    GameFoundationSerializableData
    object.
    The
    MemoryDataLayer
    is initialized with this data, as it represents the data you want to manipulate.

    After a game session, if you want to save the profile, you can get the data object by calling
    MemoryDataLayer.GetData()
    .

    Then you can save this data the way you want.

    Now if you want this data save/load to be done by GameFoundation, consider using
    PersistenceDataLayer
    instead.
    You can initialize one by pointing a file in your system. It will save to this file by calling
    PersistenceDataLayer.Save()
    .
     
    mingz-unity likes this.
  5. Renea

    Renea

    Joined:
    Nov 12, 2017
    Posts:
    9
    Thanks for the clarification. This helps me understand the system better.