Search Unity

More optimally checking effect duration against timer class

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Dec 18, 2014.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I have a custom timer class I use to run the clock in my game- I feed it day length in real world minutes, and it proceeds to accurately track and increment the hour, day, year, and so forth. To keep things consistent, I want all buffs and effects that last a duration of time to consult my timer, not real world time, to see when they expire.

    Where I'm stuck is figuring out how to most efficiently execute the check- right now I've put everything into a function CheckEffects that gets called during the update loop, iterates through the list of every effect on the character, and goes:
    Code (csharp):
    1.  
    2. void CheckEffects(){
    3.      if (clock.CurrentTime >= effect.onsetTime + effect.duration){
    4.      character.Remove(effect);
    5.      }
    6. }
    7.  
    This is incredibly slow in the grand scheme of things, and the obvious optimization would be to throw it in a coroutine that checks a few times a second, but here's where my conundrum lies: time and resource management is a core gameplay component, and the player is intended to look at the numbers carefully and plan accordingly, which means that if they plan on a buff or debuff lasting exactly n (in-game) minutes, their character's abilities and output need to reflect that status effect having persisted for exactly as long as it was supposed to. A coroutine that updates things every 0.1 seconds, or even every 0.25 seconds, would look functionally identical from the player's perspective, but if one hour of game-time takes 30 seconds of real time, and I'm checking every 0.1 seconds, I'm losing chunks of seconds and possibly minutes during which effects that should've already self-terminated are counted as active. This becomes considerably more problematic when I accelerate time, and whole chunks of minutes are lost in the nine ticks I skip.

    So is there any simple way to make my checks more optimal within these constraints, or is running the check through Update the cruddy but inevitable solution to my problem?