Sign In Register

How can we help you today?

Start a new topic
Answered

How to securely store temporary data for every player within a real-time match?

Hello,


I have read through the cloud code documentation and the tutorial about managing data, but I still have no idea how to actually store a temporary data on the server. I want to let all players who participated in a real-time match to by applied with an integer when the match started. The integer should be able to retrieve from clients and cloud code when the match is still going. Any recommendation on how to achieve this?


Thank you.


Best Answer

I'm not sure what you mean, like this?

 

var exampleInt = 24

 

You can just do that on the script and get it from any function, for example if players send packet 66 to the server, the server can send them something back like:

 

RTSession.onPacket("66", function(packet){

	RTSession.newPacket().setReliable(true).setOpCode(6).setTargetPeers([packet.getSender().getPeerId()]).setData( RTSession.newData().setInt(1, exampleInt)).send();
});

 

The server will send a packet of OpCode 6 back to the player that sent the packet 66 to the server. Is this what you're trying to achieve?


Cheers,

Omar




You're welcome, Tong!


Yes, the script is the server version and variables can be given back to each player. You can set intervals in the script to allow the server to execute some logic every once in a while. In the script you can do server authoritative instructions.


Yes, you can store any variable in the real-time script and it will persist.


Cheers,

Omar

I think I have get it, thanks for your helpful replies! One last question, wil the realtime script have an instance on each realtime match, and persist to be same across all players in the match? So I can just store the int as a variable in the realtime script?
Answer

I'm not sure what you mean, like this?

 

var exampleInt = 24

 

You can just do that on the script and get it from any function, for example if players send packet 66 to the server, the server can send them something back like:

 

RTSession.onPacket("66", function(packet){

	RTSession.newPacket().setReliable(true).setOpCode(6).setTargetPeers([packet.getSender().getPeerId()]).setData( RTSession.newData().setInt(1, exampleInt)).send();
});

 

The server will send a packet of OpCode 6 back to the player that sent the packet 66 to the server. Is this what you're trying to achieve?


Cheers,

Omar



Is it possible to store the int on server? And become available when client make request to the server? Or being use in cloud code?

 Hi,


How about something like:



 

var playersJoined = []; // this is only used at the start to make sure each player is connected

function contains(a, obj) { // this is a simple method that just checks if an element is in an array or not
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

RTSession.onPlayerConnect(function(player){
// first we check to see if the player has already joined the match
if(!contains(player.getPeerId(), playersJoined)){
    playersJoined.push(player.getPeerId()); // and add them if not
}

// next we check to see the max (or min) number of players has joined that match
if(playersJoined.length === 2){
    //Do something
}
});

 

This way you can track how many players have joined, when enough have, you can assign them roles/ints?


Hope it helps,

Omar

Like a temporary role, which will be apply to all players in a match randomly. So the data should be set by the server, and not client.

Hi Tong


"I want to let all players who participated in a real-time match to by applied with an integer when the match started"


Can you give me more details please,I don't understand why you're trying to achieve.


Who is setting this data? The developer or is this something dynamic?


Cheers,

Omar

Login to post a comment