I am reading a book on Game Development in which they show you how to make scripts for your game in Lua. They program Lua and use it from C++, but I was wondering if there is a library or API to use Lua from C#: specifically from XNA to develop games for PC and XBox 360?

A kind of small nitpick, but it's Lua (a name), not LUA (an acronym, that or you're shouting)
–
RCIXJul 24 '10 at 23:43

From the Lua "About" page: "Lua has been used to extend programs written not only in C and C++, but also in ... C# ...". First result from Google for "lua in C#" was an interesting article from GameDev.net (gamedev.net/reference/articles/article2275.asp) titled Using Lua with C#.
–
reverbbDec 14 '10 at 7:50

Lots of .net devs use ironpython or ironruby for scripting.
–
WillDec 29 '10 at 22:55

7 Answers
7

This is were I would point you (http://www.xnua.com/xna_lua_xnua), but it hasn't been updated for a long time.

C# is great language, and could in a sense be used as a scripting language within your c# game. Nick Gravelyn has created some nice tutorials about tile engine (http://blog.nickgravelyn.com/downloads/#tileengine) and used a custom scripting engine to fire off dialog. He has also has a basic example when he learned about the "yield" keyword: http://blog.nickgravelyn.com/2010/02/the-magic-of-yield/

Some good points here. One thing to think about, is why do you want a scripting language when your game is written in c#? C++ engines can gain alot of agility from using a scripting language, as C++ typically takes a long time to compile. C# is pritty quick in this regard. If for some reason you want your scripts compiled at runtime, you could always use CodeDom (or any other code generation methods) to compile a CS file at load time, but this would only work on PC. Theres a chapter on this in the book "Game Engine Toolset Development"
–
Cubed2DJul 14 '10 at 20:13

^^ Game Engine Toolset Development is a nice book :). It's a bit outdated now, but the concepts are still solid. The problem with .NET scripting language bindings is that, in order to run on the Xbox 360, that they pretty much have to be interpreted. Without System.Reflection.Emit, you can't do much in the way of code generation. I was under the impression that these capabilities would be coming to the Xbox 360 in the near future, but based on the comment discussion in my reply below, this is probably not the case. Thus, script performance on the Xbox 360 probably won't be so great :(.
–
Mike StrobelJul 16 '10 at 15:35

@Cubed2D: Lua is a sort of industry standard for scripting games, and it's also easier for the non-professional programmer to understand.
–
RCIXJul 24 '10 at 23:46

@Mike your right, can compile or generate code on the 360. If i was going to implement this i just wouldn't support c# scripts on 360, and just have it as a pc toolset feature. Just tweak the vs project to include the scripts folder on a 360 build and ignore on a pc one. @RCIX just because everyone else does it, dont mean you should. If you want to use Xna, its going to be more trouble than its worth. There are better ways to expose tweakable parameters to non programmers in the .net world trying to implement lua(what i assume your getting at with your non pro-programmer comment)
–
Cubed2DJul 25 '10 at 0:16

@Cubed2D: That's a good point, but it really depends on your target platform. if you're writing just for the PC, then you can use LuaInterface and the native lua dll, and not have to write an implementation. Besides, the point is that since it has such a following in the game development world, it's naturally developed features that make it good for that environment. Moreover, i got my main start into programming through Lua, and i bet many people did, so it's understandable why people want to use it.
–
RCIXJul 26 '10 at 8:54

If you're looking for scripting in C#, definitely take a look at IronPython. It's not just a python API for C#, it is actually a completely separate python implementation which compiles to the same CLR assembly (with the same performance) that C# does, but can be interfaced with as a scripting language. Any Lua wrapper for C# would be much slow due to the overhead of Marshalling.

As Sean said, IronPython is worth looking at. IronPython is actually one of a few dynamic language implementations for .NET targeting the Dynamic Language Runtime (DLR). There is at least one open source project aimed at implementing Lua on the DLR (like this one), but I'm not sure if there are any implementations near completion. A well-implemented set of DLR bindings for Lua should provide performance on par with IronPython and IronRuby, and would be much faster than a Lua wrapper. For the sake of comparison, IronPython generally performs better than CPython (sometimes significantly better).

Note: As ElementCy states below, DLR languages are generally not supported on the Xbox 360. However, there is an "interpreted mode" in the DLR. I don't know enough about it to say for sure, but it might be possible to get IronPython (or other DLR language implementations) to run on the Xbox 360 in interpreted mode, though performance would certainly suffer. It might be worth asking someone on the DLR team whether they know this to be possible. If not, the Xbox 360 should get Reflection.Emit support as part of upcoming Silverlight support. If and when that happens, I imagine getting the DLR up and running on the Xbox 360 won't be a problem.

Only problem with IronPython, it wont run on the xbox360. It is fine if you want to use it for PC only. IronPyton uses System.Reflection.Emit a lot, and that is not available on the Xbox's .net Framework.
–
ElementCyJul 14 '10 at 20:39

Dont think xbox is getting Reflection.Emit any time soon, as winPhone has both xna and silverlight and does not support that namespace
–
Cubed2DJul 14 '10 at 23:42

1

You could be right. WP7 does not actually run Silverlight, but "Silverlight Mobile Compact Framework", which is based on .NET CF instead of .NET. I believe the Xbox 360 also runs .NET CF instead of the full .NET Framework, so it's likely to end up with a similar implementation to that of WP7. Oh well.
–
Mike StrobelJul 15 '10 at 3:19

The best solution I could give you is to just script in C#. You can compile them at runtime when you target windows and compile them before hand for the xbox. (as a dll)

That would be the easiest solution. You could also do it in VB.NET or any .NET language. (not sure about the scripting ones though) Basically, I'm saying to have your scripts in their own dll. You can have them compile at runtime on Windows. (do you really want them to do this on the xbox 360?, you can't update your games on there, at least I don't think so)

I can understand your frustration, I wanted to use Lua as well.

To sum it up:
1) Have it built at runtime on Windows
2) Build it before hand for the Xbox 360

I would've said first point is untrue, since there are several links here that have Lua<->C#.
–
The Communist DuckDec 13 '10 at 18:43

You can only run managed code on the Xbox. A Lua <-> C# bridge is unmanaged code allowing you to call Lua from managed code; until MS includes it in XNA or the Xbox 360 OS, you can't run it there.
–
user744Dec 13 '10 at 19:05

Just to update this question, I spent the last day doing some extensive searching, and I've found that using UniLua (made for use with unity, pure C# implementation) is the best and most effective option for getting Lua on Xbox. I stripped it down, took out all the Unity material, and it works great.

I also added some functionality found in LuaInterface by overloading the [] operator for LuaState, and such.