Search Unity

Avoiding Update: Do I need to?

Discussion in 'Editor & General Support' started by nabrown3, Apr 9, 2016.

  1. nabrown3

    nabrown3

    Joined:
    May 29, 2015
    Posts:
    10
    I'm designing a game where lots of different elements can be causing various effects simultaneously (similar to every other game really). I was looking at my code and thought a "clean" solution would be to create a "GameEvent" abstract class that each type of effect/event is a child of:

    Code (CSharp):
    1. public abstract class GameEvent {
    2.  
    3.     public enum Type
    4.     {
    5.         TileManipulation,
    6.         DamageOverTime,
    7.         SpawnUnit,
    8.     }
    9.  
    10.     public Type type;
    11.     public float creationTime;
    12.  
    13.     public GameEvent(Type type, float creationTime)
    14.     {
    15.         this.type = type;
    16.         this.creationTime = creationTime;
    17.     }
    18.  
    19.     /// <summary>
    20.     /// Causes this event to occur
    21.     /// </summary>
    22.     /// <returns>True if this event should be removed from the event list, false otherwise</returns>
    23.     public abstract bool Fire();
    24. }
    So in my main Game class I would maintain a list of these GameEvents and in the Update loop I would fire all of them in the list. Anything that occurs that would cause a new game effect instead creates a GameEvent and adds it to the queue.

    This is beneficial because it allows you to essentially order the execution of events each frame by event type (ex always spawn new monsters first). My question to all of you is 1) do you think this is worth it? and 2) Do you think this will incur a lot of lag since I'm pulling from the queue in Update().

    thanks for any help!