- Home /
Day counter and saving time played
I am making a game similar to a tower defense game where you have to see how long you can last. I have a timer that runs and the in-game hours take approximately 4 seconds each, so a 24 hour day is roughly a minute and a half. Once the time (hoursSurvived) go past 24, they are reset to 0 and the days are increased by 1. These parts work fine and are good for a single game session.
What I am trying to do is have each session time recorded and added to a total play time for the game. There should also be a best time that stays in the record.
The flow should be as follows:
At the start of a level
1) Load the total play time of the game (days and hours)
2) Load the best session time of the game (days and hours)
During Gameplay
3) Increment session time (days and hours)
4) Check to see if session time is better that the current best time. If so, change the best session time to be the same as the current session time.
5) Increment total play time (days and hours)
At Gameover
6) Display the current session times (days and hours)
7) Save current session time if it has become the best time
8) Save the total play time
The problem I have is when trying to add to the total play time as session hours and days will differ from the total hours and days. For example:
On first play through, the player makes 6 hours. (total time 0 days, 6 hours)
The next time they play they make 16 hours. (total time 0 days, 22 hours)
The next time through they make 10 hours (total time 1 day, 8 hours)
The next time they play they make 1 day and 17 hours (total time 3 days, 1 hour)
Maybe theres a way that I could just use hours only as the value and a function that would divide that by 24 to work out the days with the remainder as the hours. So that 260 hours = 10 days, 20 hours. Could modulus calculations work?
What I have that works for single session:
I am using NGUI and Anti Cheat for the saving prefs
using UnityEngine;
using System.Collections;
using CodeStage.AntiCheat.ObscuredTypes;
public class Clock : MonoBehaviour {
float timer;
public ObscuredInt daysSurvived;
ObscuredInt hoursSurvived; //convert our time to an int
ObscuredInt totalDaysSurvived;
ObscuredInt totalHoursSurvived;
ObscuredInt bestDaysSurvived;
ObscuredInt bestHoursSurvived;
public CounterLabel daysLived; //The label for our game over photo
UILabel dayLabel; //The label for displaying our days
UILabel hourLabel; //The label for displaying our hours
void Start ()
{
totalDaysSurvived = ObscuredPrefs.GetInt("TotalDaysSurvived");
totalHoursSurvived = ObscuredPrefs.GetInt("TotalHoursSurvived");
bestDaysSurvived = ObscuredPrefs.GetInt("BestHoursSurvived");
bestHoursSurvived = ObscuredPrefs.GetInt("BestDaysSurvived");
}
public void Begin()
{
daysSurvived = 0;
timer = 0.0f;
}
void LateUpdate ()
{
hoursSurvived = (int)timer; //convert the float time to an int
timer += Time.deltaTime / 4;//slow the time to be roughly .25 seconds
if(timer >= 24.0f)
{
daysSurvived += 1;
totalDaysSurvived += 1;
timer = 0;
}
//Ingame on screen labels
hourLabel.text = "Hour: " + hoursSurvived.ToString("##");
dayLabel.text = "Day:" +daysSurvived.ToString("##");
//on our gameover screen and summary
daysLived.label.text = "I Survived: " + daysSurvived.ToString("##") +"Days, " + hoursSurvived.ToString("##") + "Hours";
totalHoursSurvived += hoursSurvived; //runs every frame - not good!
if(totalHoursSurvived > 24)
{
totalDaysSurvived += 1;
totalHoursSurvived = 0;
}
//overwrite best times with the current times if the are greater
//Not tested and probably doesnt work as intended
if(hoursSurvived > bestHoursSurvived)
{
if(daysSurvived > bestDaysSurvived)
{
bestDaysSurvived = daysSurvived;
bestHoursSurvived = hoursSurvived;
}
}
}
public void Save()
{
ObscuredPrefs.SetInt("HoursSurvived", hoursSurvived);
totalHoursSurvived += hoursSurvived;
if(totalHoursSurvived > 24)
{
totalDaysSurvived +=1;
totalHoursSurvived = 24 % totalHoursSurvived;
}
ObscuredPrefs.SetInt("TotalHoursSurvived", totalHoursSurvived);
ObscuredPrefs.SetInt("DaysSurvived", daysSurvived);
totalDaysSurvived += daysSurvived;
ObscuredPrefs.SetInt("TotalDaysSurvived", totalDaysSurvived);
}
}
Thanks for any help!