- Home /
Call function once in a while after a condition is met?
I want to grant the player one extra life every 10000 points.
void extraLife (){
PlayerPrefs.SetInt ("Lives", PlayerPrefs.GetInt("Lives") + 1);
}
I keep the player's score in PlayerPrefs.Getint ("Score");
The player's score doesn't increase in steady amounts - it increases in 10s, 200s and 500s.
How do I call the function extraLife()
once the 10,000
limit has been crossed, but only once, until the limit is broken again?
For example, the player has 10,210 points. I want to grant him a life. When he reaches 20,000, I want to grant him another life. But the player won't have exactly 20,000 points, but a bit more. How do I do this?
Answer by DoTA_KAMIKADzE · Apr 12, 2015 at 03:30 PM
Well you could do something of those:
1) If your progression is linear then just add another int variable and a constant of progression. How will that work? Well just when each time score increases check like this:
if (score >= progressionConstant * currentProgressionInteger)
{
currentProgressionInteger++;
extraLife();
}
Also don't forget to save that currentProgressionInteger.
2)If non-linear. Then you might have to add a for example a List or array of "progression bases" for which your player will be rewarded, and then do almost the very same thing (except that this time progressionConstant will be replaced with a List/Array/Dictionary/...):
if (score >= progressionList[currentProgressionInteger])
{
currentProgressionInteger++;
extraLife();
}
I did post an alternative way of awarding an extra life every x amounts of points, but apparently one answer is enough for such an "ancient" question! Sorry to anyone co$$anonymous$$g here who doesn't understand the "allowed" answer and might've benefited from $$anonymous$$e.