Home > Software design >  How to store DateTime data and calculate it later? Unity C#
How to store DateTime data and calculate it later? Unity C#

Time:04-21

I'm a beginner, and I'm trying to make something like a classic Tamagotchi game with a flower, where you need to water it once a day. I'm using "Water the flower" button as a main functionality. And to do that, I need to know when was the last time clicked to compare it with the current time.

I was hard trying to work with the PlayerPrefs and DateTime methods, but here is the problem — DateTime using longs, and PlayerPrefs using int or strings. I found the solution to convert DateTime into Binary and then to String, but Unity told me this thing:

FormatException: Input string was not in a correct format. System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Number.ParseInt64 (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Int64.Parse (System.String s, System.IFormatProvider provider) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Convert.ToInt64 (System.String value) (at <695d1cc93cca45069c528c15c9fdd749>:0) HealthScript.Update () (at Assets/Scripts/HealthScript.cs:42)

Here is the entire code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthScript : MonoBehaviour
{
    public int health = 100;
    public Slider slider;
    public int damage;
    public int waterTime;
    public bool isGoing = true;
    DateTime currentTime = DateTime.UtcNow;
    DateTime lastTimeClicked;

    //reseting the health every time button is clicked and saving the time of it
    public void buttonClicked()
    {
        health = 100;
        PlayerPrefs.SetString("Last Time Clicked", DateTime.UtcNow.ToBinary().ToString());
    }

    public void Update()
    {
        //connecting the health to the slider
        slider.value = health;
        //quit of the game
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        //calculating the difference between last click and actual time
        long temp = Convert.ToInt64(PlayerPrefs.GetString("Last Time Clicked"));
        lastTimeClicked = DateTime.FromBinary(temp);
        print("LastTimeClicked"   lastTimeClicked);

        TimeSpan difference = currentTime.Subtract(lastTimeClicked);
        print("Difference: "   difference);
    }
}

For now, I'm just trying to solve this issue to step forward to other things. Could you please suggest the solution for the console log, or maybe a better way to achieve my goal?

CodePudding user response:

If you change the way you store and retrieve the DateTime the rest of your logic should be fine. Try this:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthScript : MonoBehaviour
{
    public int health = 100;
    public Slider slider;
    public int damage;
    public int waterTime;
    public bool isGoing = true;
    DateTime currentTime = DateTime.UtcNow;
    DateTime lastTimeClicked;

    //reseting the health every time button is clicked and saving the time of it
    public void buttonClicked()
    {
        health = 100;
        PlayerPrefs.SetString("Last Time Clicked", DateTime.UtcNow.ToString()); // you can add a formatter here if you want
    }

    public void Update()
    {
        //connecting the health to the slider
        slider.value = health;
        //quit of the game
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        //calculating the difference between last click and actual time
        lastTimeClicked = DateTime.Parse(PlayerPrefs.GetString("Last Time Clicked"));
        print("LastTimeClicked"   lastTimeClicked);

        TimeSpan difference = currentTime.Subtract(lastTimeClicked);
        print("Difference: "   difference);
    }
}

CodePudding user response:

If you want to log to the console, the command is Debug.Log, Debug.LogWarning, or Debug.LogError.

You'll need to make sure you have your console filters set to show whatever you're trying to print, too:

Console filters

  • Related