question

joeypc6 avatar image
joeypc6 asked

How To create Inventory itself?

so i got this code

    static void OnLogin(LoginResult result)
    {
        LoginData.Instance.SaveUser();
        Debug.Log("Loged in with: " + result.PlayFabId);
        GetUserInventory();
        UIThings.Instance.LoginRect.SetActive(false);


        //Join Server List
    }





    public static void GetUserInventory()
    {
        PlayFabClientAPI.GetUserInventory(new GetUserInventoryRequest(), UserInventoryRe        sult, OnAPIError);
    }


     public static void UserInventoryResult(GetUserInventoryResult result)
    {
        Debug.Log(result.Inventory);

       //Get The Player's Inventory

      
    }


can someone give me a simple start

so i get this response

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{
  "code": 200,
  "status": "OK",
  "data": {
    "Inventory": [
      {
        "ItemId": "IronOre",
        "ItemInstanceId": "9879843",
        "ItemClass": "Items",
        "CatalogVersion": "5",
        "UnitPrice": 0
      },
      {
        "ItemId": "GoldOre",
        "ItemInstanceId": "3854637",
        "ItemClass": "Items",
        "CatalogVersion": "5",
        "UnitPrice": 0
      }
    ],
    "VirtualCurrency": {
      "GC": 100
    },
    "VirtualCurrencyRechargeTimes": {
      "GC": {
        "SecondsToRecharge": 500,
        "RechargeTime": "Date",
        "RechargeMax": 50
      }
    }
  }
}

what to do ?

apissdksPlayer Inventory
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.

joeypc6 avatar image joeypc6 commented ·

here is a small example of what i want to do it's 0%

https://www.dropbox.com/s/4qizxeponkl4sg5/Testing.rar?dl=0

i have a build system but i need to save it in player's inventory so is it possible to get 2 invs ?

i will save it when it changed and it won't changed frequently

0 Likes 0 ·
joeypc6 avatar image joeypc6 joeypc6 commented ·

here is my goal

i want to make inventory and use the inventory to build somethings in the grid

so can you please give me a simple example of inventory because i don't get GetUserInventoryResult.Inventory how do i convert it into a list

0 Likes 0 ·
brendan avatar image
brendan answered

Rather than make assumptions about your design, could you provide a clear write-up of what it is you're trying to accomplish? What you've listed above is the code to read the player inventory, and it looks like it's working fine for you. If you want to read another player's inventory, you would do that using the Server API, via a dedicated server (custom game server) or Cloud Script. To add things to the player inventory, you would either use a Grant... API call (also via dedicated server or Cloud Script) or else sell it to the player for a non-zero amount of virtual currency or real-world money.

4 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.

joeypc6 avatar image joeypc6 commented ·

i want to convert this

  1. HTTP/1.1200 OK
  2. Content-Type: application/json; charset=utf-8
  3. {
  4. "code":200,
  5. "status":"OK",
  6. "data":{
  7. "Inventory":[
  8. {
  9. "ItemId":"IronOre",
  10. "ItemInstanceId":"9879843",
  11. "ItemClass":"Items",
  12. "CatalogVersion":"5",
  13. "UnitPrice":0
  14. },
  15. {
  16. "ItemId":"GoldOre",
  17. "ItemInstanceId":"3854637",
  18. "ItemClass":"Items",
  19. "CatalogVersion":"5",
  20. "UnitPrice":0
  21. }
  22. ],
  23. "VirtualCurrency":{
  24. "GC":100
  25. },
  26. "VirtualCurrencyRechargeTimes":{
  27. "GC":{
  28. "SecondsToRecharge":500,
  29. "RechargeTime":"Date",
  30. "RechargeMax":50
  31. }
  32. }
  33. }
  34. }

into this

public class ItemInstance : MonoBehaviour {
    [SerializeField]
    public string ItemId;
    public string ItemClass;
    public int UnitPrice;


    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

0 Likes 0 ·
brendan avatar image brendan joeypc6 commented ·

Inventory[] itself is a list, so you could simply iterate across it using a for loop and copy the information you need.

0 Likes 0 ·
joeypc6 avatar image joeypc6 brendan commented ·

any example because we couldn't understand the forms

we are willing to get any info

pls give me an example

i want make health potion

can you give me an example plese

0 Likes 0 ·
Show more comments
joeypc6 avatar image
joeypc6 answered

So i got An new problem to deal with here is what i'm getting

 [SerializeField]
    private List<ItemIs> itemss;
    public static List<ItemIs> Itemss
    {
        get { return Instance.itemss; }
        set { Instance.itemss = value; }
    }


     public static void GetUserInventory()
    {
        PlayFabClientAPI.GetUserInventory(new GetUserInventoryRequest(), UserInventoryRe        sult, OnAPIError);
    }





 public static void UserInventoryResult(GetUserInventoryResult result)
    {
        int i = 0;
        string messageValue;


        foreach (PlayFab.ClientModels.ItemInstance item in result.Inventory)
        {

            //Add Item To The List
            Itemss.Add(new ItemIs());
            //Add Item Id to The List
            Itemss[i].Index = item.ItemId;
            //Add Name To the List
            Itemss[i].Name = item.DisplayName;

// -------------------------------  The Error ------------------------------
            if(item.CustomData.TryGetValue("Cost", out messageValue))
            {
                Debug.Log(messageValue);
            }
           
            // Go To The Next Item
            i += 1;
        }


    }
7 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.

brendan avatar image brendan commented ·

Can you clarify this? What is the specific error you're getting? That "Cost" isn't in your item's CustomData? How are you writing it to the CustomData for the inventory item instance?

0 Likes 0 ·
joeypc6 avatar image joeypc6 commented ·

I get This Error

NullReferenceException: Object reference not set to an instance of an object
PlayfabLogin.UserInventoryResult (PlayFab.ClientModels.GetUserInventoryResult result) (at Assets/GameAssets/Scripts/Menu/PlayFabb/PlayfabLogin.cs:177)
PlayFab.Internal.PlayFabHttp+<MakeApiCall>c__AnonStorey0`1[PlayFab.ClientModels.GetUserInventoryResult].<>m__1 () (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:183)
PlayFab.Internal.PlayFabWww+<MakeApiCall>c__AnonStorey1.<>m__0 (System.String response) (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabWWW.cs:98)



i got this customdata

{"Prefab":"Iron","Icon":"IronOre","Cost":"7"}

and i'm trying to get this custom data , what i'm doing wrong ?

0 Likes 0 ·
joeypc6 avatar image joeypc6 joeypc6 commented ·

it's item instance not catalogeitem

0 Likes 0 ·
brendan avatar image brendan joeypc6 commented ·

Can you give me a specific PlayFab ID for a user with the item in question, and the Item Instance ID for the item? The only way the data KVP wouldn't be in the custom data for the item instance is if the attempt to update the item instance with that data failed in some way.

0 Likes 0 ·
Show more comments
joeypc6 avatar image
joeypc6 answered

I Got Taken In-Depth

I have This Inventory Menu it Works

    private static PlayfabLogin instance;
    public static PlayfabLogin Instance
    {
        get { return instance; }
        set { instance = value; }
    }
   


   [SerializeField]
    private List<PlayFab.ClientModels.ItemInstance> inventory;
    public static List<PlayFab.ClientModels.ItemInstance> Invenotry
    {
        get { return Instance.inventory; }
        set { Instance.inventory = value; }
    }


   public static void GetUserInventory()
    {

        PlayFabClientAPI.GetUserInventory(new GetUserInventoryRequest(), UserInventoryRe        sult, OnAPIError);
        
    }


    public static void UserInventoryResult(GetUserInventoryResult result)
    {


        Invenotry = result.Inventory;

    }


And Here How Do i Display It

//You Call Clear First
private void UpdateInventoryInfo()
    {
        for (int i = 0; i < PlayfabLogin.Invenotry.Count; i++)
        {


          GameObject item = Instantiate(Resources.Load("Item") as GameObject);
          item.transform.SetParent(inventoryContents);

          item.transform.GetComponent<ItemSlot>().name = PlayfabLogin.Invenotry[i].Di          splayName;
          item.transform.GetComponent<ItemSlot>().stacks = "x" + PlayfabLogin.Invenot          ry[i].RemainingUses;


            


        }
    }


    public void ClearInventory()
    {
        Debug.Log("Cleared");
        if (PhotonNetwork.connected)
        {
            if (inventoryContents != null)
            {
                


                    for (int i = 0; i < inventoryContents.childCount; i++)
                    {
                        Destroy(inventoryContents.GetChild(i).gameObject);
                    }
                    
                
            }


        }


        UpdateInventoryInfo();
    }

ItemSlot(Edit-1)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemSlot : MonoBehaviour {
    public Sprite icon;
    public string name;
    public string stacks;


	// Use this for initialization
	void Start () {
        transform.GetChild(1).GetComponent<Image>().sprite = icon;
        transform.GetChild(2).GetComponent<Text>().text = name;
       transform.GetChild(3).GetComponent<Text>().text = "x" + stacks;
    }


}


Crafting(semi good)

-Cloud Script (Edit-1)

function CraftItem_internal(CraftingMarital , SecondCraftingMarital ,ItemGiveId) {
    var inventory = server.GetUserInventory({ PlayFabId: currentPlayerId });
    var FirstitemInstance = null;
    for (var i = 0; i < inventory.Inventory.length; i++) {
        if (inventory.Inventory[i].ItemInstanceId === CraftingMarital)
            FirstitemInstance = inventory.Inventory[i];
    }
    if (!FirstitemInstance)
        throw "Item instance not found";
  
    var SecondtemInstance = null;
    for (var i = 0; i < inventory.Inventory.length; i++) {
        if (inventory.Inventory[i].ItemInstanceId === SecondCraftingMarital)
            SecondtemInstance = inventory.Inventory[i];
    }
    if (!SecondtemInstance)
        throw "Item instance not found";
 if(FirstitemInstance != null && SecondtemInstance != null){
   server.ConsumeItem({ PlayFabId: currentPlayerId, ItemInstanceId: CraftingMarital , ConsumeCount: 1 });
   server.ConsumeItem({ PlayFabId: currentPlayerId, ItemInstanceId: SecondCraftingMarital , ConsumeCount: 1 });
   server.GrantItemsToUser({ PlayFabId: currentPlayerId, ItemIds: ItemGiveId });
   log.info("Item Given!")
 }
   
}


handlers.CraftItem = function (args){
  if (!args || !args.CraftingMarital || !args.SecondCraftingMarital || !args.ItemGiveId)
        throw "Invalid input parameters, expected soldItemInstanceId and requestedVcType";
     log.info("On Craft Got Called!");
    CraftItem_internal(args.CraftingMarital , args.SecondCraftingMarital ,args.ItemGiveId);
  
}

So Now I Want To Do Crafting OverTime

(You Click On Button In Game For Crafting and Get Timer That Update Onlogin)

(RealTime Timer Not In Game Like Clash Of Clans Timers)

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.

brendan avatar image brendan commented ·

For a crafting timer, the simplest thing to do would be to post the information on crafting operations started by the player, with their start timestamp, in player data. That way, you can read that from player data (just put all the crafting attempts in a single key/value pair) and use the start time to determine if the player is done (in case you want to provide ways to accelerate the time - that way, you're calculating from the starting point).

0 Likes 0 ·

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.