question

georgeosipov52 avatar image
georgeosipov52 asked

CloudScript SetTitleData

Sorry if the same question was asked before, but I can't understand what am I missing.

Unity C# code:

    public void setTitleData() {
        var request = new ExecuteCloudScriptRequest();
        request.FunctionName = "SetTitleDataTest";
        PlayFabClientAPI.ExecuteCloudScript(request, OnCloudScriptExecuteSuccess, OnFailure);
    } 

    void OnCloudScriptExecuteSuccess(ExecuteCloudScriptResult _result) {
        Debug.Log("success");
    }

CloudScript code (just an example from "Using Title Data" page):

function SetTitleDataTest() {
    try {
        server.SetTitleData({
            Key : "Server2Data",
            Value : "67890"
        });
        log.debug("Set titleData successful");
    } catch(error) {
        log.error("Got error setting titleData:");
        log.error(JSON.stringify(error));
    }
}

And it doesn't work. Title data is not changed.

UnityLog writes "success". Thanks for help.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Actually, we should back up to the question of what it is you're trying to do. You cannot drive changes to Title Data from player actions. Title Data is a sharded and cached resource that cannot be changed at a high frequency. Even if it were allowed, trying to do so would simply not work, as the time required to update all the caches with the new data would mean that the data values being written by the players would constantly be stomping each other.

Can you describe the feature you're trying to implement?

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

georgeosipov52 avatar image georgeosipov52 commented ·

Okay, I got it.. Then I can't do what I want with changing TitleData.

What I need to do:

I have a server managing with Photon Unity Network. I just need to store somewhere some data about current server state which should be updated at least once per 10 seconds. What can I do here? I can't achieve this with Photon.

0 Likes 0 ·
brendan avatar image brendan georgeosipov52 commented ·

Can we back up to the top-level of your design? What, specifically, are you trying to do with the data you want to write?

0 Likes 0 ·
georgeosipov52 avatar image georgeosipov52 commented ·

Actually I have already described what I want to achieve. If it is really necessary... I need to store current amount of players on servers. So players could see whether the rooms are full or not :)

0 Likes 0 ·
brendan avatar image brendan georgeosipov52 commented ·

Actually, I don't see that anywhere in the info above. Regardless, what you're looking for is a way to have a piece of data be updated by all players (potentially millions), have high consistency, and be readable by all players. A generic system that could do that for arbitrary data types would be excessively expensive to run, so we do not currently provide that. Since you're focused specifically on maintaining a count, I would recommend you use Redis for this.

0 Likes 0 ·
georgeosipov52 avatar image georgeosipov52 commented ·

I will have less than 1000 players at once and this data will be updated only once per 10 seconds. So I will need only to read this value quite often.

0 Likes 0 ·
brendan avatar image brendan georgeosipov52 commented ·

All I can say is that I would strongly advise against this. Writing to Title Data as a result of a player action is not a supported scenario, and we will not be able to assist with any issues you encounter. Further, if the title attempts to update Title Data too frequently, we may have to throttle its ability to call that endpoint, to prevent issues.

0 Likes 0 ·
georgeosipov52 avatar image georgeosipov52 commented ·

What about using player data or player group data? Can it be read quite often? I don't need it to be read REALLY often. Now I will have only 100 players. While growing I will think about something else. I will need playfab for a lot of things but just for first beta only for this. (For Redis I need my own server that I don't have)

0 Likes 0 ·
brendan avatar image brendan georgeosipov52 commented ·

Cloud Script was designed for sharing data between a relatively small number of players - generally, for sharing state info on turn-based async games. It's not designed for a large number of players, and if you have multiple players read from it and write to it at the same time, you're going to have consistency issues. So no, it really isn't a way to solve for this.

If you need to query for the info about the number of players in Photon, you'll really have to get that data directly from Photon, as cloudweight pointed out. There isn't an effective way to bypass the need to get that info from them, since that's where the authoritative information about the number of connected players is.

0 Likes 0 ·
cloudweight avatar image
cloudweight answered

Photon allows you to read the current players in a server via an array, or you can return the number of players on the server via int. This is built into Photon and is expressed via the docs of Photon.

PhotonNetwork.room.PlayerCount;
PhotonNetwork.countOfPlayers;
PhotonNetwork.countOfPlayersInRooms;
PhotonNetwork.countOfPlayersOnMaster;
PhotonNetwork.countOfRooms;
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.

georgeosipov52 avatar image georgeosipov52 commented ·

Thanks... I know that. The main problem is that I can't receive room state updates while I'm disconnected from Photon. I mean how will I notice about a full server and when it is not full again? Maximum CCU is reached so I can't get updates from Photon... Or there is a way I don't know? :)

0 Likes 0 ·
cloudweight avatar image cloudweight georgeosipov52 commented ·

Unless you are using a Photon Self-Hosted Server (and not Photon Cloud) then there is no concrete way to get a player count because cloud hosting is rather limited on the API side but here are some ideas on how to work with this limitation.

Server Side Approach

Make a build of your project that is barebones and can only load a scene where you can connect to Photon and can receive information from the cloud server. Place this build on a VPS and let it run indefinitely.

This build can send and receive updates from the Photon Cloud to a MySQL database automatically and then you can use this information to list your servers using PHP.


Client Side Approach

If you don't have a VPS to run an automated client then maybe you can have a bool called 'BroadcastServer' that a server master can turn off and on when they create a room. This is so that they know they are sending extra data in conjunction to the base game data to the web.

Then if 'BroadcastServer' is true, you can have the server master send a player count to an SQL Database every x amount of seconds/minutes. Then via your website you can list your servers with PHP and read the player count that is received from the server master.

0 Likes 0 ·
georgeosipov52 avatar image georgeosipov52 commented ·

@cloudweight I would have solved a lot of problems had I a server with SQL Database. I thought of your solutions. But thank you anyway! I appreciate it :)

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.