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

How can I use a single timed event to post events?

Discussion in 'Scripting' started by grimofdoom, Jul 3, 2016.

  1. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    In my game, I made my own clock (24 hours every 6 minutes). I am using Time.deltaTime to control it, but I do not want to use more than that use of time.deltatime so that I can keep performance up. My game is kind of like sim city and tycoon game mixed together, where every "minute" X resources are produced by Y building. I want to use my clock, being widely accessible through my Manager script, so I can post events for when a building should add more resources that it gained (like rock or iron ore). The field/map will end up having possibly hundreds of buildings, so using time.deltatime for that many timed events would probably cause performance to drop. My question is, how can I make a timed event so that every "minute" a 'flag' is raised for buildings to add more resources, only once per "minute". I thought of adding a bool in the timer that goes off then on, but I figured it would be way too fast for all the other scripts to recognize it- or it would happen multiple times per "minute".

    My script for my clock:
    Code (CSharp):
    1.     public void RunTime(){
    2.         absoluteTime += Time.deltaTime;
    3.         //====Minutes====
    4.         if (absoluteTime >= .25) {
    5.             min += 1;
    6.             absoluteTime = 0;
    7.         }
    8.         //====Hours====
    9.         if (min >= 60) {
    10.             hour += 1;
    11.             min = 0;
    12.         }
    13.         if (hour >= 24) {
    14.             hour = 0;
    15.         }
    16.         //====Convert====
    17.         currentTime = hour.ToString("D2") + ":" + min.ToString("D2");
    18.         if (previousTime != currentTime) {
    19.             previousTime = currentTime;
    20.             TimeDisplay.text = currentTime;
    21.         }
    22.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    I wouldn't waste too much time speculating about what might cause performance to drop. I'd give it a try first and if the naive "every building uses Time.deltaTime" works okay, then you're done.

    If you actually do start having performance issues, profile what is going on to figure out what the problem is. Don't assume it is something you speculated might cause a problem.

    If the profiler tells you that the performance hit turns out to be related to timing, then look into the logical construct called a priority queue, using "time until action required" as your priority measure.
     
  3. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    Alright. I guess you are right. I never really used Profiler before(and forgot it ever existed). I just kinda assumed that because I left it running for several hours, that when I came back and my mac's fans were blazing- that it was the time.deltatime causing it (since it is the only thing that does the most when nothing is going on). I will try using time.deltatime to check the process for now. But I will try and look into a priority queue (never got to learning one before).
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    These simple operations would probably not cause any trouble at all as they generally happen quite often either way. However, it's a good idea to think about a maintainable timing system for your intentions and this genre and there's already a good recommendation in the post above.

    One thing about the approach you've taken in this part of your code:
    Here, I'd personally reduce the elapsed time by your stepping instead of using a simple reset. You'll get a result which is significantly better (in terms of regularity) and it matches the elapsed real time more precisely.
     
  5. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    Wow, I feel stupid for not noticing that. It makes more sense to do "absoluteTime -= 0.25f" than "= 0". for a queue/maintainable time system, would I just iterate through every building(in a list or array) performing their actions required?