question

Niall Muldoon avatar image
Niall Muldoon asked

TypeScript - IPlayFabContext.currentEntity missing

Hi,

I am in the process of porting my CloudScript file to TypeScript to reap the many benefits this offers, I followed the tutorial: https://blog.playfab.com/blog/typescript2

I now have an an error in "makeEntityAPICall" accessing context.currentEntity: "Property 'currentEntity' does not exist on type 'IPlayFabContext'.ts(2339)". This function is part of the default cloud script for a new PlayFab project.

I thought that maybe I would just copy this file - https://github.com/PlayFab/SdkTestingCloudScript/blob/master/DefaultCloudScript.ts and add my own custom functions, but noticed it was incomplete. Is it possible to get this updated so it matches what a new project is given?

Entity access through TypeScript is of particular importance for us as we are using the player entity file system for cloud saves on a mobile title. I will need to be able to write these files from TypeScript so I can do cheat detection of the save files prior to final upload.

Thank you,

Niall

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

·
Hernando avatar image
Hernando answered

If you want to call Entity API in CloudScript, you can retrieve the entity of the current user with GetUserAccountInfo method like the following

let entitykey:PlayFabServerModels.EntityKey = server.GetUserAccountInfo({PlayFabId:context.playerProfile.PlayerId}).UserInfo.TitleInfo.TitlePlayerAccount;

Then, this code demonstrates how to update objects for the player.

  let request : PlayFabDataModels.SetObjectsRequest={
    Entity: entitykey,
    Objects:[{
      ObjectName:"PlayerData",
      DataObject:{"LV": Math.floor(Math.random()*10)+1,
                  "Exp": Math.floor(Math.random()*1000)+1,
                  "EquipedWeapon": "Gun"}
    }
    ]
  }
  entity.SetObjects(request)

As for the DefaultCloudScript.ts in GitHub, it is only used to demonstrate part of the functionality of CloudScript and not for all game. So I don't recommend you to use it as a template and add your own custom functions.

Besides, If you still have any questions about Cloudscript, feel free to ask questions in the forum.

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.

Niall Muldoon avatar image Niall Muldoon commented ·

Hi Hernando,

found that code after posting in another thread, thank you for confirming this as best practice.

That said, "context.currentEntity" is used in the default CloudScript that PlayFab gives new projects. This thread also references the function being given to new titles: https://community.playfab.com/questions/23513/group-api-with-cloudscript.html

handlers.makeEntityAPICall = function (args, context) {
    var entityProfile = context.currentEntity;

    var apiResult = entity.SetObjects({
        Entity: entityProfile.Entity,
        Objects: [
            {
                ObjectName: "obj1",
                DataObject: {
                    foo: "some server computed value",
                    prop1: args.prop1
                }
            }
        ]
    });


    return {
        profile: entityProfile,
        setResult: apiResult.SetResults[0].SetResult
    };
};

If this works in CloudScript it should also work in TypeScript.

What exactly does "context.currentEntity" represent? I had to strip the comments from the function above for space, but it mentions "X-EntityToken".

Thank you,

Niall

0 Likes 0 ·
Niall Muldoon avatar image Niall Muldoon commented ·

Hi,

On taking a second look at the "makeEntityAPICall" function comments, it links to this: https://api.playfab.com/documentation/CloudScript/method/ExecuteEntityCloudScript

It seems as though Instead of calling "ExecuteCloudScript" (as I am currently doing), I could call "ExecuteEntityCloudScript" and provide the entity I want to work on. In my case I could just pass the "title_player_account" entity.

So I guess I've come back around to wanting to be able to access "context.CurrentEntity" in TypeScript. I'm guessing that the typings need to be updated here in some way to reflect the current SDK?

Thank you,

Niall

0 Likes 0 ·
Hernando avatar image Hernando Niall Muldoon commented ·

Yes, the currentEntity property will only be included in the entity parameter context of the function called via ExecuteEntityCloudScript.

If you insist on accessing "context.CurrentEntity" you can modify the cloudscript.t.ds file according to your needs with the following code(This is not the complete code, just provide a train of thought):

interface IPlayFabHandlers {
  [handlerId:string]: (args?:any, context?:IPlayFabContext | IPlayFabEntityContext) => any;
}

interface IPlayFabEntityContext {
    currentEntity: PlayFabProfilesModels.EntityProfileBody;
    playStreamEvent: PlayStreamModels.IBasePlayStreamEvent;
}

And you should set type for function parameters when adding a function to the handler, as shown below:

handlers.SetupPlayerData = function (args:any,context: IPlayFabEntityContext
) {...}

In addition, since it is a public repository, we welcome everyone to send a pull request. And we will inform the relevant team to review.

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.