- Home /
Large amount of timers
Hey guys,
I want to create a mobile application that can use a variable amount of "timers" but should at least be able to time at least 100 items at a time without any problems, this is what I want, but, is it also possible? There are for example 120 items, each item has it's own 'time to completion'. For each item I want to show a timer.
For above example, I think the creation of 120 timers will be the death of any device, but what would be a good way to approach this?
Not really, a timer is just a float
variable, and displaying it is just the visual representation. A 120 visual items is really not much (depending on the complexity), many games display thousands of items at the same time.
Anyway, best is to try it, and if you find that it doesn't work, then profile it and check what causes the performance problem. As the old program$$anonymous$$g saying goes "premature optimization is the root of all evil".
Haha alright, but what makes more sense. Using unity's deltaTime in Update or using C#'s Timer function?
Depends what level of precision you want. If it is for a game, Time.deltaTime
should be fine, even though repeatedly sum$$anonymous$$g floats
accumulates precision error over time. C#'s Timer is more precise, but it is also more performance heavy (as far as I know), and it measures real time, so it might not be the best if you also need to pause the game (for example, you enter an in-game menu).
Answer by Summit_Peak · Jun 04, 2018 at 01:42 PM
Instead of using timers, try storing the start time for each item:
DateTime startTime = DateTime.Now;
item.startTime = startTime;
...
float elapsedSeconds = (float)((DateTime.Now - item.startTime).TotalSeconds);
if(elapsedSeconds >= item.timeToCompletion)
Kill(item);
Answer by ShadyProductions · Jun 04, 2018 at 02:10 PM
Very easy actually, just cache all timers by item in a dictionary and in update count them up all up every second.
Here is an example:
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts
{
public struct Item
{
// Unique item id
public int Id;
}
public class TimeClass : MonoBehaviour
{
private float _gameTime;
private Dictionary<int, int> _timerCache;
private void Start()
{
_timerCache = new Dictionary<int, int>();
}
public int? GetTimerValue(Item item)
{
int time;
if (_timerCache.TryGetValue(item.Id, out time))
return time;
return null;
}
public void AddTimer(Item item)
{
int time;
if (!_timerCache.TryGetValue(item.Id, out time))
{
_timerCache.Add(item.Id, 0);
}
}
public void RemoveTimer(Item item)
{
if (_timerCache.ContainsKey(item.Id))
{
_timerCache.Remove(item.Id);
}
}
private void Update()
{
_gameTime += Time.deltaTime;
// Calculate every second
if (_gameTime >= 1f)
{
_gameTime = 0f;
// Add to all timers
foreach (var item in _timerCache.Keys)
{
_timerCache[item] = _timerCache[item]++;
}
}
}
}
}
Why a Dictionary
? Quick lookup is not essential, and using Item
as key does not provide any advantage, not to mention that using foreach
in Unity is not recommended (due to the GC). A List
would make a lot more sense in this case.
Where did you get the information that 'foreach' in unity is not recommended, yes foreach over a dictionary makes garbage but only the first loop, the once thereafter create 0 garbage. A list creates no garbage in unity anymore and would techniqually be a better choice if the Item pool would be small. A dictionary can be useful in cases if there are a lot of items in game, and yes the key could have rather been an int, possibly the Item Id.
I chose the dictionary because it was the easier way of storing the timer, without having to directly add the timer to the item class/struct.
I read it in this Unity blog post: https://unity3d.com/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games
Apparently it has been fixed in Unity 5.5.
I find this solution very unusual... but I guess it must be a program$$anonymous$$g style thing.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Animator equivalent of animation.isPlaying? 1 Answer
Rotate sun during set of time 2 Answers
programming help/teacher? c# 0 Answers