question

Dylan Hunt avatar image
Dylan Hunt asked

How to find item tag in C# (Unity)?

In an ItemInstance, where can I find the tag(s) in C# with Unity?

(Sorry about so many posts recently haha I've been on a PF rampage!)

EDIT: Forgot img

apis
where-tag.png (7.5 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Joshua Strunk avatar image
Joshua Strunk answered

The problem your having is that you are trying to access a CatalogItem property with a ItemInstance object.

So long story short to get the tags for a ItemInstance you must search through your catalog of items for the matching item. ItemInstance.ItemId == CatalogItem.ItemId. From there you can use CatalogItem object and get the tags by doing CatalogItem.Tags.

Please see this post for more info on the topic of mapping ItemInstance objects to CatalogItem objects.

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.

Dylan Hunt avatar image Dylan Hunt commented ·

This is the answer - cheers! ..But it (the API/SDK) is definitely not intuitive if this is still the main way to do it. To save time, a lazy workaround would be to store tags in annotation and just split() with commas. Would also save processing if you don't have to iterate.

This really needs to be changed or everyone will have different ways of doing the core functions instead of having a standard like most other calls

EDIT: Added feat req https://community.playfab.com/idea/5738/make-items-more-intuitive-in-terms-of-accessing-ce.html

EDIT 2: @Joshua Strunk I just saw your comment - I think I see what you're saying - I'll try it out.

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

In this situation I always recommend when you pull a catalog from PlayFab just instantly build a dictionary. Dictionary<string, CatalogItem> catalog. This is instead of storing it as returned by the API as a List<CatalogItem>. You can still iterate through all the items in dictionary if needed and this allows you to quickly map ItemInstances to CatalogItems by doing catalog[itemInstance.ItemId].

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

Oh, actually, I'm getting the items from loginResult.InfoResultPayload.UserInventory so I don't actually have access to the catalog unless I call it separately. But using payload is kind of the reason to not call it separately in the first place to have it all at once, or the point is lost.

I'm still newish at PF so maybe there's another way, but I'm out of ideas.

EDIT: So I'm still not sure about the correct way to do this -- the answer seems that I actually need to make a 2nd call to GetCatalogItems - is this correct? Meaning, I can't resolve this by only the LoginResult type (via the info result payload.userinventory)?

EDIT 3: I suppose most catalogs won't change during a game session .. maybe I'm going about this wrong. Instead of grabbing it each time, should I grab it at the very beginning of the game and no need to check again? Maybe as soon as a successful semi-anon device ID login (same time I grab the news)?

0 Likes 0 ·
frog-cry.png (18.6 KiB)
Dylan Hunt avatar image Dylan Hunt Joshua Strunk commented ·
            // Save in dictionary for more convenient references to find item info
            catalogDict = new Dictionary<string, CatalogItem>();
            foreach (CatalogItem item in catalog.Catalog)
            {
                catalogDict.Add(item.ItemId, item);
            }

Something like this?

0 Likes 0 ·
Dylan Hunt avatar image Dylan Hunt Dylan Hunt commented ·

Woot I got it! Thanks! After putting into dictionary:

List<string> tags = catalogDict[item.ItemId].Tags;
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.