Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I created a simple scripting language to use on a Unity3D game. My parser generates an AST with nodes for every type of operation, and to run the script all I need to do is call Execute() on the root of the tree, which propagates recursively to every node in it.

To run functions from my script, I first create a dictionary linking function names to delegates, and the function node simply needs to look it up run it.

But I have ran into a problem now... Some of the functions I need to call from the script are coroutines that take time to execute, such as WalkTo or WaitNSeconds, and for those coroutines I need to halt the script execution until they are done.

I'm stuck on figuring a way to halt the script execution, as once I call Execute() on the root node, the script gets executed recursively all the way.

Just to provide some more context, here are some examples of a few possible types of nodes and how they operate (not the actual nodes from my language, but close enough). Some nodes simply call Execute() on their children and return nothing. Some nodes simply return values directly. Other nodes get values from their children and modify it in some way.

abstract class Node
{
    object Execute();
}

class RootNode: Node {
    List<Node> Children;
    object Execute() {
        foreach(Node node in Children)
            node.Execute();
        return null;
    }
}

class NumberNode : Node {
    int Value;
    object Execute() { return Value; }
}

class SumNode : Node {
    Node Left;
    Node right;
    object Execute() {
        return (int)Left.Execute() + (int)Right.Execute();
    }
}

class PrintNode : Node {
    Node Expression;
    object Execute() {
        Console.WriteLine(Expression.Execute().ToString();
        return null;
    }
}

class FunctionNode: Node {
    string Name;
    object Execute() {
        return SymbolTable.FindFunction(Name)();
    }
}
share|improve this question
    
yield return StartCoroutine(function()); will wait for the given coroutine to finish before continuing execution. It's a place to start at least. –  XGundam05 May 24 '13 at 18:36
    
@XGundam05 Yes, I am aware, but I can only call yield return from within a coroutine. So I think I would have to change object Execute() to IEnumerator Execute() and replace every call to Execute() with a return yield new StartCoroutine(Execute()). Few problems with this. First I would lose the return value, so I would need to do something like pass a callback to write the value to on every Execute. Worse, every yield return StartCoroutine will execute in the next frame. But I only want to wait extra frames on coroutine call nodes, not on every other type of operation. –  David Gouveia May 25 '13 at 7:39
    
Get the return values of the coroutine in a loop. Check the returned value's type - if it's a wait yield type, handle it differently. –  maul May 25 '13 at 8:04
    
@maul If I understood your comment correctly, using a loop will not do, since it would block the thread. If that is not it, can you show me an example? What I think I need is a way to yield from deep inside a recursive call, and resume from that point. –  David Gouveia May 25 '13 at 8:20
    
What I meant was that you can manually skip all the returned "WaitXYZ" objects that would halt the thread. Not the best solution, that's why I only commented it. –  maul May 25 '13 at 10:10
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.