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'm trying to figure out how to implement c# scripting into my game (XNA with C#). I will be using C# as the scripting language.

My question is, how to call functions that take more than 1 frame to finish?

For example:

class UserScript : Script
    public override void execute(Game game)
        //script must wait for dialog to be closed
        game.openDialog("This is a dialog");

        //script should'nt wait for this
            int goldToGive = 100;
            goldToGive += 100;

        //script should wait for cinematic to end

        //doesn't wait

I found that you can do that with yield, but I'm not sure if it's the correct way (It's from 2010, the article mentioned no longer exists). http://stackoverflow.com/questions/3540231/implementing-a-simple-xml-based-scripting-language-for-an-xna-game

Is yield the answer? If so, can anyone point me any examples/tutorials/books? I haven't found any regarding my situation.

If not, what approach should I take? or am I better off with multi-threading?

share|improve this question
in new c# there is compiler as service, that you could use to interpret just some code instead of all, or you can use some IL interpreter (i think mono has one inside), or use yield (thats what unity does) or run each script as separate task (thread) –  Kikaimaru Dec 8 '12 at 21:41
add comment

3 Answers

You can find the mentioned article in the internet archive: The magic of yield.
As mentioned in the conclusion of the article, you don't have to return a "float", but rather a custom type that you can query for all sorts of results.
In the case of your dialog (and in the same way for your cinematic), you can write something like:

Dialog dialog = game.openDialog("This is a dialog");
while (dialog.isOpen) {
    yield return 1f;
//Continue script...
share|improve this answer
add comment

Since yield is just generating a state machine for you, you can just as easily roll your own and implement a bunch of tasks to be executed in some sort of collection. Or use yield and use the results for something (time to wait, state to be in, etc.). Good discussion about all this with some images can be found here: http://stackoverflow.com/questions/5923767/simple-state-machine-example-in-c

share|improve this answer
add comment

Consider using reflection instead. You can provide dlls with classes to call and types/interfaces to uYpse. Users can compile code and drop dlls in a particular folder for you to load at runtime. Same for content.

share|improve this answer
add comment

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.