Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Basic functions, finding the code

Discussion in 'Economy' started by Corva-Nocta, Nov 6, 2022.

  1. Corva-Nocta


    Feb 7, 2013
    Hey all! I'm very excited for all the new multi-player components officially released by Unity, trying to dive in! Having a little bit of trouble so I'm trying to get some guidance, feeling very newbie right now haha.

    Previously I have worked with php mysql database stuff to store things like a player's inventory or equipped items (I used this series as the initial framework , I'm trying to get the same thing working with the economy system but I'm hitting a bit of a road block. Honestly I'm just having a hard time finding the basics. Before when dealing with the database I would just be making a www form and passing info into the form, relatively simple stuff. But with the economy system I can't find the actual code just to save an item to a player.

    I think I may just be digging around in the wrong place, I've been checking the documentation but I'm not finding any actual code. Am I just looking in the wrong place? I'm feeling a little lost on this one.

    What is the code just to add an item (well just the itemID of an item, just an int) to the player? It seems like the economy system uses something similar to the php/mysql system where you make a form and then fill it with data you pass to the code, is that accurate?

    Any help at all would be appreciated!
  2. Corva-Nocta


    Feb 7, 2013
    I found this page: which seems to be what I am looking for, now I just need to figure out how to implement it properly! Its still a bit confusing with what is there in the docs, but I think I'm getting it. Still hoping there is a more step by step system somewhere to help out!

    This also helps as a bit of a path to follow: so I might try doing a currency first instead.

    Oh Wait! Just found this: which I think will get me what I need!
  3. Laurie-Unity


    Unity Technologies

    Mar 5, 2020
    Hi @Corva-Nocta

    So, as you have discovered from the various documentation links, there are different ways you can interact with the Economy system, depending on your specific use case. This also applies to getting items and performing currency operations.

    The different links you have found apply to different use cases:
    • Client SDK - If you want your client side game code to interact with the Economy system, you would use the methods in the Economy SDK directly in your client side game code.

    • Sever Side Cloud Code - There may be occasions when you want to access the player's inventory or currency from Cloud Code for a more secure server initiated interaction with Economy

    • REST API - You can access the player's Economy from your own server tools or game back end with the REST API methods

    • Game Overrides - Can be used to automate operations against Economy. e.g. Gift all players who played on 31st October a Halloween cosmetic hat.
    Hopefully those mechanisms cover most of your use cases, but do let us know if there are use cases you are struggling with? We are also hard at work on some additional exciting capabilities that we should be able to share with you before too long.
  4. Corva-Nocta


    Feb 7, 2013
    There are certainly a lot of different places to find different information, its a bit confusing haha. But between the documents and the sample projects I am very slowly starting to get the code.

    Thanks for the reply!

    Right now I am just trying to keep things simple, so just trying to add items to the inventory from my server build. I am guessing that would be the SDK code that I should look at. Only trouble I've had with that is if I try copy-pasting the code it throws up all kinds of errors, so I am not really sure how to actually use the code from that section.

    I did use the code from the samples to find this:
    Code (csharp):
    1. public async Task RefreshInventory()
    2.     {
    3.         GetInventoryResult inventoryResult = null;
    4.         try
    5.         {
    6.             inventoryResult = await GetEconomyPlayerInventory();
    7.             Debug.Log("Got Inventory!");
    8.             Debug.Log(inventoryResult);
    9.         }
    10.         catch (EconomyRateLimitedException e)
    11.         {
    12.             //inventoryResult = await Utils.RetryEconomyFunction(GetEconomyPlayerInventory, e.RetryAfter);
    13.         }
    14.         catch (Exception e)
    15.         {
    16.             Debug.Log("Problem getting Economy inventory items:");
    17.             Debug.LogException(e);
    18.         }
    19.         if (this == null)
    20.             return;
    21.         //inventoryHudView.Refresh(inventoryResult.PlayersInventoryItems);
    22.     }
    23.     static Task<GetInventoryResult> GetEconomyPlayerInventory()
    24.     {
    25.         var options = new GetInventoryOptions { ItemsPerFetch = 10 };
    26.         return EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);
    27.     }
    which is working just fine right now. Seems to be perfect in fact! I thought maybe I could use the same code to add an item rather than just refresh the inventory. But I a already getting some errors and I am not really sure how to fix it. So I am not really sure how to add an item.

    I know eventually I will want to use cloud code, but looking at what's there and cloud code seems to be WAY over my head for right now. So I'll come back to that later. I think I will need a step by step walkthrough on those, looking at the fully finished code isn't helping too much right now.

    For some suggestions though, can't say I have any major feature requests right now but I do know I will eventually want to save things like what items are equipped on my player. So a system just like the inventory but for equipment will be nice to have :D
  5. Laurie-Unity


    Unity Technologies

    Mar 5, 2020
    Hi There,

    SDK method can be used to add items to a player's inventory.

    Code (CSharp):
    1. Dictionary<string, object> instanceData = new Dictionary<string, object>
    2. {
    3.     { "rarity", "purple" }
    4. };
    6. AddInventoryItemOptions options = new AddInventoryItemOptions
    7. {
    8.     PlayersInventoryItemId = "customID",
    9.     InstanceData = instanceData
    10. };
    12. PlayersInventoryItem createdInventoryItem = await EconomyService.Instance.PlayerInventory.AddInventoryItemAsync("SWORD", options);
    I'm not entirely clear on your specific use case, but is there a reason that you wouldn't use the Inventory system for recording Equipment too? You can add additional data to the
    on the item to infrom your client side code to handle this type of item differently.
  6. Corva-Nocta


    Feb 7, 2013
    I think I'm getting a better hand on the system, so far it's been working well to add the items :D

    As for equipment, it's more about what I'm used to haha. I'm used to a database system where you can partition any information you want into any groups (tables) you want. So in those systems I would have the list of my inventory items in a completely separate table from the table of what is equipped. (And could make a separate table for things like abilities, skills, exp, etc) it makes it very easy to store info, and retrieve info. If I just need to check what is equipped I can just pull the list of 10 equip slots, (1 for head, 1 for chest, etc) rather than having to pull a massive list of items and check through all of them to find which one is equipped. And for storing the items all I would need is to store the ID if the item (I just used an int)

    I think I just need to shift my thinking of how they would be set up with the new UGS. I'm guessing I can just add some custom data to an item like a book for if the item is equipped or not. That should work just fine, I just need to adjust how I think about storing data.

    But if you're looking for a list of things to save as a feature request, the ability to create your own tables would be nice. :D That way you can just make a new table for equipped items, and a new table for skills, a new table for upgrade unlocks, a new table for exp, etc.
    Last edited: Nov 21, 2022