question

kawashima999game avatar image
kawashima999game asked

Cloud Script Unity Data Saving

I wrote to save the code to share the join code of Relay to each player using playFab's Cloud Script, but it is not saved. please tell me.

js: // joinコードの保存と取得(以下追記) handlers.saveJoinCodeWithMatchId = function (args, context) { var matchId = args.matchId; // マッチIDを引数から取得 var joinCode = args.joinCode;

 // マッチIDをキーにしてjoinCodeを保存
 var key = "JoinCode_Match_" + matchId;
 server.SetTitleData({
     Key: key,
     Value: joinCode
 });

     // 成功のメッセージを返す前に、保存された値をログに記録
 log.info("Join code saved: " + joinCode + " for matchId: " + matchId);

 // 成功のメッセージを返す
 return { message: "Join code saved successfully for matchId: " + matchId };

};

handlers.getJoinCodeWithMatchId = function (args, context) { var matchId = args.matchId; // マッチIDを引数から取得

 // マッチIDをキーにしてjoinCodeを取得
 var key = "JoinCode_Match_" + matchId;
 var joinCodeResult = server.GetTitleData({
     Keys: [key]
 });

 // joinCodeを返す前に、取得した値をログに記録
     log.info("Join code retrieved: " + joinCodeResult.Data[key] + " for matchId: " + matchId);


 // joinCodeが存在するか確認
 if (joinCodeResult.Data && joinCodeResult.Data[key]) {
     // joinCodeを返す
     return { joinCode: joinCodeResult.Data[key] };
 } else {
     // joinCodeが見つからない場合のメッセージを返す
     return { message: "Join code not found for matchId: " + matchId };
 }

};

C#: public void SaveJoinCode(string joinCode, string matchId) { Debug.Log($" {matchId}"); var request = new ExecuteCloudScriptRequest { FunctionName = "saveJoinCodeWithMatchId", FunctionParameter = new { joinCode = joinCode, matchId = matchId }, GeneratePlayStreamEvent = true, }; PlayFabClientAPI.ExecuteCloudScript(request, OnCloudScriptSuccess, OnError); }

 public void GetJoinCode(string matchId)
 {

     Debug.Log($" {matchId}"); 
     var request = new ExecuteCloudScriptRequest
     {
         FunctionName = "getJoinCodeWithMatchId", 
         FunctionParameter = new { matchId = matchId },
         GeneratePlayStreamEvent = true,
     };
     PlayFabClientAPI.ExecuteCloudScript(request, OnCloudScriptGetJoinCodeSuccess, OnError);
 }
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

·
Xiao Zha avatar image
Xiao Zha answered

From your code, it seems that you want to use Title Data as global variable, however, Title Data is used for “global constant/static data” and not suitable for “global variable”, as the Title Data is cached, and the changes may take up to fifteen minutes to refresh in those caches. May I know what functionality you want to implement and what is the matchId and the joincode?

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.

kawashima999game avatar image kawashima999game commented ·

I want to use Relay+Mirror in Unity to play online matches. I would like to use playFab for the matchmaking function. I would like to share the join code issued by Relay with my matching partner to join the Relay server. matchId is the ID issued during playFab matchmaking.

0 Likes 0 ·
kawashima999game avatar image kawashima999game commented ·

I want to use Relay+Mirror in Unity to play online matches. I would like to use playFab for the matchmaking function. I would like to share the join code issued by Relay with my matching partner to join the Relay server. matchId is the ID issued during playFab matchmaking.

I tried to make the player data public and have the other player read it and share the code, but that doesn't work either. The code and error are shown below.I was able to write. Unable to load.

 public void GetJoinCode(string hostPlayerId, string myMatchId)
 {
     Debug.Log($"Attempting to retrieve join code with Host Player ID: {hostPlayerId} and My Match ID: {myMatchId}");

     PlayFabClientAPI.GetUserData(new GetUserDataRequest
     {
         PlayFabId = hostPlayerId,
         Keys = new List<string> { "JoinCode", "MatchId" } 
     }, result => {
         Debug.Log("Got user data Successfully retrieved user data.");
           
     }, error => {
         Debug.LogError($"Error retrieving user data: {error.HttpCode} - {error.ErrorMessage}");
     });
 }

Autoconnected Player "Autoconnected Player" Error retrieving user data: 400 - HTTP/1.1 400 Bad Request

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha kawashima999game commented ·

I have tested your code; it works fine if you set the PlayFabId( master_player_account id) and Keys correctly. However, set the join code in host player’s data is not security, as other players which are not in that match can access to the join code. You may consider referring to https://learn.microsoft.com/en-us/gaming/playfab/features/multiplayer/lobby/lobby-and-matchmaking to join the matched players into an arranged lobby, then share join code between matched players using lobby data.

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.