question

pleasehelpme avatar image
pleasehelpme asked

How to specify a specific array item?

When I call on a Player Data, Title Data, User Inventory, etc. It lists it in an array from top to bottom from what is shown in Playfab.

How would I go about, for example your inventory consists of: Sword, Shield, Spear. In the array; 0=Sword,, 1=Shield and so on. But if you don't buy it in the order of Spear, Shiel and then sword. Then the order of the array is all messed up. So calling a 0 element from the array could be Shield or Spear depending on which item was bought last.

How would I specify the specific item I want to target when pulling my inventory/etc. from the array after calling the API?

Thank you.

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

·
Andy avatar image
Andy answered

I'm not entirely sure I follow your question. Since you'll never know the precise order of items in the returned collection, you'll need to iterate over the array and evaluate each item to determine whether it's the one you're looking for. There's metadata like ItemClass as well as potentially CustomData to help you make that decision. That's all done through client side (or Cloud Script) code.

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.

pleasehelpme avatar image pleasehelpme commented ·

I will try to be as detailed as possible. So when I call GetUserInventory, I have the results of Inventory put into an Array Variable. The array is "size" is automatically adjusted to the amount of Inventory item the user has obtained/bought and the "element" is the list of items.

So for my above example of Sword, Shield & Spear. The Array would automatically adjust to a size of 3 with Elements 0, 1 and 2.

To quote you,

Since you'll never know the precise order of items in the returned collection, you'll need to iterate over the array and evaluate each item to determine whether it's the one you're looking for.

This appears to be my problem. Once the Inventory is gathered into the Array Variable. The only way for me to check for a specific item would be to repetitively check Element 0, then pull out the DisplayName into a string variable.

Then using { If StringVar != "DisplayName"} then I would have to then call on Element 1, then Element 2, etc. Until I can find the right DisplayName for the 1 item I'm looking for. But this is quite repetitive to try and find any 1 item when you have many items.

Since each user will have a different Array Variable depending on when they buy each item.

0 Likes 0 ·
Andy avatar image Andy ♦♦ pleasehelpme commented ·

Are you asking for an efficient way to find an element in an array? Most languages support some sort of find operation that allows you to specify a "query". For instance, JavaScript supports array.find(), which is discussed more here: https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects

The specifics of your solution will depend on what language you're working in.

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.