Avgust Kuk
Avgust Kuk

Reputation: 127

How do I save values when going through other scenes?

please tell me the answers to my questions. for example, I have a lives system and a timer, I made a mistake at some level and my life was taken away, then the timer will turn on.
Questions:

  1. how to save the number of lives by moving to another scene and so that the timer is not interrupted while it is working?
  2. if the user left the game and logged in a minute later, then how can you make the timer with the life system work while the user is not there?
  3. Also with coins, how to save the number of coins when passing through the scenes and when exiting the game?

Upvotes: 0

Views: 86

Answers (3)

Erik Overflow
Erik Overflow

Reputation: 2306

There are many difference ways to persist data across scenes.

Some of the other responses here have pointed out Singleton and PlayerPrefs as potential ways to do this. I'll provide you with a list of options that I have used, and when I personally find them most useful.

  1. PlayerPrefs

    • Used to persist preferences or "quality of life" values. These values are easy to manipulate and should not be used to store "protected data" if you want to prevent players from cheating easily. Typically volume, landscape/portrait, graphics, etc are all stored here because they are "preferences" that affect game experience. Hence "player preferences".
  2. Singletons

    • Used to persist data across classes during runtime. The core purpose of a singleton is to ensure that only a single instance of a class exists. This, combined with applying DontDestroyOnLoad(this) to a game object allows a GameObject to live across multiple scenes without running the risk of creating duplicate instances.
  3. Saving to system storage or cloud

    • This is going to be a more complex solution, but it's required if you want data to persist between play sessions. Some people like to store data as JSON, while others like to use cloud storage like Firebase. My personal preference is to use a lightweight local database called SQLite, which is a very common pattern in GaveDev.

For your questions:

  1. how to save the number of lives by moving to another scene and so that the timer is not interrupted while it is working? A class that has had DontDestroyOnLoad(this) appplied would work well to pass the number of lives from scene to scene. It is often recommended to make this class a Singleton to avoid multiple instances of this class

  2. if the user left the game and logged in a minute later, then how can you make the timer with the life system work while the user is not there? If you want time to progress while the game is not running, you'll need to rely on DateTime and simulate the progress of your game. If you want to prevent the user from cheating by manipulating their system clock, you'll likely need to look into a server-based time check.

  3. Also with coins, how to save the number of coins when passing through the scenes and when exiting the game? If your data is more complex than a few values, SQLite or JSON is likely the go-to solution. If it's just a 1 or 2 values, then PlayerPrefs is usually fine. If you want to add some cheat prevention, you'll likely want to utilize a cloud-based solution.

Upvotes: 3

Andoni Rivera
Andoni Rivera

Reputation: 111

I would use PlayerPrefs class. It allows you storing float, integer and string type variables.

# Set lives left
PlayerPrefs.SetInt("Lives", 3);
# Get number of lives left
var lives = PlayerPrefs.GetInt("Lives");

Upvotes: 1

Rajas
Rajas

Reputation: 338

The answer to your 1st and 3rd question is a Singleton Class. The answer to your 2nd question is saving the logout time in a variable and comparing it later when the user logs in again.

TimeElapsed = CurrentTime - PreviouslySavedTime

Upvotes: 1

Related Questions