emptrixx avatar image
emptrixx asked

GrantCharacterToUser and ListAllUsersCharacters

So I'm new to PlayFab and am at a loss since there are no youtube video examples on how exactly to code and troubleshoot issues with CloudScript and the newer "Functions" feature.

I would like to know what's the best way to test run this code? In Unity, normally I'd know if the code has any syntax error or missing libraries but in CloudScript there's no warning if I'm trying to access something that doesn't exist.

Also is this roughly the correct way to have the server create Grant a Character to the user?

I not could anyone advice and show some sample code and kindly indicate if the code is meant to be in Unity or Cloudscript. From what I know, all "Server" logic code should be in cloudscript for a game that does not have a dedicated server.

Also is this the correct way to get the PlayFab to list the amount of Characters the Player has? I used this in Unity end with the ClientAPI and it works, but I just can't understand and don't have a clue how to go about the server side code in cloudscript without a proper editor/compiler to correct my syntax etc.

handlers.GrantCharacterToUser = function (args) {
    var listUsersCharactersRequest = {
            PlayFabId: currentPlayerId


    if (listUsersCharactersRequest.length != 0)
        return { messageValue: "A Character already exist for this Player." };

    var request = {
        PlayFabId: currentPlayerId, Character: [{
                CharacterName: args.characterName,
                CharacterID: 1,
                PlayFabID: currentPlayerId
    // The pre-defined "server" object has functions corresponding to each PlayFab server API 
    // ( It is automatically 
    // authenticated as your title and handles all communication with 
    // the PlayFab API, so you don't have to write extra code to issue HTTP requests. 
    var grantCharacterToUserRequest = server.GrantCharacterToUser(request);

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

Gosen Gao avatar image
Gosen Gao answered

We recommend that you use an Azure Function, which provides features such as syntax detection and autocompletion during implementation.

For creating characters, it is usually more recommend to do it on the server side. Here is the code about listing all characters and granting new character for your reference. For more info about writing Cloud Script, please refer to Writing custom CloudScript (Legacy) - PlayFab | Microsoft Docs.

handlers.Samples= function (args)
    //the result of GetAllUsersCharacters
    let allCharacters = server.GetAllUsersCharacters({
        PlayFabId: currentPlayerId
    //the result of GrantCharacterToUser
    let grantCharacter = server.GrantCharacterToUser({
        PlayFabId: currentPlayerId,
        CharacterName: "MyCharacter",
        CharacterType: "Lmo"
    //return both results to the caller
    return {
        "allCharacters": allCharacters,
        "grantCharacter": grantCharacter

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.