docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Game Foundation Tutorials

    Playing with currencies at runtime

    We've created a currency in the previous tutorial. Let's see how to play with this new item.

    For this tutorial, please follow the initialization steps you can find in the inventory tutorial.

    Getting the currency at runtime

    With your script created, go to the OnInitSucceeded method, and append the following code

    void OnInitSucceeded()
    {
        Debug.Log("Game Foundation is successfully initialized");
    
        // Use the ID you've used in the previous tutorial.
        const string definitionId = "myFirstCurrency";
    
        // The currencies are available in the
        // currencyCatalog of the database.
        var catalog = GameFoundation.catalogs.currencyCatalog;
    
        // Finding a currency takes a non-null string parameter,
        // but it can fail to find the definition.
        var definition = catalog.FindItem(definitionId);
    
        if (definition is null)
        {
            Debug.Log($"Definition {definitionId} not found");
            return;
        }
    
        Debug.Log($"Definition {definition.id} ({definition.type}) '{definition.displayName}' found.");
    }
    

    Compile and start your scene. You should see the following log entry in your console:

    ! Definition myFirstCurrency (Soft) 'My First Currency' found.
    

    The API is very similar to the one described in the inventory item tutorial.

    Getting the balance of a currency

    We now have a valid definition of a currency. What I want to know now is the amount of this currency the player owns. Go back to your OnInitSucceeded method and append the following lines:

    // You can get the balance of a currency
    // with the WalletManager.
    var balance = WalletManager.GetBalance(definition);
    
    Debug.Log($"The balance of '{definition.displayName}' is {balance}");
    

    Compile and start your scene. You should see the following log entry in your console:

    ! The balance of 'My First Currency' is xxx
    

    xxx is the value you've set for the initial allocation.

    Changing the balance

    Getting the balance is fine, but now I want to play with it.

    The WalletManager exposes a simple API to change the balance of a currency. We'll see here the most straighforward method of its API.
    Go back to your OnInitSucceeded method and append the following lines:

    // SetBalance replaces the current balance by a new one.
    var success = WalletManager.SetBalance(definition, 50);
    
    if (!success)
    {
        Debug.LogError($"Failed in setting a new value for '{definition.displayName}'");
        return;
    }
    
    var newBalance = WalletManager.GetBalance(definition);
    
    Debug.Log($"The new balance of '{definition.displayName}' is {newBalance}");
    

    Compile and start your scene. You should see a new log entry:

    The new balance of 'My First Currency' is 50
    

    The WalletManager also exposes methods to add and subtract an amount from the balance.

    Conclusion

    The WalletManager is a very simple API, but a lot of games, especially on mobile, can limit their usage to this one.

    Going forward

    Debug.Log-ging your balance and items may be a bit intrusive in your code. You may need a better way to monitor the player profile, and that's what we'll see in the next tutorial.

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)