Is code in episode 1 of New Game 2 actually an infinite loop? In episode 1 of New Game 2, around 9:40, there is a shot of the code that Nene has written:



Here is it in text form with the comments translated:



// the calculation of damage when attacked
void DestructibleActor::ReceiveDamage(float sourceDamage) 
{
    // apply debuffs
    auto resolvedDamage = sourceDamage;
    for (const auto&amp; debuf:m_debufs)
    {
        resolvedDamage = debuf.ApplyToDamage(resolvedDamage);
        m_currentHealth -= resolvedDamage
        if (m_currentHealth &lt;= 0.f)
        {
            m_currentHealth = 0.f;
            DestroyMe();
        }
    }
}


After the shot, Umiko, pointing at the for loop, said that the reason why the code crashed is that there is an infinite loop.

I don't really know C++ so I'm not sure whether what she is saying is true.

From what I can see, the for loop is just iterating through the debufs that the Actor currently has. Unless the Actor has an infinite amount of debufs, I don't think it can possibly become an infinite loop.

But I'm not sure because the only reason that there is a shot of the code is that they wanted to put an easter egg here, right? We would have just gotten a shot of the back of the laptop and heard Umiko saying "Oh you've got an infinite loop there". The fact that they actually showed some code makes me think that somehow the code is an easter egg of some sort.

Will the code actually create an infinite loop?
The code is not an infinite loop but it is a bug.

There's two (possibly three) issues:


If no debufs are present no damage will be applied at all
Excessive damage will be applied if there is more than 1 debuf
If DestroyMe() immediately deletes the object and there are still m_debufs to be processed the loop will be executing over a deleted object and trashing memory. Most game engines have a destroy queue to work around this and more so that may not be an issue.


The application of damage should be outside the loop.

Here's the corrected function:

// the calculation of damage when attacked
void DestructibleActor::ReceiveDamage(float sourceDamage) 
{
    // apply debuffs
    auto resolvedDamage = sourceDamage;
    for (const auto&amp; debuf:m_debufs)
    {
        resolvedDamage = debuf.ApplyToDamage(resolvedDamage);
    }
    m_currentHealth -= resolvedDamage
    if (m_currentHealth &lt;= 0.f)
    {
        m_currentHealth = 0.f;
        DestroyMe();
    }
}