Having something happen when game isn't running.
Hi all,
Bit of a game logic question here, if your familiar with the app Clash of Clans, you'll know that a lot of the production of materials, buildings, upgrading etc happens outside the game.
For example, a building that produces money, will be still producing money when the game isn't running, and you'll be able to come back to the game, open it, and you will have a different value from when you left. Does this somehow run off a server? Is there way of implementing without a server? One theory I've had in my head is somehow calculating the downtime of the app, and once you've come back, then changing the value.
For example, if you are generating one gold per second, and you close the app and leave the game for 60 seconds, as soon as you leave a timer starts and when come back the timer stops and feeds the value to a function that gives the the gold when you return.
I'ld like to hear others thoughts on procedures to implement this.
Thanks.
Answer by Dazin09 · Jul 05, 2016 at 04:48 AM
Seems like I didn't do enough studying before I posted this answer:
using UnityEngine;
using System.Collections;
using System;
public class GameManager : MonoBehaviour {
//This class is responsible for the storing of values such as
//money and public opinion
public static float gold;
public static float publicOpinion;
public static float protection;
public static bool canTap;
//Caps
public static int serfCap;
public static float taxCollect;
public static float goldCollectedWhileAway;
public static float goldPerSec; //Gold per second the serfs generate
DateTime currentDate;
DateTime oldDate;
void Start ()
{
StartCoroutine("HappinessCalculation");
//Date comparisons
//Store the current time on start
currentDate = DateTime.Now;
//Grab the old time and store as long
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
//Convert the old time from binary to a DateTime variable
DateTime oldDate = DateTime.FromBinary(temp);
print("oldDate:" + oldDate);
//Use Subtract method and store result as a timespan variable
TimeSpan difference = currentDate.Subtract(oldDate);
print("Difference:" + difference.TotalSeconds);
//Adding gold while away to gold amount
goldPerSec = UpgradesManager.serf_Amount * 0.2f * UpgradesManager.serf_Effectiveness;
goldCollectedWhileAway = goldPerSec * (float)difference.TotalSeconds;
gold += goldCollectedWhileAway;
Debug.Log("Gold Collected While Away: " + goldCollectedWhileAway);
}
void Update ()
{
}
public void CollectTaxes()
{
gold += taxCollect + UpgradesManager.merchant_Amount * 4 * UpgradesManager.merchant_Effectiveness;
publicOpinion -= 0.1f;
}
IEnumerator HappinessCalculation()
{
while(true)
{
yield return new WaitForSeconds(5f);
publicOpinion += UpgradesManager.noble_Amount * 0.5f;
publicOpinion -= UpgradesManager.serf_Amount / 100;
}
}
//Random Events
public void Raid(float strength)
{
}
public void Deserters(float strength)
{
}
void OnApplicationQuit()
{
//Save the current system time as a string
PlayerPrefs.SetString("sysString", DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs:" + System.DateTime.Now);
}
}
It's very blotchy and could probably be improved upon somehow. I'll leave this post up for a couple more hours if anyone would like to share anything.
Your answer
Follow this Question
Related Questions
Jumping/Jerking issue in Unity Vuforia markerless app 0 Answers
Firebase Offline Support? 1 Answer
I can't make transition from actual animation to previous one 1 Answer
Rolling Animation Problem 0 Answers