vendredi 27 novembre 2015

Better way to ensure a method logic executed once without boolean flag

This is a way to execute Dosomething logic once using flag. (C# code and Update is always called once per frame.)
And it's not so complicated, simple, very plain and well used way.

class Monster {
    bool isCalled = false;
    float energy = 0.0f;

    void Update()
    {
        energy += Random.Range(0f, 1f);
        if((isCalled == false) && (energy>100.0f))
        {
            isCalled = true;
            DoSomething();
        }
    }

    void DoSomething(){}
}

But, I think the management of boolean flag is a kind of tiresome task.
So I am trying to find better alternatives.

Is there any better or elegant way to do this (executing Dosomething once) without boolean flag?
For example, another design pattern's way, etc.

Aucun commentaire:

Enregistrer un commentaire