fourthking
fourthking

Reputation: 331

Unity get a value being set in a coroutine

I have a Player class that has a public float reviveTimer. So when a player is downed another player can go revive them which starts a coroutine for the downed player that increases the revive timer until it gets to 10.

There is a situation where if one player goes to revive a downed player, 5 seconds later a second player comes to revive that same downed player and 1 second after that the first player leaves, the second player should continue the revive process with 6 seconds left (5+1) instead of restarting the timer since the original starter left. So when the second player come's in to revive the downed player, they need to know the revive timer is at 5 seconds.

Therefore I do something as simple as timer = downedPlayer.reviveTimer;. However this always returns 0, no matter what the actual timer is set to, as if I cant retrieve what the reviveTimer is being set to in the coroutine.

So how do I or can I retrieve the value of reviveTimer that is being set within a coroutine?

EDIT:

As requested, adding code. When a player is downed, this is the coroutine that the downed player runs.

    IEnumerator ReviveMeTimer()
    {
        while (reviveTimer < 10)
        {
            reviveTimer += Time.deltaTime;
            reviveIndicatorTimer.text = ((int)reviveTimer).ToString();
            reviveIndicatorImage.fillAmount = reviveTimer / 10;
            yield return null;
        }
    }

Upvotes: 0

Views: 119

Answers (2)

Adam B is essentially correct. I just want to point out that the reason for that is because of this line:

timer = downedPlayer.reviveTimer;

Because you get the timer value and store it in a local variable you aren't actually updating the downed player's reviveTimer. You're manipulating a local variable called timer and that's why it doesn't appear to synchronize with a second helping player.

Nominally this could be fixed by simply removing the local variable all together, however that would let two players aid the downed player and they'd get up faster (10 seconds divided by number of helpers). So in your detailed scenario, the first player would leave and the second player would see 3 seconds remaining, not 4:

Note: question says "6 second", but it's counting up towards 10, but says this value is the value remaining to get to 10.

 1 player for 5 seconds: +5
 2 players for 1 second: +2
 Total: +7

As both coroutines would be running.

The only way to fix this is as Adam B says: let the downedPlayer's code handle the timer (but you don't want the revive() method modifying it!).

Upvotes: 3

Adam B
Adam B

Reputation: 3842

The downed player should store the revive timer. The reviving players should call a public revive() method that gets the downed player’s timer. The revive method should check how long is left on the timer and update it accordingly. The revive method should be called from the downed player’s code. This way the downed player is in charge of maintaining its own timer internally and the other player’s access it through public methods. If you use coroutines to handle the timing, just have the coroutines (in the downed player) access its timer.

Upvotes: 3

Related Questions