Sign In Register

How can we help you today?

Start a new topic
Answered

Json problem

Good day.

I have a problem with json variable.

In atributes (Events) I make variable type - json. If I want to receive data from json file - the data is null.

Here is the code:

image


 

//Sending data to json variable
//====================================================================================================
//
// Cloud Code for RT_TEST_RECIEVE, write your code here to customize the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://docs.gamesparks.com/
//
// ====================================================================================================
var matchData;
var participantData = [];

RTSession.setInterval(function(){
    getMatchDetails();
        if(matchData != undefined)
        {
            generateBattleStatistics();
        }
}, 10000);

function generateBattleStatistics(){
    for(var i = 0; i < matchData.participants.length; i++)
    {
        participantData.push({
            heals: matchData.participants[i].heals == null ? matchData.participants[i].endurance * getRandomNumber(20, 30) : matchData.participants[i].heals,
            damage: matchData.participants[i].strength,
            blockPoint: getRandomPoint(0,3),
            attackPoint: getRandomPoint(0,3)
        });
    }
    var request = RTSession.newRequest().createLogEventRequest().setEventKey("SET_MD");
        request.setmid(RTSession.getSessionId());
        request.settest(JSON.stringify(participantData));
        request.setPlayerId(RTSession.getPlayers()[0].getPlayerId())
        .send(function(response){
            sendStatistics();
        });
}

function sendStatistics(){
    var rtDataToSend = RTSession.newData().setString(1, "true");
    RTSession.newPacket().setOpCode(5).setData(rtDataToSend).send(); 
}

 

//Receiving data from json variable ====================================================================================================
//
// Cloud Code for SET_MD, write your code here to customize the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://docs.gamesparks.com/
//
// ====================================================================================================

var matchID = Spark.getData().mid;

var testJSON = JSON.parse(Spark.getData().test);

var matchDetails = Spark.getMultiplayer().loadMatch(matchID);
var matchData = matchDetails.getMatchData();

for(var i = 0; i < testJSON.length; i++)
{
    matchData.participants[i].heals = testJSON[i].heals;
    matchData.participants[i].damage = testJSON[i].damage;
    matchData.participants[i].blockPoint = testJSON[i].blockPoint;
    matchData.participants[i].attackPoint = testJSON[i].attackPoint;
}

matchDetails.setMatchData({"participants": matchData.participants}); 

 Thanks in advance


Best Answer

LOL. I already solved the problem. 
Result:

 

var matchID = Spark.getData().mid;

var participantsData = Spark.getData().pdata;

var matchDetails = Spark.getMultiplayer().loadMatch(matchID);
var newMatchData = matchDetails.getMatchData();

for(var i = 0; i < participantsData.length; i++)
{
    newMatchData.participants[i].heals = participantsData[i].heals;
    newMatchData.participants[i].damage = participantsData[i].damage;
    newMatchData.participants[i].blockPoint = participantsData[i].blockPoint;
    newMatchData.participants[i].attackPoint = participantsData[i].attackPoint;
}

matchDetails.setMatchData({"participants": newMatchData.participants});

 


1 Comment

Answer

LOL. I already solved the problem. 
Result:

 

var matchID = Spark.getData().mid;

var participantsData = Spark.getData().pdata;

var matchDetails = Spark.getMultiplayer().loadMatch(matchID);
var newMatchData = matchDetails.getMatchData();

for(var i = 0; i < participantsData.length; i++)
{
    newMatchData.participants[i].heals = participantsData[i].heals;
    newMatchData.participants[i].damage = participantsData[i].damage;
    newMatchData.participants[i].blockPoint = participantsData[i].blockPoint;
    newMatchData.participants[i].attackPoint = participantsData[i].attackPoint;
}

matchDetails.setMatchData({"participants": newMatchData.participants});

 


Login to post a comment