Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dazin09 · Jul 05, 2016 at 03:18 AM · unity 5gamedatabaseapplicationexternal

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
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

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

106 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 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 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 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 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 avatar image

Related Questions

Jumping/Jerking issue in Unity Vuforia markerless app 0 Answers

Firebase Offline Support? 1 Answer

Make an object get closer to another object slowly until it explodes,Make an object get closer to another object until impact 0 Answers

I can't make transition from actual animation to previous one 1 Answer

Rolling Animation Problem 0 Answers


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