Hi,
I've implemented a simple calendar (message system) for my game which consists from:
1) List<Event> calendar;
2) public class Event
{
/// <summary>
/// When to process the event
/// </summary>
public Int64 when;
/// <summary>
/// Which object should process the event
/// </summary>
public GameObject who;
/// <summary>
/// Type of event
/// </summary>
public EventType what;
public int posX;
public int posY;
public int EventID;
}
3) calendar.Add(new Event(...))
The problem with this code is that even thought the number of messages is not excessise per second. It allocates still new memory and GC will once need to take care of that. The garbage collection may lead to a slight lag in my game and therefore I'd like to optimalize my code.
My considerations:
To change Event class in a structure - but the structure is not entirely small and it takes some time to copy it wherever I need it.
Reuse Event object somehow (add queue with used events and when new event is needed I'll just take from this queue).
Does anybody has other idea how to solve the problem?
Thanks for suggestions!