- Home /
Is this a good way of resetting stats?
private void Awake()
{
currentHealth = startingHealth;
movement = GetComponent<PlayerMovement>();
points = PlayerPrefs.GetInt("Points", 0);
currentHealth = PlayerPrefs.GetInt("Health", startingHealth);
movement.extraJumpValue = PlayerPrefs.GetInt("JumpThree", movement.extraJumpValue);
movement.jumpForce = PlayerPrefs.GetFloat("JumpHeight", movement.jumpForce);
movement.speed = PlayerPrefs.GetFloat("PlayerSpeed", movement.speed);
}
public void ResetStats()
{
PlayerPrefs.SetInt("Points", points);
PlayerPrefs.SetInt("Health", 100);
PlayerPrefs.SetInt("JumpThree", 1);
PlayerPrefs.SetFloat("JumpHeight", 5);
PlayerPrefs.SetFloat("PlayerSpeed", 3);
}
private void OnDestroy()
{
PlayerPrefs.SetInt("Points", points);
PlayerPrefs.SetInt("Health", currentHealth);
PlayerPrefs.SetInt("JumpThree", movement.extraJumpValue);
PlayerPrefs.SetFloat("JumpHeight", movement.jumpForce);
PlayerPrefs.SetFloat("PlayerSpeed", movement.speed);
}
Answer by UltraGearGames · May 26, 2019 at 08:18 AM
Depends on if you want to carry the data over through scenes or entire game instances.
If you want it to carry over game instances, keep your code as it is.
If you want it to carry over through scenes but reset to a completely default null value upon game restart, make some static variables.
Thank you for the help! Although, if you mean by game instances by when they hit a "RESET STATS" button, it resets their stats until they upgrade them. So do I just keep it like this?
Basically, PlayerPrefs are more often used for "permanent" settings (Permanent until changed manually), you could say static variables are "temporary", in the sense they will carry over everything in that one current instance of the game, after a game restart, they reset to 0/null values.
$$anonymous$$y example of use of static variables is the following:
I have a FPS game, and basically on every start of a level, there pops up a tab with selection of the player $$anonymous$$m and how many bots are there gonna be on each $$anonymous$$m. I have the variables of the player $$anonymous$$m and bot numbers as static variables to carry over scenes, so that you dont have to set it every single time a scene reloads. That means i need to have a script in my menu scene which resets these statistics, so they are fresh upon loading a new map. I hope you understand it better now.
So in conclusion, YES, you should keep the code as it is.
oh and just saying, if my answer is what you were looking for, please mark it as the accepted answer. Thank you!