1

Hi! At the moment, I'm trying to develop a game with C# and Unity where you earn money while the game is opened or even closed. In my game I have:

  • speed (2 speed = 1 / 2 = 0.5 secs to earn money)
  • delay (1 delay = 1 sec waited to "loop" again)

Example:

Speed is 3 and delay is 0.5. So it takes 0.33 (1/3) secs to earn money and waits 0.5 secs before starting the "loop" again.

Although it runs correctly with the game opened, I don't know how to calculate how many times you've earned money outside the game.

I already have the amount of seconds that have passed since you've closed the game so far.

  • Check out [Coroutine](https://docs.unity3d.com/ScriptReference/Coroutine.html)'s. You can specify how often they run. – Chuck Savage Apr 03 '17 at 21:31
  • I would love if the solution would be that simple. However, Coroutines can't run if the game is closed. – Pedro Brito Apr 03 '17 at 21:33
  • @PedroBrito so if speed = 2 and delay = 1 it basically means the player gets money every 0.5 + 1 = 1.5 second? – I.B Apr 03 '17 at 21:35
  • Yeah but the speed and delay will change as the player upgrades. I want to "dinamically" calculate this value, independently of the speed and delay. – Pedro Brito Apr 03 '17 at 21:37
  • @PedroBrito well he can't really level up when the application is closed so you have to use the speed and delay you have when the application is opened to calculate the amount you have to give him right? – I.B Apr 03 '17 at 21:38
  • @CNuts yes so is there anything to determine the value in any case ex: speed = 3, delay = 1.2 speed = 0.1, delay = 10 speed = 2.4, delay = 1.4 What i mean, **ANY** value? – Pedro Brito Apr 03 '17 at 21:41

3 Answers3

2

So basically your player gets money every speed/2 + delay. So if speed = 2 and delay = 1 he gets money every 1/2 + 1 = 1.5 seconds.

If you have the amount of seconds that have passed since he closed the game you can calculate how much time he was suppose to receive money.

So in that case if let's say the amount of seconds since he closed the application was 1000. You could find how many time you where suppose to give him money and give it all as soon as the application load.

So with speed = 2 and delay = 1 and with timeSinceClose = 1000 the amount of time you need to pay him is 1000/1.5 = 666.667

So if you payed the person let's say 1$ each time, as soon as he load the game you should give them 1 * 666.67 = 666.67$

The general formula (where x is total amount payed and y is pay amount per cycle) is :

x = (timeSinceClose/(1/speed + delay)) * y
I.B
  • 2,925
  • 1
  • 9
  • 22
  • @PedroBrito Could you explain more in detail how it didn't work. What values did you have for the time since it closed? – I.B Apr 03 '17 at 21:53
  • First of all, your algorythim is inverted, I think. _speed/2 + delay_ where speed = 2 and delay = 1, you've put 1/2 + 1. It should be 2/2 + 1 – Pedro Brito Apr 03 '17 at 21:56
  • Also, I've tried to use the following values: timeSinceClose = 1000, speed = 2 and delay = 1. It has returned 2000! – Pedro Brito Apr 03 '17 at 21:56
  • @PedroBrito Didn't you say in your question if speed is 2 then 1/2 = 0.5? _Speed is 3 and delay is 0.5. So it takes 0.33 (1/3) secs to earn money and waits 0.5 secs before starting the "loop" again._ – I.B Apr 03 '17 at 22:00
  • @PedroBrito So what you're doing is 1/speed no? – I.B Apr 03 '17 at 22:01
  • OH! I've figured out my problem: speed = 2, delay = 0.5, earning = 1 and timeSinceClose = 1000. I should use timeSinceClose / (1 / speed + delay) * earning = 1000 / (0.5 + 1) * 1. – Pedro Brito Apr 03 '17 at 22:04
  • @PedroBrito Exactly like that it's perfect :) – I.B Apr 03 '17 at 22:07
  • I just have a little problem. You earn 1 money per sec. *Each second*, you earn $1. Howeveer, with this background system, I'm getting values like 6.33 or 8.67 instead of 6, 9, etc :o – Pedro Brito Apr 03 '17 at 22:11
  • @PedroBrito Take a look at [this question](http://stackoverflow.com/questions/8844674/how-to-round-to-the-nearest-whole-number-in-c-sharp) to learn how to round up numbers. – I.B Apr 03 '17 at 22:13
  • that helped but what if money is 1.51, for example? You'll gain a lot of extra money in a long scale, $0.49 extra money :o – Pedro Brito Apr 03 '17 at 22:18
  • @PedroBrito That really is your choice on how much money you want to give them. You could always round it down or round it to 2 decimals or add some type of factor to reduce the amount of money they make. – I.B Apr 03 '17 at 22:22
1

If you want to know how much time has passed since you started an action you should use timestamps. This way even when the app is closed and you comeback, you can check the difference between the start of the action timestamp and current timestamp.

As an example:

DateTime startTime = new DateTime(636268559590016930L);
DateTime endTime = DateTime.Now;

TimeSpan span = endTime.Subtract(startTime);
Debug.Log(span);
Iggy
  • 4,767
  • 5
  • 23
  • 34
  • I already have the amount of seconds, but I want to know, with a speed and delay properties, how many times something ran in *x* seconds. – Pedro Brito Apr 03 '17 at 21:46
  • @PedroBrito In that case it would simply be: `(int)(span.TotalSeconds / 1.5f)` – Iggy Apr 03 '17 at 21:51
0

Check out Coroutine's. You can specify how often they run, while in game. And C#'s TimeSpan for when the game is closed, for measuring time's between last collection of money with DateTime.Now minus the current DataTime.Now. That'll give a TimeSpan that you can get the time in units.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69