0
\$\begingroup\$

I am developing a stand-alone 3d game in Unity that will have anywhere from 1 to 200 timers running simultaneously. There will be a GameObject containing 1 timer. For this game timer durations will range from 5 minutes to 4 days. There will not be any countdown displays or any UI for the timers.

Each object is a prefab, with all the necessary materials included. An attached script will handle the timer and all the necessary code to change the materials and make any sound effects. Once the timer is expired, the user will then click on the object again, and the object will be destroyed, and the user's inventory will be adjusted.

If the user wants to save or end the game before all the timers are done, the start value of the still running timers is to be saved to an XML file such that when the game is started again, any still running timers will be checked to see if they have expired, where the object's materials will be changed appropriately.

I am still trying to figure out what type of timer to use, and see also if there are any suggestions for saving and calculating times over several days.

What class(es) of timers should I use? Are there any special issues I should look out for in terms of performance?

\$\endgroup\$
  • \$\begingroup\$ I am assuming you are creating a game that has coins or elixir that you can collect from objects after a certain amount of time… Instead of creating your own "timers" why don't you just use the C# DateTime class and check your object timers with that? You could set a variable "timeUntilEvent" to a time in the future and compare the current time with that. Do you understand what I'm talking about? or have I just destroyed this explanation lol… If you need any clarification just post here again and I will try to help. \$\endgroup\$ – Savlon Aug 25 '14 at 2:32
  • \$\begingroup\$ @Savlon - I have worked out how to use DateTime to handle my events. Thank you. How would I go about saving the timer starts in XML? \$\endgroup\$ – Kyle Seidlitz Aug 25 '14 at 6:44
  • \$\begingroup\$ Personally I would write my own ScheduledEvents class, that stores a Dictionary of callback functions and expiration times, rather than use any built-in timers. \$\endgroup\$ – jhocking Aug 25 '14 at 19:05
3
\$\begingroup\$

With so many timers, you are better off with setting ExpirationTime for each of your events.

When you need to start a "timer" set the events expiration time to "now" + "timer duration". Then add just one timer that will run e.g. each second and check all the "expiration times" and fire events when their time has come.

\$\endgroup\$
  • \$\begingroup\$ This is how I would approach the ScheduledEvents class I mentioned: a class that has a list of all registered expiration times. \$\endgroup\$ – jhocking Aug 25 '14 at 20:01
  • \$\begingroup\$ Seems like the ideal case for a priority queue. \$\endgroup\$ – crush Sep 2 '14 at 19:55
0
\$\begingroup\$

Recognizing time is pretty expensive but you can do it just once with a System.Timers.Timer and let that drive your timers. I.e.:

public class EfficientTimer ()
{
    //A static timer that drives all other timers; ticks every .1 seconds
    static System.Timers.Timer GlobalTimer = new System.Timers.Timer (100);

    //What the EfficientTimer keeps track of time with
    public uint MyTimerCount = 0;

    public EfficientTimer ()
    {
        GlobalTimer.Elapsed +=
            //Function that increases MyTimerCount every tick of GlobalTimer
            (object sender, System.Timers.ElapsedEventArgs e) => MyTimerCount++;
    }

    public float ToSeconds ()
    {
        //Returns the time in seconds
        return .1f * MyTimerCount;
    }
}

With the above class, you could have thousands of timers running simultaneously without breaking a sweat. When you're done with one, you can even recycle it by setting MyTimerCount to 0 instead of sending it to the garbage collector.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.