Skip to main content
gamedev.net
🔒 Locked

Using Lua with C#

Started by Myopic Rhino Nov 1, 2005 at 11:25 PM 33 replies 15.4k views
BradSnobar
BradSnobar
Thanks, this is awesome and could save me a bunch of work.
BradSnobar
BradSnobar
I was looking at Iron Python too though. It is written natively in C# and so might be embedded easily too.

Has anyone used python? How easy is it to use compared to lua?
Mite51
Mite51
Why would you want to use lua in C#? From C# you can use other C# files as scipts and not have to build bridge functions or deal with yet another GC...

J
jpetrie
jpetrie
Interoperability with existing code or scripts, perhaps.
Vexorian
Vexorian
Yes, but some developers would like to give the users a more flexible language like lua for example. And lua is used by more and more applications for a scripting language.

Although I am the kind of person that would preffer c# over lua for scripts, mostly because I am a programmer that feels much more confortable with c like syntax and a strongly typed environment
------ XYE - A new edition of the classic Kye
acid2
acid2
Although this is a nice, well writen article, I also don't see the need for Lua in .NET. I personally use boo to script (woot for boo!)
Oluseyi
Oluseyi
Quote:
Original post by BradSnobar
I was looking at Iron Python too though. It is written natively in C# and so might be embedded easily too.

There's no need to "embed" Iron Python; as a first-class .NET language, it can consume assemblies generated by other .NET languages and generate assemblies for consumption by other .NET assemblies seamlessly.

Quote:
Has anyone used python? How easy is it to use compared to lua?

Python is bigger than Lua, though generally slower as well. It has far more functionality, being a standalone language right from its original conception, rather than an extension/data configuration format like Lua. The traditional trade-off has been the feature-richness of Python for a performance hit and difficulty integrating with C++ or the bare-bones simplicity of Lua for a cinch to build. With Iron Python and the CLR, the integration becomes a non-factor, leaving you with robust features, and since your code can be JIT compiled just like C#...

By default, though, your code is interpreted, which makes it a very quick way to prototype and/or develop code that requires a lot of hand massaging.
Balaam
Balaam
I use Lua and C# together a lot - I think they're a great match :D
It's very easy to generate lua code on the fly and use lua's tables for storage. Also the first class nature of the functions makes some stuff much simpler to write.

(I also like passing back more than one return value - it's make many things that are usually tricky just dissapear)

And I've written a much more basic introduction here.

[Edited by - Balaam on February 7, 2006 6:44:41 AM]
I'm writing a book about How To Make An RPG, check it out! | Blog
Penance
Penance
You'd use LUA instead of C# code because you might need to reload the script at some point while the program is still running. In the .NET framework there's no way to unload assemblies unless you create a new application domain and then destroy it when you're done. Very wasteful and hackey.
mascarenhas
mascarenhas
Hi, I am the author of LuaInterface, the library this article uses. Previous releases of LuaInterface have focused on making it easier to use .NET assemblies from Lua, as this is the use case I thought would be more common (building prototypes, using .NET as a GUI for Lua programs, writing computation-intensive code in C# for speed, and calling it from Lua, etc.), but it appears there is a lot of interest in embedding Lua in .NET applications! I am working on the next release, and will include the suggestions in this article, and hopefully make LuaInterface even better for the embedding folks.

--
Fabio Mascarenhas, Lablua, PUC-Rio
Penance
Penance
I'm using LuaInterface for my .NET game engine project.

So far it is working well for me and native <-> lua interactions work great.

One thing I found annoying was figuring out the compat library stuff. Documentation on how to get .NET assembly/type importing to work could be better. Right now I just have the compat library in the same directory as my exe and I basically messed with stuff until it worked.

Anyway, I'm looking forward to future releases! Don't give up on this project, it has great potential.

SilentDtH
SilentDtH
I enjoyed this article - thank you very much. I am a beginner to game development and have been researching thoroughly before i even start my first Tetris clone.

I have made a little mod for World Of Warcraft which uses LUA. I guess if these guys use it it's gotta be good ;)
Lantz
Lantz
Quick comment, having non-stateless stuff static doesnt make for good reusability. What if you want more than one instance of the lua vm?

Edit: Also, I would name the attributes the way microsoft's named the ones in the dotnet framework. For example, I'd name AttrLuaFunc LuaFunctionAttribute, which would mean it can be used as [LuaFunction(...)]. A lot more elegant.

[Edited by - Lantz on November 14, 2005 7:58:08 AM]
Rob Loach
Rob Loach
Very nice article, well done. I'll have to give it a longer check out once I get the time....

[Edited by - Rob Loach on December 4, 2009 12:05:20 PM]
Rob Loach [Website] [Projects] [
kornman00
kornman00
Nice article, but you don't really stick with C#\.NET coding standards that are used widely in the .NET community (as someone else pointed out, fulling naming a class with Attribute instead of Attr).

Let me guess, you come from a java background ;p.
kornman00
kornman00
Quote:
Original post by Anonymous Poster
It's one of the dumbest things I've ever read. So, ignoring that Lua is a complete pile of crap language, IMO, what's the point? You can compile and run C# code at run-time, and reload the assemblies at will making C# into a scripting language.

Now by using Lua with C# you're using a scripting language within another scripting language. People that have to maintain your crappy code now have to learn two languages one of which is a complete pile of trash.

Just because Blizz uses Lua in a hugely popular game doesn't mean that it's a great language; it just means that somebody's experiment got published.

Wow! That's smart!


First of all, C# isn't a "scripting language". Don't be ignorant and stupid at the same time.
C#.NET is compiled into ILasm, which the .NET Virtual Machine then interprets at runtime. VB.NET, C++.NET, etc, all get compiled into the same code, ILasm.

Second of all, allowing a game engine have the fuctionality of all of .NET's framework is a security risk. With a scripting language, you get to choose what is compiled and what not.
If a game engine is going to allow modding by end users (and not just developers, like 'programmers'), Lua would be an appropriate scripting language to use, since its used in other games (like WoW I believe someone said). Your engine's reply value depends on your players, and if your players know how to create new content, they're going to want to do more with it (assuming your game is actually fun.

The guy took some time to at least write a decent tutorial, try giving some constructive criticism next time.
kornman00
kornman00
Quote:
Original post by Anonymous Poster
2kornman00:
You have to learn .NET before making such considerations.
.NET does NOT inrerpret IL code at runtime !!! Java does, .NET does not.
Instead, .NET framework does compile IL code into native machine code with JIT compiler and then execute it. This give a perforance like you write entire code in C++ !

Whats your definition of "interpret" :p

It still has to interpret the IL code once to turn it into native code at runtime.

Quote:

Each method for which Microsoft intermediate language (MSIL) has been generated is just-in-time-compiled (JIT-compiled) when it is called for the first time, and then run.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.