Implementing a simple XML based Scripting Language

2020-03-07 06:26发布

问题:

I'm working with a team on a RPG engine in C# and XNA. We're planning on targeting Windows and Windows Phone 7, but are running into issues with AI interactions and controlling player actions during cutscenes. FOr the most part, everything is extracted using the MVC design pattern, but abstracting all logic and movement into a controller could cause issues down the line. So the idea is to have an interface (IScriptEngine) that takes an IScriptObject and updates data in the map model accordingly. I was thinking about putting the scripts in an XML sort of syntax:

<Script Name="MoveNPC_1"> 
    <Action Command="MoveToTile" Target="NPC_1" Value="10,2"/> 
</Script> 

And have an IScriptEngine parse it accordingly. I highly doubt parsing in a giant switch statement would be a good idea, but it's what I've been working with.

switch(Action.Command)
{
    case "MoveToTile":
      { 
         doMovement(Action.Value, Action.Target); 
         break;
      }
}

Even my high school programming experience tells me that this is a bad idea, but I can't think of any other way around it.

Edit: I'd like to somehow embed this all in the map file. In the map XML file, we have areas designated for , which contains multiple tags. Then there would be an section, designating where the NPC is on the map, and what resources and scripts it would use. Then there would be a section, where all battles would be defined (Again, defining resources and what not), and then possibly a Section, where these scripts would be defined. I just wish there was a way to compile C# on the fly using WP7. If I could get support on both sides, this wouldn't be an issue. I wonder if there would be any Mono library that could be ported to WP7 that would share the functionality of CodeDom and all of the Compiler classes?

回答1:

XML Scripting!? Gasp! Now that is something that "could cause issues down the line"!

Why not just use C#? I hear it's pretty terrific. I've already given my opinion on this matter.

You need to ask yourself: Do your scripts really need to be data driven? Is there a reason that data can't be expressed using C#?

Do they really need to be interpreted at runtime? Because, if they really do, that can be done with C#.

And here's another answer of mine to an almost identical question, over on the gamedev.stackexchange.com. I've even put a little example of a possible implementation in there.

If you want to have actions that take more than a frame to execute (basically co-routines), for example: "Walk over here, Talk, Wait, Walk over there", you can implement this with yeild in C# reasonably well, too.


Edit: If you want to mix your XML levels, and C# script, here is an example of what I mean:

<Level>
    <Door position="4,4" name="spookyDoor" />
    <Region position="4,2" name="spookyOpener" />
</Level>

And in C#:

public void LevelStart()
{
    this.Regions["spookyOpener"].OnPlayerEnter += () =>
    {
        (this.Items["spookyDoor"] as Door).Open();
    };
}


回答2:

You may wish to consider embedding a real language such as Lua instead of using an XML syntax. There's a reason why you don't see many programming languages built upon actual XML syntax! It's really awkward for actual programming tasks.



回答3:

You could use an existing XML parser, there are a lot of different ways of doing it (SAX, StAX) and parsers available for many languages.

Then you could use XPath to reference nodes in your document.



回答4:

Do you necessarily need to interpret the scripts at runtime? You say your development is content driven, which is a good thing to do. But you don't necessarily need your scripts to be interpreted in order to take advantage of the increase in speed and efficiency from using XML or a scripting language.

I'm following Shawn Hargreaves' example of creating XML data and allowing the Content Pipeline to compile it into a .XNB. If you implement a scripting system or integrate an existing and make a ContentPipeline project for it, you can compile just the scripts that were added or modified, very little downtime between runs in comparison to recompiling the whole darn game.

And I definitely wouldn't go for XML, XNA may automatically have an importer for it, but it will be ugly as hell trying to use it. XNUA was supposed to be a lua library for XNA that's supposed to work well enough for this sort of thing.

Making designers compile to .XNB isn't all that different from the crap you have to do in engines like Source anyway.

Also, I found the following useful for thinking about scripted events:
Riverman Media - Object Oriented Programming, the Scripted Event System
Brandon Furtwangler - First Impressions Matter



回答5:

You may also consider a library called XNA Debug Terminal at http://www.protohacks.net/xna_debug_terminal . This allows you to execute arbitrary c# statements at runtime. It basically works like an Eval() method for scripting languages. Maybe instead of making xml scripted actions, you can allow users to script actions on the fly while the game is running using this library? Or you can use it to script your own actions to see what happens in each case. One of the special terminal commands allows you to take a string that contains c# code and evaluate the code. So you could set up some preset actions and have the user choose from them. These ideas may not work for your particular case, but just thought I would give you (and anyone else reading this) some more options to think about.