question

Martin avatar image
Martin asked

Is it possible for the Key to be a variable and change according to the passed argument?

Hi everyone!

I recently got my hands on PlayFab in order to use it in my Unreal Engine project. Unfortunately, I ran into a problem when working with Cloud Script - Revisions(Legacy) and I can't figure it out.

In particular, it is about updating/creating player data.
If I decide to use PlayFab's plugin for Unreal Engine and its UpdateUserData function, I am able to insert a JSON object in Key:Value format (for example "Games" : "5") and everything works fine.
The player will create this pair within his Player Data (Title) where the number of games will be stored.

However, the problem appears when I want to define this function directly in Cloud Script (Revisions). I have created a function that has 2 parameters - KeyName and ValueName (to create a Key:Value pair). These 2 parameters are then stored in 2 variables - var Key and var Value. In this function, I further use UpdateUserData, where I try to enter these two values (Key and Value variables) - however, if I do this, only Value variable takes the actual value that the user enters as an argument and Key part (within Data) ignores the user's input and uses only the variable name itself (or whatever is entered there) and not what is stored in it.

I would also like to add, that both of these variables actually have the correct value from user/caller when I use the logging to verify it, but the Data part simple doesn't take the Key one.


It is little bit complicated to explain in plain text, so I will attach some screenshots to demonstrate it.
I can attach only 2 screenshots, so the rest can be found here - https://imgur.com/a/nJGf534


Player DataCloudScriptunreal
customkey.png (20.9 KiB)
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

Please replace your Cloud Script handler with the code snippet below.

handlers.CTsDynamicKey = function (args, context) {
    let Key = args.KeyName
    let Value = args.ValueName
    
    server.UpdateUserData({
        PlayFabId:currentPlayerId,
        Data:{
            [Key]:Value
        }
    })
};

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

Martin avatar image Martin commented ·

Oh... I see! So, they Key has to be inside of [] brackets in order for it to accept the data/value from inside the variable?

0 Likes 0 ·
Martin avatar image Martin commented ·

Anyway, thank you so much, it all works perfectly now! You are the best ❤️

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.