convert float to int for google leaderboard
Hi
i have a new mode where score is measured by distance, i've got it working fine in game and saving in player pref, but unsure how to report it back to google leaderboard as float...
here is my distance player pref script - working
float distanceunit = 0;
float bestDistanceunit;
private void Awake() { if (PlayerPrefs.HasKey("BestDistanceBulking")) { bestDistanceunit = PlayerPrefs.GetFloat("BestDistanceBulking"); }
private void Update() { currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, time / accelerationTime); transform.position += Vector3.right currentSpeed Time.deltaTime; time += Time.deltaTime;
if (distanceIncreasing)
{
distanceunit += pointsPerSecond * Time.deltaTime;
}
if (distanceunit > bestDistanceunit)
{
bestDistanceunit = distanceunit;
PlayerPrefs.SetFloat("BestDistanceBulking", bestDistanceunit);
GPGSLeaderboards.UpdateLeaderboardBestDistanceBulking();
}
here is part of my gpgsleaderboard script for posting to leaderboard which isnt working..
public static void UpdateLeaderboardDistance()
{
if (PlayerPrefs.GetFloat("BestDistanceBulking", 0) == 0)
{
return;
}
Social.ReportScore(PlayerPrefs.GetFloat("BestDistanceBulking", 1), GPGSIds.leaderboard_bulk_chokie__distance_, (bool success) =>
{
if (success)
{
//PlayerPrefs.SetInt("BestDistanceBulking", 0);
}
});
}
Thank you heaps
Answer by Dangerface · Jun 07, 2020 at 05:18 AM
You could use the 'Mathf.RoundToInt' function to round the float however you like
https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html
Thanks, would you be able to advise how i'd do it with my script? thanks
Sorry, I don't know anything about google leaderboards, so I don't know if your provided code is something you wrote or some standard stuff.
You could try this:
PlayerPrefs.SetFloat("BestDistanceBulking", $$anonymous$$athf.RoundToInt(bestDistanceunit));
or if you want to store the int in its own variable just convert it and store it globally
int bestDistInt;
bestDistInt = $$anonymous$$athf.RoundToInt(bestDistanceunit);
thank you - this got it working for showing the best distance in the main menu etc. but still having problems reporting the correct distance into the google leaderboard, this is what ive done so far:
Player distance script:
if (distanceIncreasing)
{
distanceunit += pointsPerSecond * Time.deltaTime;
}
if (distanceunit > bestDistanceunit)
{
bestDistanceunit = distanceunit;
PlayerPrefs.SetInt("BestDistanceBulking", bestDistanceunit);
GPGS$$anonymous$$erboards.Update$$anonymous$$erboardBestDistanceBulking();
bestDistInt = $$anonymous$$athf.RoundToInt(bestDistanceunit);
}
$$anonymous$$erboard script (it is reporting to the leaderboard but the score is always "1" regardless of my distance
public static void Update$$anonymous$$erboardDistance() { if (!PlayerPrefs.HasKey("BestDistanceBulking")) { return; } Social.ReportScore(PlayerPrefs.GetFloat("BestDistanceBulking", 1), GPGSIds.leaderboard_bulk_chokie_distance, (bool success) => { if (success) { } }); }
Your answer

Follow this Question
Related Questions
Try to show a string on textfield 0 Answers
OnApplicationPause Not working 0 Answers
Leaderboard not found 0 Answers
local player ranking system 0 Answers
Leaderboard shows an error with Google Play Services 0 Answers