question

Kim Strasser avatar image
Kim Strasser asked

GetEntityToken is not working in Cloud Script

I need to get Entity.Id and Entity.Type in Cloud Script because I want to get the players profile language. But I get an error message when I call API authentication.GetEntityToken().

"Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "ReferenceError: authentication is not defined\n    at GetEntity (BFD0A-main.js:56:18)\n    at handlers.GetPlayerPreferredLanguage (BFD0A-main.js:21:26)\n    at Object.invokeFunction (Script:116:33)"
        }
handlers.GetPlayerPreferredLanguage = function (args, context)
{
    var getentitytoken = GetEntity();
    
    var currentplayerlanguage = "";
    
    if ((getentitytoken.EntityId != "") && (getentitytoken.EntityType != ""))
    {
        var entityKey = {
                    "Id" : getentitytoken.EntityId,
                    "Type" : getentitytoken.EntityType
                    };
    }
        
    if (entityKey != "")
    {
        var resultplayerprofile = entity.GetProfile({Entity: entityKey});
        if (resultplayerprofile != null)
            currentplayerlanguage = resultplayerprofile.Profile.Language;
        else
            log.info("Could not get current language.");
    }
    else
        log.info("Problem with entityKey");
            
    log.info(currentplayerlanguage);
}

function GetEntity()
{
    var entityid = "";
    var entitytype = "";
    var result = authentication.GetEntityToken();
        
    if ((result != null) && (result.Error == null))
    {
        entityid = result.Entity.Id;
        entitytype = result.Entity.Type;
    }
    else
        log.info("Could not get entitytoken.");
    
    return{EntityId: entityid, EntityType: entitytype};
}

How can I get Entity.Id and Entity.Type in Cloud Script?

CloudScript
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

·
Citrus Yan avatar image
Citrus Yan answered

You can use entity.GetEntityToken() to call GetEntityToken API in CloudScript. However, if you do use that to get the entity token, the result returned are actually your title’s entity token along with its EntityKey, which is not helpful for your case because I think what you really want is the player’s EntityKey instead. And, It makes no sense to get the player’s Entitykey when you need to provide its EntityKey for the GetEntityToken API to consume in the first place. From my perspective, you should consider implementing this feature from the client-side directly, Enitytoken and Entitykey are already returned from Login API calls for the GetProfile API to consume, and, if you accidently lost the EntityToken and EntityKey, you can always call GetEntityToken with SessionTicket to get them back.

3 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.

Kim Strasser avatar image Kim Strasser commented ·

What can I do if I have another cloud script function that is only triggered by a rule and if I want to send a push notification in the same language as the players profile language in this function?

What is the best way to get the players profile language in this case?

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

Why not set the Action to be "Send push notification" directly? PlayFab has the ability to send notification template with localized string, in other words, send notifications specific to each player's preferred languages. Please navigate to this tutorial to learn more:Push notification templates.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Citrus Yan commented ·

I store the profile language now in Player Data(for example: key: ProfileLanguage, value: de). I always update the Player Data key/value pair after the player called PlayFabProfilesAPI.SetProfileLanguage.

Then, I can easily get the Player Data key/value pair in Cloud Script to determine the preferred language for the push notifications.

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.