question

Tolin Simpson avatar image
Tolin Simpson asked

Read Custom Data from ItemInstance

I am getting the player's inventory then saving it as a list<ItemInstance> called "items" that I can reference in code and I want to get the Custom Data from an item in the players inventory so I try items[index].CustomData and from there I don't know how to get the values from the keys stored in the items Custom Data. Is this not stored in it's item instance? Also is there a way I can get the description written in the catalog from the item instance?

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.

Joshua Strunk avatar image Joshua Strunk commented ·

Details of the question from @leatherboundgames@gmail.com

My scenario is that each instance of an item in the player's inventory is unique with custom data values that are randomized in cloud script when granted to the user, I want to access those values to show in UI and pass to other scripts.

0 Likes 0 ·
Joshua Strunk avatar image
Joshua Strunk answered

Let me clear some things up.

CatalogItem.CustomData is of type String

ItemInstance.CustomData is of type Object<Key, Value> where Key and Value are both of type String



How do they relate?


Lets say our game is an RPG and we have an item called CommonGreatSword. All the CommonGreatSwords in our game deal the same damage and are two handed. This we would do through CatalogItem.CustomData because if we rebalance the damage for our CommonGreatSword we want all of the CommonGreatSwords in our game to do the new damage amount. We would define this item in the game manager like so


Now the information above represents a CatalogItem. The confusing part is that the CustomData here looks like its of type Object<Key,Value> but if you click EDIT AS STRING it will show you how it looks when you access it in code.

http://i.imgur.com/3O2ivyM.png

"{\"Damage\":\"2\",\"isTwoHanded\":\"true\"}". (\ is to show how the string would be typed out in code)

This is JSON encoded into a string and we must parse it if we want to access it in the same way we would ItemInstance.CustomData.

Now lets say we want players to have items be equipped or in their backpack and give every item a fun unique name. This we would do through ItemInstance.CustomData because it is different for every ItemInstance in our game. We want each instance of CommonGreatSword to define its position in our users inventory individually. You would define this data in the Game Manager like so

(This panel is opened by clicking the i icon next to an Item Id in the inventory of a player or character.)

The data defined here represents this object in code {"InventoryPosition":"Equipped", "Name":"Sword of the Lion Heart"}. This is an unencoded JSON object and we will not need to parse it to access the parts.

So lets get to some code in cloud script. First a disclamer I have not pushed and tested this script on PlayFab yet so possible typos and other errors. This script will get the InventoryPosition, Damage and Description for the first item in a users inventory.

https://gist.github.com/JoshuaStrunk/7ac993fc87bd8d3256ab2c280846995c



Some special notes now that you have seen how this works out in code. (both are why you might want to version your Catalogs)

  • If you change a CatalogItem's CustomData it WILL change for all the CatalogItem.CustomData for ALL of the items with the same ItemId in your player's inventories. As you can see from above CatalogItem and ItemInstance have no inherent link and changing one will not change the other.
  • If you change an ItemId or remove a CatalogItem from your Catalog you will NO LONGER be able to get the correct CatalogItem.CustomData for any of the ItemInstances which share the ItemId for the changed/removed CatalogItem

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

Dylan Hunt avatar image Dylan Hunt commented ·

This thread is quite epic

1 Like 1 ·
Joshua Strunk avatar image Joshua Strunk commented ·

Question from @leatherboundgames@gmail.com

Does the cloud script return the catalog value or the itemInstance value?

Can I parse the Json from the C# script on the clients side and return the customdata directly from there?

I guess I don't understand why we iterate through the catalog besides to get the description?

0 Likes 0 ·
Joshua Strunk avatar image Joshua Strunk Joshua Strunk commented ·

Does the cloud script return the catalog value or the itemInstance value?

The attached Cloud Script sample does not just return a value from the CatalogItem or the ItemInstance it returns values from both.

0 Likes 0 ·
Joshua Strunk avatar image Joshua Strunk Joshua Strunk commented ·

Can I parse the Json from the C# script on the clients side and return the customdata directly from there?

I am confused what your are referring to as "the Json" If you mean can you do what I did in Cloud Script but in client side C#, sure. You will need use a Json library to deserialize the Json in C#. I believe PlayFabSDKs have one included, but am not sure how to reference it.

0 Likes 0 ·
Tolin Simpson avatar image Tolin Simpson Joshua Strunk commented ·

Using PlayFab.Json;

but I am not sure if this lets you parse code the same way you did in cloudscript.

0 Likes 0 ·
Show more comments
Joshua Strunk avatar image Joshua Strunk Joshua Strunk commented ·

I guess I don't understand why we iterate through the catalog besides to get the description?


We iterate through the catalog to get the CatalogItem object for the ItemInstance object we have. We can then use both of these objects together to get information which is global to all items of the same ItemId and information that is specific to a single user's instance of that item.

0 Likes 0 ·
rainerlonau avatar image rainerlonau Joshua Strunk commented ·

I stumbled over this thread because I want to create a game where the players have, lets say TeamMembers in their squad.

So I could define for example four different CharacterClasses as Items in the Catalog. They have the some general values for that specific CharacterClass. Then wenn creating them, I could randomize some values for the specific ItemInstance. E.g. the name of the TeamMember, right?

1. My question now is how many of the CharacterClasses could I implement (I read 250 somewhere)?*

2. How many Instances could one player have?*

3. How many Instances in total?*

*in free version

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

CustomData is held as string data in JSON format. Find a JSON deserializer or parser and use that to convert the data held in CustomData into a dictionary.

You should be able to print "items[index].CustomData" to the console and read the output to understand what's held in there too.

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

glen avatar image glen commented ·

You might want to replicate catalog custom data to inventory item custom data as initial data per item instance. This data not automatically replicating is significant because of the server API call limit. Currently we must apply custom data per item instance as a separate API call rather than having specific catalog custom data fields replicate automatically when a new instance is created.

0 Likes 0 ·
brendan avatar image brendan glen commented ·

Correct - the system is designed to be used either way. For items that need custom data which should be the same for all instances, you can use the catalog item's CustomData. If those values are meant to be unique to each instance, you would copy it to the item upon creation. There's also a Feature Request on auto-initializing values here, if you feel that's an important addition: https://community.playfab.com/content/idea/895/210537287-Atomically-Creating-Items.html

0 Likes 0 ·
glen avatar image glen commented ·

That may actually be the case for Catalog CustomData only. Looks like CustomData for inventory items are key value pairs which means technically you should be able to get the values you're looking for by doing something like items[index].CustomData["keystring"]

-1 Like -1 ·
glen avatar image glen commented ·

Also remember that CustomData from catalog items does not automatically propagate to granted inventory items. You have to set CustomData for inventory items explicitly. I stumbled on that one before.

-1 Like -1 ·
Joshua Strunk avatar image Joshua Strunk glen commented ·

I am not sure what you mean by "automatically propagate", but you should not be replicating data from CatalogItem.CustomData down to ItemInstance.CustomData. This is unless you want the items data to be independent from balance changes, in which case there is no need to propagate the data.

0 Likes 0 ·
Tolin Simpson avatar image
Tolin Simpson answered

My scenario is that each instance of an item in the player's inventory is unique with custom data values that are randomized in cloud script when granted to the user, I want to access those values to show in UI and pass to other scripts.

Ok now just to make sure I understand, Joshua is saying if I change the custom data values in the catalog then the itemInstance will have the wrong values. I guess this doesn't matter since I override the values when the item is granted to the user.

Does the cloud script return the catalog value or the itemInstance value? Can I parse the Json from the C# script on the clients side and return the customdata directly from there, I guess I don't understand why we iterate through the catalog besides to get the description.

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

Joshua Strunk avatar image Joshua Strunk commented ·

You might want to keep discussion about Questions and Answers as comments under those specific Questions and Answers. The default post at the bottom of a thread is a new Answer which often times is not what you want to post.

0 Likes 0 ·
Joshua Strunk avatar image Joshua Strunk Joshua Strunk commented ·

I went ahead and tried to move parts of your reply to relevant areas of the thread I am not sure were you are responding too with "Ok now just to make sure I understand, Joshua is ...". So feel free to put that as a comment under wherever you are not understanding.

0 Likes 0 ·
Tolin Simpson avatar image Tolin Simpson Joshua Strunk commented ·

I tried to reply to the first answer but I seem to have made a new answer.

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.