- Home /
Calling coroutines through pointers/"Function" class?
Here's my code:
AIBase.js
#pragma strict
var in_action: boolean;
var action: function(): IEnumerator = coroutine;
function Update () {
if(in_action)
return;
in_action = true;
enter_phase();
}
function enter_phase() {
Debug.Log("entering action");
yield action();
in_action = false;
}
function coroutine () {
Debug.Log("in coroutine");
yield WaitForSeconds(1);
}
If I comment out the "yield WaitForSeconds(1)" the code works and I see "in coroutine" in the debug log. However, if I put it in, I can't see it anymore. For some reason, I can't properly call coroutines through pointers or Function classes in UnityScript (but I can call normal functions). How can I get the functionality I want?
P.S. I realize I can just write:
yield coroutine();
but I want to be able to have this AIBase be a base class where the actions are set by the derived classes.
Answer by laian · Aug 11, 2014 at 05:45 PM
Fixed it!
Here's the new code:
#pragma strict
var in_action: boolean;
var action: function(): IEnumerator = coroutine;
function Update () {
if(in_action)
return;
in_action = true;
enter_phase();
}
function enter_phase() {
Debug.Log("entering action");
yield StartCoroutine(action());
in_action = false;
}
function coroutine () {
Debug.Log("in coroutine");
yield WaitForSeconds(1);
}
just had to change
yield action();
to
yield StartCoroutine(action());
Answer by rutter · Aug 11, 2014 at 05:50 PM
UnityScript does allow you to create and call function references, with the Function type (note big F):
#pragma strict
function Start() {
var myFunc : Function;
myFunc = Test;
myFunc();
var myFuncWithArg : Function;
myFuncWithArg = TestWithArg;
myFuncWithArg(6);
}
function Test() {
Debug.Log("Test successful");
}
function TestWithArg(arg : int) {
Debug.Log("You're thinking of the number " + arg);
}
One downside with the above: I don't know if there's a way to force compile-time checking of the types you'll pass to those functions, like you can get in C#.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160347im_/https://answers.unity.com/themes/thub/images/avi.jpg)