Hi,
I am working on adding coroutines support to an application framework that uses anglescript for GUI scripting. After giving a try to the coroutines sample, it appears that it won't work for me: you cannot manage the coroutines from the scripts, as it requires that the ExecuteScripts function to be called on a regular basis by the C++ side. Also, calling yield from a context that was not registered will just do nothing.
So I have written a very simple Generator add-on that makes it easier to manage coroutines from scripts, using a very similar syntax as Javascript (not returning values for the moment). So the coroutines sample script becomes:
void main()
{
int count = 10;
generator@ genB=createGenerator(thread,
dictionary = {{"count", 3},
{"str", " B"}});
while( count-- > 0 )
{
print("A :" + count + "\n");
genB.next();
}
}
void thread(dictionary @args)
{
int count = int(args["count"]);
string str = string(args["str"]);
while( count-- > 0 )
{
print(str + ":" + count + "\n");
yield();
}
}
The major difference is that "main()" can be called from any context (no need to add it to the context manager). And you can then write your own coroutines dispatcher in Angelscript, like this very simplistic one:
class GenDispatcher
{
bool next()
{
if(nextIndex>=tasks.length)
nextIndex=0;
if (nextIndex<tasks.length)
{
bool hasNext=tasks[nextIndex].next();
if (!hasNext)
{
tasks.removeAt(nextIndex);
}
else
nextIndex++;
}
return tasks.length!=0;
}
array<generator@> tasks;
private int nextIndex=0;
};
Would you be interested in incorporating this add-on into angelscript? It is already running and I'll share the code anyways, but if it has to become an official add-on, some extra work is required, such as implementing the generic calling convention (which I do not use for the moment).