Performance due to entity update

Posted by Rizzo on Game Development See other posts from Game Development or by Rizzo
Published on 2014-06-11T19:03:21Z Indexed on 2014/06/11 21:42 UTC
Read the original article Hit count: 352

I always think about 2 ways to code the global Step() function, both with pros and cons.

Please note that AIStep is just to provide another more step for whoever who wants it.

    // Approach 1 step
    foreach( entity in entities )
    {
        entity.DeltaStep( delta_time );
        if( time_for_fixed_step ) entity.FixedStep();
        if( time_for_AI_step ) entity.AIStep();
        ...
        // all kind of updates you want
    }

PRO: you just have to iterate once over all entities.

CON: fidelity could be lower at some scenarios, since the entity.FixedStep() isn't going all at a time.

    // Approach 2 step
    foreach( entity in entities ) entity.DeltaStep( delta_time );
    if( time_for_fixed_step ) foreach( entity in entities ) entity.FixedStep();
    if( time_for_AI_step ) foreach( entity in entities ) entity.FixedStep();
    // all kind of updates you want SEPARATED

PRO: fidelity on FixedStep is higher, shouldn't be much time between all entities update, rather than Approach 1 where you may have to wait other updates until FixedStep() comes.

CON: you iterate once for each kind of update.


Also, a third approach could be a hybrid between both of them, something in the way of

    foreach( entity in entities )
    {
        entity.DeltaStep( delta_time );
        if( time_for_AI_step ) entity.AIStep();
        // all kind of updates you want BUT FixedStep()
    }

    if( time_for_fixed_step )
    {
        foreach( entity in entities )
        {
            entity.FixedStep();
        }
    }

Just two loops, don't caring about time fidelity in nothing other than at FixedStep().

Any thoughts on this matter? Should it really matters to make all steps at once or am I thinking on problems that don't exist?

© Game Development or respective owner

Related posts about game-loop

Related posts about fixed-timestep