2010-11-15 78 views
0

我目前正在编写XNA中的RPG引擎。引擎执行一系列脚本命令,但必须阻塞,直到下一个脚本命令。我怎样才能做到这一点?XNA中的阻塞函数

例如:

// interact with an NPC named in String 'Name' 
String interactfunc = String.Format("{0}_Interact", Name); 
System.Reflection.MethodInfo info = Factory.Script.GetType().GetMethod(interactfunc); 
if (info != null) info.Invoke(Factory.Script, new object[]{this}); 

//this may run the following script command for NPC 'Bob' 

    public void Bob_Interact(NPC Bob) 
    { 
     Bob.Say("Well this worked."); 
     Bob.Say("Didnt it?"); 
    } 

//the say command looks like this 

    public void Say(String Text) 
    { 
     TalkGui gui = new TalkGui(this, Text); 


     Factory.Game.Guis.Add(gui); 
     Factory.FocusedGui = gui; 

    } 

现在我需要剧本的等待,直到第TalkGui已运行下一个脚本命令之前被解雇。

这样做的最好方法是什么?也许可以在自己的线程中运行脚本函数?

回答

1

你不想为这种事情使用线程。他们的体重太重了。

你真正想要的是co-routines。您可以使用yield和迭代器在C#中模拟它们。

有一些例子herehere。也许我的回答here也值得一读。

C#5.0引入了一种更好的异步编程方式。 Here's a videohere is the CTP