Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 8r3nd4n · Apr 21, 2014 at 01:11 AM · timesavingcounterhighscores

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShubhamKumar1906 · Apr 11 at 05:05 AM 0
Share

Any solution?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

it is Playerprefs can use in saving highscore and both PC having this can same see data in Highscores? 0 Answers

How do I make a counter that goes up by 1 every second? 1 Answer

How to create an Idle counter ? 2 Answers

Highscore Save 0 Answers

Run time counter 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges