question

matias avatar image
matias asked

Cloudscript cast string to int

So i'm working in an app with a few leaderboards and challenges, the thing is that i need that data to be modify from play fab, so i'm using title data for that... But recently when trying to get an int from title data it is not working correctly

var getTitleDataRequest ={ "Keys": ["DesafiosDisponibles"] };
    var getTitleDataResponse = server.GetTitleData(getTitleDataRequest);
    
    if (!getTitleDataResponse.Data.hasOwnProperty("DesafiosDisponibles")) 
    {
        log.error("key not found. Exiting...");
        return JSON.stringify([]);
    }
    else 
    {
        var myObject= JSON.parse(getTitleDataResponse.Data.DesafiosDisponibles);
        
        for (var i = 0; i < myObject.Items.length; i++) 
        {
            if (myObject.Items[i].Nombre == args.Leaderboardname) 
            {
                var baseScore = myObject.Items[i].PuntosBasicos.Value; 
                var correctScore = myObject.Items[i].PuntosBasicos * (myObject.Items[i].BonusRacha.Value / 100) * Racha;
                var timeScore = myObject.Items[i].BonusTiempo.Value * (Tiempo * 1000);
            }
        }
    }


    var totalScore = baseScore + correctScore + timeScore;
    
    var AddScore = UpdatePlayerScore(args.Leaderboardname, totalScore);
 

the code works perfectly, the only thing is that "totalScore" is not an int or at least that is what i think since if i change total score to a flat number it works but if i leave it it doesnt (if i debug totalscore it gives me the value i need)

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

You can use Number(str) or parseInt(str) to cast the string to int, and since statistic only accepted integer typed data, you can use Math.floor(2.13123) to round down.

1 comment
10 |1200

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

matias avatar image matias commented ·

Thanks it worked

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.