Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Calculating resources gained per second

Discussion in 'Scripting' started by solidp26, Jun 2, 2021.

  1. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    Hi, Im making a idle game that has a drill mining system. It basicly adds a certain ammount of resources to the inventory every x seconds. For example: 1 drill mines a single tile of coal - this adds 8 coal per 0.5 seconds.
    The thing i need help with is a system that calculates how much ore is being gained per second total, because there will be many drills mining at different speeds and different frequencies. Like in Cookie Clicker there is a "per second: " text. Having a "gain: ammount/s" shown to player would be a neat little feature.

    I can't really figure out how to calculate this. So how can i get this value?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Just add up all of the "X per second" numbers. That will give you a total per second number. Btw 8 per .5 seconds is actually 16 per second. You just divide the number of coal by the amount of time it takes to gather that. 8 / 0.5 == 16.
     
    Bunny83 likes this.
  3. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    That's where the problem is. The drill starts mining as soon as it's placed. It mines independently of other's. So when you have a bunch of them working the gain becomes kinda chaotic. That's why i can't just add up all of the "X per second" numbers.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I don't understand how this changes anything.
     
  5. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
  6. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    I ended up with this system.
    To be honest i could just add all the mining results from each and every one of the drills that mine the ore. But i ended up with a system that's more fail proof. It's basicly a simple math equation.

    Code (CSharp):
    1. public IEnumerator GainPerSec(int id)
    2.     {
    3.         Debug.Log("Started calculating gain per second variable for " + inv.item[id].itemName);    
    4.  
    5.         float startAmmount; //ammount in the begining of the interval
    6.         float endAmmount; //ammount in the end of the interval
    7.  
    8.         while (true)
    9.         {
    10.             startAmmount = ammountBuffer;
    11.          
    12.             yield return new WaitForSeconds(1);
    13.          
    14.             endAmmount = ammountBuffer;
    15.  
    16.             gain = (endAmmount - startAmmount).ToString("F0") + "/s";
    17.         }
    18.     }
    Sometimes you have to ask a question on the forums to really start thinking about the solution.
     
    Last edited: Jun 3, 2021
  7. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Using coroutines isn't wrong, but imagine the processing to be done when you have tons of drills?
    As PraetorBlue suggested, you should sum the resource/time then multiply by time, if your time unit is 0.5 seconds, then the maximum delay/offset of a miner is 0.5seconds, from player prospective it's unlikely to be a problem, and it sure will be more friendly than running hundreds of coroutines as the game progresses.
    P.S: I'm assuming you're running such a game on mobile devices? if so then you should be concerned of CPU and battery usage as well, if for PC then I doubt the coroutines will have notable effect even for thousands of them.
     
    PraetorBlue likes this.
  8. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    True. Didn't think of that before.

    And yes it will be on mobile, so i'll change it from coroutines to a delay sum.

    UPD:
    This is how it looks now
    https://imgur.com/XhiWGE8
     
    Last edited: Jun 29, 2021