Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

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

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

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:

share|improve this answer

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

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.