0

I have a series of event being triggered from a main loop, which keeps track of time and functions as a clock. For some trivial functions, I want them to occur bound by this clock, but don't need to fire every tick. My solution is

private IEnumerator regenProc(float deltaTime)
{
    float elapsedTime = deltaTime;
    while (elapsedTime < 0.5f)
    {
        yield return null;
        elapsedTime += deltaTime;
    }
    CurrentValue += elapsedTime * (MaxValue * regenPercent + regenFlat);

but that feels ugly and clunky, I would prefer the return type to just be void like I want it to be.

private void regenProc (float deltaTime)
{
    static float elapsedTime += deltaTime;
    if(elapsedTime < 0.5f)
        return
    CurrentValue += elapsedTime * (MaxValue * regenPercent + regenFlat);
}

would be cleaner and simpler, back in C++ days, but this technology seems missing. Is there a way to achieve this functionality which is.

  1. Purely native to the method (it has to work inside delegates, and not conflict with other instances)
  2. Not an enumerator
  3. Not a feature removed by C# for some reason
Airn5475
  • 2,452
  • 29
  • 51
Azeranth
  • 151
  • 11
  • The code looks very confusing... Normally you'd never return `IEnumerator` from regular C# code... At very least it would be generic one, but even more likely `IEnumerable`... Are you sure you are not talking about some Unity3d specific stuff? – Alexei Levenkov Jul 30 '20 at 20:38
  • @AlexeiLevenkov It's only returning an enumerator, because that's the only syntactically legal way to use yield. – Azeranth Jul 31 '20 at 03:32

0 Answers0