- Home /
Question by
Darky1 · May 17, 2013 at 07:50 PM ·
functionwaitforsecondsreturncooldown
Return without Break
I want do return a value from a function and let the function running.
private var cooldown : boolean = false;
public function HasCooldown() : boolean {
if(!cooldown) {
cooldown = true;
return false; //Here i want to return false but let the function going
yield WaitForSeconds(5);
cooldown = false;
}
else{
return false;
}
}
I know that I can start a new function to use yield WaitForSeconds, but I want to know if it is possible.
Hope you can help.
Comment
Best Answer
Answer by whydoidoit · May 17, 2013 at 07:55 PM
No that isn't possible. As soon as you make a coroutine then it cannot return a value. Given you are using UnityScript you only have one way of getting any kind of "pseudo" return value from your function, and that is by passing a class to it, in which you set a value.
so:
class CoolDownReturnValue
{
var hasCoolDown : boolean;
}
function HasCooldown(var retVal : CoolDownReturnValue)
{
if(!cooldown)
{
retVal.hasCoolDown = false;
...
yield WaitForSeconds(5);
}
else
retVal.hasCoolDown = true;
}
function CallIt()
{
var coolDown = new CoolDownReturnValue();
HasCooldown(coolDown);
if(coolDown.hasCoolDown)
{
}
}
This will only work because the coroutine runs until the first yield immediately.