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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Multiupgrate calculation and performance.

Discussion in 'Scripting' started by unity_Ug-b95TzTbCuYQ, Mar 10, 2020.

  1. unity_Ug-b95TzTbCuYQ

    unity_Ug-b95TzTbCuYQ

    Joined:
    Mar 6, 2020
    Posts:
    7
    I am developing a small game for fun. It is an idle game based on an upgrade system. So you have objects that can be constantly improved via upgrades. There is a button for this (if there is enough money) to upgrade. This upgrade costs a certain amount, the subsequent upgrade costs a bit more and the following upgrade a little more. So there is a formula behind it. Let us assume it is the formula 2 ^ x, then the price for the first upgrade would be "1", the next "2", then "4", "8", "16", "32", ...

    So if you did all the upgrades, you'd spend a total of 63
    (1 + 2 + 4 + 8 + 16 + 32 = 63).

    Now the task. I would like to install an optional button with which you can carry out several upgrades with one click. Logically, I would have to execute a loop that adds these values together individually. Of course, this is not a problem with a size of a few upgrades, but it may be that you want to do up to 1000 upgrades at once. It would be a bit unperforming if I ran a loop that called the formula 1000 times per click.

    Is there a way to feed the formula with information or to convert that you get the sum of all individual updates after a calculation? I myself do not believe that it works, especially if you e.g. is already at level 5 and then wants to calculate 5 further upgrade steps. But maybe you know more than I do. :)
     
  2. jamespaterson

    jamespaterson

    Joined:
    Jun 19, 2018
    Posts:
    391
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    First of all, doubling the cost is actually pretty efficient to calculate, since it only required a simple bitshift, ie if we have a cost of 1, in bits that would be 0001, double of that (2) would be 0010, double of that (4) would be 0100 and so on. So if the formula was not just chosen for the sake of argument, bitshifts are very efficient. Even tho, with this formula, cost would grow so fast that thinking about multiupgrades or performance issues would be a waste of time, so i guess it's just chosen for the sake of argument.

    If you do not want to call the function a thousand times, then you need to think about a way to summarize the spent amount. This depends on the specific formula you use. For the formula 2^x, when you want to calculate 2^a to 2^b with b > a, you can just calculate 2^(b+1)-2^(a) to get the same result. With some numbers, if we already purchased 2^2 = 4 and wanted to upgrade 3 times (so from 2^3 to 2^5), that would be the costs of 8, 16 and 32. The total cost would thus be 32+16+8 = 56. Which is the same result we get if we calculate 2^(5+1)-2^(3) = 56. So in this case, reducing the required calculations is quite simple.
    For other formulas it may not be as simple, i literally dont know. You may, however, find a decent enough approximation and could offer the player some percents off for purchasing large amounts of upgrades at once.

    That said, calling a function 1000 times between two frames is easily possible. Of course this depends on the complexity of the called function, but even if you target 200 FPS, there would still be 5ms between each frame, which is a huge amount of time to any modern computer.
    If you are still worried, you could move the calculations off the main-thread and thus make them completely separated from your framerate. When doing this, you could even make use ob Jobs+Burst, speeding the calculations up even more.
     
  4. unity_Ug-b95TzTbCuYQ

    unity_Ug-b95TzTbCuYQ

    Joined:
    Mar 6, 2020
    Posts:
    7
    Okay x^2 was just a simple example jut to see, if it is generally possible. My formulars are much more complex and have some additional parameters. But you're right. I tested it with 1000 calls, and it is not a problem. :)

    Thanks you very much and have a nice day. :)