question

Pat avatar image
Pat asked

CloudScript Help

I am trying to create my server function for UpdateUserInventoryItemCustomData and after looking at the documentation for cloud script I am lost. I understand I have to create a javascript file and upload it to the CloudScript revision in the game manager and that I have to run a GetCloudScriptUrl once in the client but besides that I am kind of lost.

I want to create two function one when the player obtains an item to set up some CustomData so it can be gotten when I update items. Another function would be when the character moves an item to another slot.

Any other help will be greatly appreciatively.

 
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 answered

What you would do is to create a JavaScript handler for each thing you want to do, put them in a file, and upload them to Cloud Script. You can use GitHub for this as well, if that's more convenient.

Once you've uploaded the handlers, you do need to call GetCloudScriptUrl in the client, in order to get the endpoint for the RunCloudScript call. We recommend saving that locally, so that you can use it for subsequent calls, as it's unlikely to change and you can re-query if you run into an error with the call.

Have you tried walking through the tutorial on using Cloud Script (https://api.playfab.com/docs/using-cloud-script/)? What are the specific issues that you're getting stuck on, using this?

10 |1200

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

Pat avatar image
Pat answered

Yeah I read the tutorial a couple of times. I guess I'm confused on how to actually call the UpdateUserInventoryItemCustomData function in the handler i created. Also how to add parameters for the function as well.

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 answered

We have a few examples of making calls in the Server API in our Cloud Script samples: https://github.com/PlayFab/CloudScriptSamples.

So the call to use UpdateUserInventoryItemCustomData might look something like this:

 

var serverData = server.UpdateUserInventoryItemCustomData ({
"PlayFabId": currentPlayerId,
"ItemInstanceId": args.instanceToUpdate,
"Data": dataObj
});

 

Where the data payload would be created as shown in this post: https://community.playfab.com/hc/en-us/community/posts/205460148-Cloud-Script-how-do-I-write-data-to-a-Key-which-is-defined-dynamically

10 |1200

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

Pat avatar image
Pat answered

Okay, just getting confused on the data part. The data would change depending on what item the character obtained or moved. For an example, the character looted a monster and got an item "Shirt". "Shirt" has a custom data on it called "Armor" with a value of 5. When the client tries to get that value in Armor it gives an error which you help me understand that the value wasn't there and said I should use the server function to do so. So I would create the data by saying

var dataObj = ["Armor", "5"];

But what happens if they obtained "Shirt 2" which has an "Armor" value of 10?

 

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 answered

First, you'll run into an issue with it defined that way. Since you'll probably need to have a variety of Keys for the data you're setting up, I would encourage using the script the way shown in the linked post, above:

    var keyString = "foo";
    var dataPayload = {};
    dataPayload[keyString] = "bar";

That way, you can set the Key to whatever you need programatically.

So, for your example, you would decide what values you want to set, and to what keys, based on your custom game logic. So, if ItemId "Shirt" should always be 5 and "Shirt2" should always be 10, you would set them that way. But if the items always have the same value, I would recommend just using the CustomData at the Catalog level, so that you can change it later and have that apply to all players. The item instance CustomData is really intended for data which is going to be unique to that instance - like for procedurally generated items, or items that "wear out" over time.

 

10 |1200

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

Pat avatar image
Pat answered

So things like Armor, Power, etc should be used on the custom data? But things like position in a bag I shouldn't be using it for. How should I go about doing the position of the item in the inventory then? How could I get the custom data for a tooltip or to increase stats on the character?

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 answered

To be clear, what I'm saying is that a value which should be the same for all instances of an ItemId, that value should be defined at the Catalog level, so that you can adjust it there if needed, and have that be picked up and used by all clients. For any value which is unique to the specific item instance, that should be stored in the Custom Data of the item instance itself.

So, the position of an item for an individual player is clearly unique to that player, and so should be stored on the item instance. For armor and power, it depends - is that value unique to that player's instance, or is it the same as all other instances of the item in the game (in which case you should store that data at the Catalog level)?

Tooltips are usually global, so again, I would recommend storing that (or a lookup value, if you have a separate localization file) in the Catalog.

For increasing statistics on a player (or character), presumably you mean due to having the item equipped? Again, is the value in question the same for all instances, or is it unique to the player's instance of the item? That's really the only deciding factor.

10 |1200

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

Pat avatar image
Pat answered

Things like armor and power would be the same for all instances and the only thing that would change from player to player would be the position of the item. So my question is how do I get the custom data in either instance?

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 answered

The CustomData at the Catalog level is in the CatalogItem returned from a call to https://api.playfab.com/Documentation/Client/method/GetCatalogItems.

For item instances, it's in the ItemInstance info returned from any inventory retrieval call, like https://api.playfab.com/Documentation/Client/method/GetUserInventory.

You set the CustomData in the Catalog via https://api.playfab.com/Documentation/Admin/method/SetCatalogItems or https://api.playfab.com/Documentation/Admin/method/UpdateCatalogItems.

And you can set the CustomData in the player's item instance in Cloud Script with https://api.playfab.com/Documentation/Server/method/UpdateUserInventoryItemCustomData.

10 |1200

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

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.