- Home /
Total Time Played since Installation
First of all: I was looking around "answers" already, but couldn't find exactly what I'm looking for. Whatever I tried is not giving me what I needed. So here I am asking for help! Sorry if I'm a total noob with coding... Anyway:
Basically I'd like my game to have a couple of statistics that the player can visualize on a menu. One is called "Time Played Since Installation" and one is called "Time Played Since Start Game".
The first should be a variable that is increasing every second the game is launched and running, despite what the player is doing and since the first run of the application (after installation), and keep going once the player come back after quitting (i.e. if he/she plays 2 hours today, and 1 hour tomorrow, the total time should be 3 hours).
The second is similar to the first, but it's reset every time the player "Start a New Game" (and stored in the Saved files and increasing as well). Basically the only variation is that the second has a "reset to 0" when clicking "New Game".
How can I obtain this? And also, how can I show the time format in Days/Hours/Minutes/Seconds?
Thanks!
Answer by CoryButler · May 17, 2019 at 01:31 PM
Check out C#'s TimeSpan class. It has the properties Days, Hours, Minutes, and Seconds. https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8
You can store DateTime.Now upon starting the application/new game, and then subtract the stored DateTime from DateTime.Now upon closing the application/starting another new game. This will give you the TimeSpan of the current play session.
DateTime startTime = DateTime.Now;
/* user plays the game for a while */
TimeSpan playTime = DateTime.Now - startTime;
I would save play times using TimeSpan.ToString()
, and load play times using new TimeSpan.Parse(str_previousPlayTime)
.
Answer by ShubhamKumar1906 · Apr 11 at 04:51 AM
Hey anyone gets the answer to this question working? If yes then do tell me the correct solution, please.
Your answer
Follow this Question
Related Questions
countdown timer acivation 1 Answer
If "equals" doesnt work. 3 Answers
Accumulating deltaTime oddity 1 Answer
Accurate timing while using timeScale. 1 Answer
creating a timed function 3 Answers