question

Pat avatar image
Pat asked

GrantItemToCharacter help

I am trying to grant an item to a character and I have two questions. How do I pass in the itemid and how can I get the itemInstance for the item being granted?

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 ItemIds for the items you want to grant to the character are specified as an array in the https://api.playfab.com/Documentation/Server/method/GrantItemsToCharacter call (ItemIds). When the items are granted, the return value from the call contains a https://api.playfab.com/Documentation/Server/datatype/PlayFab.Server.Models/PlayFab.Server.Models.GrantedItemInstance array for the items added, which includes the item Instance IDs.

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

Ok, so I am just trying to a single item and this is my code for the server call.

handlers.GrantItem = function (args)
{
var charaId = "";
var itemId = "";
var playfabId = "";

charaId = args.data.CharaId;
itemId = args.data.ItemId;
playfabId = args.data.PlayFabId;

var itemToGrant = {};
itemToGrant[0] = itemId;

var GrantItemsRequest = server.GrantItemsToCharacter(
{
CharacterId : charaId,
PlayFabId : playfabId,
ItemIds : itemToGrant
});

return GrantItemsRequest.ItemGrantResults;
}

I am getting an error Error Message: PlayFabAPIError InvalidRequest: Invalid JSON in request and it points me to this line: var GrantItemsRequest = server.GrantItemsToCharacter

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 I fixed the server call now I am having the issue the return data. In the samples it has it as List<ItemInstance> grantedItems = (List<ItemInstance>)result.FunctionResult; but I do not FunctionResult as a method to be called I have tried with just Results but I am getting an error: Error Message: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidCastException: Cannot cast from source type to destination type.

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

It sounds like you're probably still using RunCloudScript, yes? You'll want to update to use ExecuteCloudScript, as we've deprecated RunCloudScript, at this point. It will continue to work for previously-shipped titles, but the newer API method is a significant improvement (GetCloudScriptUrl is also no longer needed). The docs for ExecuteCloudScript are here:

https://api.playfab.com/Documentation/Client/method/ExecuteCloudScript

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.