- Home /
How to continue scoring after game/app has been closed,Making score count after app is closed
So, I've been trying to make an idle game for mobile, and currently I'm faced with the probably simple problem of calculating your score after the game's been closed. Now, I'm not very competent with code, I'm using c#. All the code I have written is either very simple or taken off of threads and other stuff I've seen online. So far, I have this. I know it's wrong but so far it's what I've got
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class GaneManager : MonoBehaviour
{
public Text scoreText;
DateTime currentDate;
DateTime oldDate;
private const string money = "Money";
public int Money
{
get { return PlayerPrefs.GetInt(money); }
set { PlayerPrefs.SetInt(money, value); }
}
public float scoreTime;
public float scoreTimeInit = 5.0f;
// Start is called before the first frame update
void Start()
{
currentDate = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate:" + oldDate);
TimeSpan difference = currentDate.Subtract(oldDate);
print("difference:" + difference);
Mathf.RoundToInt(difference);
Money = Money += difference.TotalSeconds;
}
// Update is called once per frame
void Update()
{
scoreText.text = "Score: " + Money;
if (scoreTime <= 5)
{
scoreTime -= Time.deltaTime;
}
if (scoreTime < 0)
{
scoreTime = scoreTimeInit;
Money += 1;
}
}
private void OnApplicationQuit()
{
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs:" + System.DateTime.Now);
}
}
I'm open to suggestions on how to make things better and more efficient since I am a complete newbie when it comes to coding. Currently I'm trying to find out how to convert a TimeSpan Value into an int so I can at least try to add the diference to the score, but that's probably not very smart either. I don't even know if my loop for scoring is even the one I should be using for a game like this.
Thanks in advance for any and all help anyone can provide Papa bless ,