none
How to view the return value of a method without re-executing it?

    Question

  • Hi, so I'm writing this with the unity framework but this is more of a general programming question. 

    Attack() returns either a value of x or it returns 0. I want to call Attack() to be used in another method and then see whether or not it returned 0. Currently what I have is I call Attack() and then call it again (rather nonsensically) to see if it equals zero. So i'd be working with two potentially completely different results. 

        
    private void Engage()
        {
            attackSpeed -= Time.deltaTime;
    
            //After x amount of time, apply damage.
            if (attackSpeed <= 0)
            {
                
                //get the leader's healthbar component
                health = getLeader.Leader().GetComponent<HealthBar>();
    
                health.TakeDamage(Attack());
    
                //I want too see the value returned from Attack() without calling it.
                if (Attack() == 0.0f)
                {
                    missed = true;
                }
                else { missed = false; }
                //CombatAnimation();
                //reset timer
                attackSpeed = 2.0f;
            }
    
    
        }
    How do I call the Attack() method and check it's value to see if it returned zero?
    Thursday, March 30, 2017 8:58 PM

Answers

  • Capture the returned value in a variable. Then reuse the variable.

    var attack = Attack();
    health.TakeDamage(attack);
    
    if (attack == 0.0f)
    ...

    Michael Taylor
    http://www.michaeltaylorp.3net

    • Marked as answer by FMC Q Thursday, March 30, 2017 9:33 PM
    Thursday, March 30, 2017 9:07 PM
    Moderator