views:

252

answers:

5

Hello, 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?

A: 

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.

Sean A.O. Harney
+2  A: 

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.

Greg Hewgill
I agree totally. The biggest problem I can for see is the lack of Reflection.Emit on WP7. Is there a way to do LUA in .Net without Reflection.Emit or Native Interop?
Well there's LuaCE (though it's not managed). http://github.com/ynezz/luace/downloads
ctacke
+2  A: 

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();
    };
}
Andrew Russell
I'd like them to be interpreted at Runtime, to allow the engine to be a bit more dynamic. Our current design is content driven. I looked at using C# as a scripting language, but once again, WP7 is lacking CodeDom and Compiler Support, which is what would be ideal. I even looked at Boo and Lua. Issues with Reflection.Emit there. Thanks for the links. I'll read up
You raise an interesting point. On XNA/Xbox 360 games can't be modified or access external content - so you may as well precompile there anyway (it's what I would do in your situation). But it seems you *can* pull data from the Internet on WP7. My question is: Is there an actual business case behind "a bit more dynamic"? Do you intend to deliver content via the Internet? Will this fall afoul of Microsoft's content approval policy? Would you be better off going through the MS-approved update mechanism? (And technically: If you go ahead with this, is it possible to distribute precompiled DLLs?)
Andrew Russell
I've been wondering that myself. We're in the Imagine Cup, and as such want to have a flexible engine for competitions sake. I feel it would help give us an edge if we can rapidly release content. That's the reason we dropped XBox 360 early on. We need full support for dynamic code and Windows Azure, and XNA couldn't provide that on the 360. I suppose with System.Reflection or MEF I could use precompiled DLLs, but I dunno if that would be supported on WP7
I'm still not fully understanding the need for rapid releases. A competition generally has only a single "release". And Microsoft have already built an update mechanism - so I don't understand how building an orthogonal would get you more points in the judging. (I'm similarly confused as to why using Azure is a good idea for the competition?) Do you really mean rapid *development*? If so: it only really needs to be "rapid" on Windows. Just do a straight build for Phone (or Xbox for that matter).
Andrew Russell
I suppose I was focusing too much on end user modding and making the team's life easier. The competition is broken into three rounds, and in some of the rounds, you have 24 hours to add features to the game. I haven't seen anything about the Microsoft Update system, but I think it'll be similar to the XBox XNA process. I certainly hope that's not the case. Azure is going to be used for putting game saves, and other data in the could. (Mods, etc.)
Wait - end-user modding is yet *another* requirement! I think you really need to pare this down to what you really need for the competition. If the only thing you need is the ability to make changes inside 24 hours, then I think my answer is more than suitable. If you want to allow end-users to write scripts for WP7 - that is a *completely* different problem space.
Andrew Russell
A: 

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

bearcdp
A: 

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.

BluePlateSpecial
I would love to use this library, but I don't plan on licensing this project as GPL. Thanks though, that's a great resource :)