Sign In Register

How can we help you today?

Start a new topic

Anyone can help me with real time script???

I keep doing your RT Scripts tutorial from the beginning to the end of it and each time i cant get through all of it... the last part, the one about the clock synchronization never works well... i always get the same error on unity which says that  nullable objects must have a va;lue but i dont understand because i enven tried copy and paste your code directly , i tried many different ways to male it work but still doesnt wanna work,, someone can hekp me on this?? cause im starting to go nuts....! loll.. thank you!


1 person has this question

someone ??

Hi Marc,


Can you give us some more details here please ? If you copy and pasted that part of the code is possible that the error is caused somewhere else. Are you getting this error when you try to run the scene ? 


Regards,

Liam

I think the error is from  my cloud code... i dont really understand it all yet.. here it is:




RTSession.onPacket(100, function(packet)

{

    var senderPeerId = packet.getSender().getPeerId();

    var senderPlayerId = packet.getSender().getPlayerId();

    

    if (packet.getTargetPlayers().length > 0)

    {

        for (var i = 0; packet.getTargetPlayers().length; i++)

        {

            var targetPeerID = packet.getTargetPlayers()[i];

        }

    }

    

    else

    {

    }

       

});



RTSession.onPacket(102, function(packet)

{

    var dateNow = new Date().getMilliseconds();

    

    if (dateNow - (packet.getData().getNumber(1)) < 1000)

    {

        return false;

    }

    

    else if (packet.getSender().getPeerId() === 4)

    {

        return false;

    }

    

});



RTSession.onPlayerConnect(function(player)

{

    var playerPeerId = player.getPeerId();

    var playerPlayerId = player.getPlayerId();

    

});



RTSession.onPlayerDisconnect(function(player)

{

    var playerPeerId = player.getPeerId();

    var playerPlayerId = player.getPlayerId();

    

});




RTSession.onPacket(100, function(packet){

    var rtData = RTSession.newData(); // create a new RTData object

    // check the peerId of the sender

    if(packet.getSender().getPeerId() === 1){

        rtData.setString(1, Date().getMilliseconds()); // if the sender is 1, then send back the current date

    }else{

        rtData.setNumber(1, 100); // otherwise send back a number

    }

    // construct a new RT-packet with op-code 1 using the rt-data we set

    RTSession.newPacket().setOpCode(101).setData(rtData).send();

});



RTSession.onPlayerConnect(function(player){

    // construct a new account details request with the ID of the player that just connected //

    RTSession.newRequest().createAccountDetailsRequest().setPlayerId(player.getPlayerId()).send(function(response){

        // construct a new RTData object with the player's display name //

        var rtDataToSend = RTSession.newData().setString(1, response.displayName);

        // send the display name to all the players //

        RTSession.newPacket().setOpCode(101).setData(rtDataToSend).send();

    });

});





RTSession.onPacket(100, function(packet){

    RTSession.getLogger().debug("This Is A Debug Message...");

    RTSession.getLogger().error("This is An Error Message...");

    RTSession.getLogger().info("This is An Info Message...");

    RTSession.getLogger().warn("This is A Warning Message...");

});




var playerZeroID = RTSession.getPlayers()[0].getPlayerId();

var playerZeroPeerID = RTSession.getPlayers()[0].getPeerId();

// get the ID of a player who's peerID is known

var playerPeerID = RTSession.getPlayer(1).getPeerId();

var playerID = RTSession.getPlayer(1).getPlayerId();

// we can use the playerID to check achievements for each player, for example //

RTSession.newRequest().createListAchievementsRequest().setPlayerId(playerID).send(function(response){

    var achList = response.achievements; // returns a list of achievements

});



RTSession.setInterval(function(){

    // get the current server-time in milliseconds //

    var rtData = RTSession.newData().setString(1, new Date().getUTCMilliseconds());

    RTSession.newPacket().setOpCode(101).setData(rtData).send(); // send the packet to all players

}, 1000); // set the time interval to be 1 second



var count = 0;

// a new setInterval callback function //

var everySecond = RTSession.setInterval(function(){

    if(count++ <= 10){

        // get the current server-time in milliseconds //

        var rtData = RTSession.newData().setString(1, new Date().getUTCDate());

        RTSession.newPacket().setOpCode(101).setData(rtData).send(); // send the packet to all players

    }else{

        RTSession.clearInterval(everySecond);

    }

}, 1000); // set the time interval to be 1 second




// this function is called when a player disconnects from the RT session //

RTSession.onPlayerDisconnect(function(player){

    // we start a new timeout function in 20 seconds //

    RTSession.setTimeout(function(){

        // after 20 seconds we send a message to the player using a log-event request //

        RTSession.newRequest().createLogEventRequest()

                                .setEventKey("sendMessageToPlayer")

                                .setScriptData({ "Message" : "Your Friends Are Having Fun Without You!" })

                                .send(function(response){});

    }, 20000);

});




RTSession.clearTimeout(timeoutId);



var playersJoined = [];



RTSession.onPlayerConnect(function(player)

{

    if (!contains (player.getPeerId(), playersJoined))

    {

        playersJoined.push(player.getPeerId());

    }

    

    if (playersJoined.length === 4)

    {

        RTSession.newPacket().setOpCode(100).setTargetPeers().send();

    }

    

});



function contains (a, obj)

{

    for (var i = 0; i < a.length; i++)

    {

        if (a[i] === obj)

        {

            return true;

        }

    }

    

    return false;

}



var playersJoined = [];


var totalPlayers = 0;



RTSession.onPlayerConnect(function(player)

{

    if (totalPlayers === 0)

    {

        RTSession.newRequest().createMatchDetailsRequest()

            .setMatchId(RTSession.getSessionId())

            .setPlayerId(player.getPlayerId())

            .setRealtimeEnabled(true)

            .send(function(response)

            {

                totalPlayers = response.opponents.length + 1;

            });

    }

    

    if (!contains (player.getPeerId(), playersJoined))

    {

        playersJoined.push(player.getPeerId());

    }

    

    if (playersJoined.length === totalPlayers)

    {

        RTSession.newPacket().setOpCode(100).setTargetPeers().send();

    }

    

});



RTSession.onPacket(101, function(packet)

{

    var rtData = RTSession.newData().setNumber(1, packet.getData().getNumber(1))

                                    .setNumber(2, new Date().getTime())

                                    

    

    RTSession.newPacket().setData(rtData).setOpCode(101).setTargetPeers(packet.getSender().getPeerId()).send();

    

});



RTSession.onPlayerConnect(function(player)

{

    if (!contains(player.getPeerId(), playersJoined))

    {

        playersJoined.push(player.getPeerId());

    }

    

    RTSession.getLogger().debug(playersJoined.length);

    

    if (playersJoined.length === 2)

    {

        RTSession.newPacket().setOpCode(100).setTargetPeers().send();

        

        RTSession.setInterval(function()

        {

            RTSession.getLogger().debug(new Date().getTime());

            RTSession.newPacket().setOpCode(102).setTargetPeers(peerIds).setData(RTSession.newData().setNumber(1, new Date().getTime() )).send();

            

        }, 1000);

    }

    

});

So?

This looks like Cloud Code to me, not C#. You said it was a Unity error.

yes b ut i tyhought maybe it was the server response that could be the source of that error....  i dont really know what i could be doing wrong...

Cast .GetBoolean to boolean. .GetBoolean returns bool? which is nullable. IE: if((bool)response.GetBoolean("someJsonKey"))
Login to post a comment