views:

6158

answers:

12

Hi, I have to program a game engine starting very soon for a 3rd year Games technology project. As a part of our project we have to integrate a scripting language for scripting our NPCs and other elements of the game.

I know that Lua has been very popular in recent years and Python has horrible white space issues, however it is making a comeback in the industry.

From a game developer's perspective, what are the merit's of each and which would you prefer for game scripting?

+16  A: 

If you plan to embed that scripting language in another language like c++, I think that lua is better.

If you plan to write your game entirely with one language, I think that Python is better.

Oli
Yes, Python would be better than Lua. But, I doubt you'd choose either. When implementing a whole game you'd need a language with a good performance profile - something like C# would work much better, and scripting in C# has been shown to work quite well in engines like NeoAxis.
RD1
to RD1: a lot of games are using lua as a scripting language (ex: World of Warcraft) and performance is really good. And it's much more easy scripting a game with lua than with c#.
Oli
Frank
+5  A: 

Both are good, but Lua C API is more pleasant to work with, and Lua is faster (see http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=all).

Kknd
I think what makes a big difference b/w Python and Lua is the size of the interpreter and the memory consumption, not the speed
Mapad
but is IS far faster (like 10-30x)
Javier
Mapad
Mapad has a good point, but speed still is a nice feature (its like 20% faster, see the link on the post).What I like most is the easy-to-use C API.
Kknd
@Kknd "(its like 20% faster, see the link on the post)" The link in the post shows ~2-3x faster.
igouy
Ops, indeed, it's ~ 2-3x faster. And LuaJIT 2 it's ~ 30x faster than CPython 3, impressive.
Kknd
+6  A: 

Lua is a better designed language, faster VM, smaller in codesize and easier to integrate with C

Python has lots of libraries, and bigger audience.

Javier
better designed ? What makes you say that ?
Mapad
Python is really nice, but it's full of special cases. lambda, lexical scoping, closures, coroutines, all are 'almost there' but not fully thought from the beginning. Lua had to break backwards compatibility a couple of times to get them right. python3 is far better, but still lacking.
Javier
+10  A: 

Quoted from this interesting article by Introversion's Chris Delay:

Python is a strong contender. Python has been around for a while, is extremely well supported and documented, and is essentially a fully functioning modern programming language. You could write an entire modern game in python, by writing your core engine in C++ and calling it from within Python. Numerous games make extensive use of Python, including Eve Online and Civ IV. In my experience over the last few days, Python is not particularly easy to embed, and essentially requires additional libraries such as Boost before it becomes genuinely useful to the C++ programmer as an embedded language. Johnny also believes Python to be quite slow, even by scripting standards. It does however have excellent built-in support for Objects, Classes, and all those modern programming features we computer scientists love so much.

Here’s an example Python script, so you can see what the syntax looks like. I’ve never been wild about Python’s style, to be honest. http://www.amk.ca/python/simple/letters.html

Lua is the next contender. Lua is also used by some fairly impressive commercial games, including SimCity 4, World of Warcraft, FarCry, Psychonauts and a ton more. Lua is relatively new compared to Python, and this does cause it some problems. The API is still changing and being rewritten yearly, meaning a lot of the documentation and examples on the internet are already out of date and refuse to work. In my experiments Lua has proved to be much simpler to embed in C++, however it is much more down to the wire – Lua requires the C++ programmer to directly manipulate a shared stack to communicate with your scripts, which is analogous to programming while sitting on a pile of volatile explosives while spiders crawl over your body. Fundamentally Lua is designed to be embedded and controlled by C++, whereas Python wants to embed and control C++ in itself. Lua also falls over itself when you start thinking about classes and Object Oriented programming – it can do it, but only through some tricks and hacks. When combined with a couple of extra libraries – Boost and LuaBind, Lua starts to become much more interesting, and takes on much of the power of Python.

Here’s an example Lua script, so you can compare. I find it to be more “readable” than Python, but that’s just a subjective opinion I think. http://lua-users.org/wiki/SciteCleanDocWhitespace

Ultimately there isn’t much between the languages, and it comes down to requirements and gut feeling as much as anything. Right now I’m going to keep working on prototypes using both languages until one of them does something so stupid that it makes my mind up for me.

I personally can't confirm this, but it is well reasoned and I thought it might help inform your decision.

xan
the 'who controls who' comment is a first sight opinion. after a while, i've found far easier to write C modules than to control Lua from C.
Javier
the 'down to the wire' because stack managing is mostly because he tried to 'control' Lua. it's much better to let Lua do it's thing, not fight it.
Javier
the 'tricks and hacks' to do OOP is because Lua doesn't provide an explicit OOP architecture; rather it lets you write your own on less than 20 lines of code, using the paradigm you choose, be it class- or prototype-based, or something else.
Javier
It must take only a few hundred lines to create your own scripting language
Mapad
Lua: "The API is still changing and being rewritten yearly" - This is not true anymore. And Lua 5.1 is mature enough.
Sébastien RoccaSerra
Thank you all for the interesting comments.
xan
A: 

Personally, I'd choose Tcl. It was designed from the ground up to be an embedded scripting language, and it excels at that. One of its great strengths (beside being dead simple to learn for non-programmers) is that you can easily extend the language. You can create new commands in C or Tcl, and even create new control structures in C or Tcl. Domain specific languages are easy to write.

Tcl is mature, modern, stable, and is being actively developed. It's biggest drawback is it doesn't have "mindshare" and isn't "hot". So, if you're looking at picking a language based on those merits Python is probably the better choice. If you want a language designed from the ground up exactly for what you are trying to do, consider Tcl.

Bryan Oakley
Not to diminish the qualities of Tcl (not sure about the "simple to learn", I only tried once, a long time ago), but Lua is also designed to be embedded and to be extended with C or Lua...
PhiLho
Certainly "easy to learn" is subjective. I based my comment on the fact that Tcl only has 12 simple rules that govern the entire language. Experienced programmers seem to have a tougher time learning Tcl since they typically have to suspend some assumptions they've learned via other languages.
Bryan Oakley
I have embedded both Tcl and Lua code. Tcl is easier only if you want to live in a world where all data are strings. Lua lets you extend the language with new kinds of values. (Tcl's extensible non-syntax is cool but is not *enough* benefit.)
Norman Ramsey
I would see everything you write on Tcl to apply exactly to Lua.
akauppi
I think for an embedded language, a world where "all data are strings" can be a Good Thing. Often (though of course not always) the user of an embedded scripting language doesn't want nor need to get bogged down in data types and whatnot. They want to write a quick macro or define some settings.
Bryan Oakley
+2  A: 

Of course, there is no answer to the question, because there is no "better" language.

As demonstrated above, both languages have pros and cons, and are basically on par. If you need to keep binary small (rarely needed in games, resources are often heavier than binary), Lua is a must. If you need to use lot of libraries, Python can be better.
I don't know for Python, but Lua makes easy to sandbox scripts to prevent them from doing too much... Power can be dangerous, even more if you allow end users to write their scripts (like in WoW).

Beside, it is obviously a very subjective topic, particularly for the syntax: you seem to dislike structure by indenting, some people dislike Lua "verbosity" (as opposed to, say, JavaScript syntax).

If that's a personal project, the choice is your, and shouldn't be driven by trends (although it might count for recruiting).

PhiLho
+3  A: 

Python has more "batteries included". Indeed, some of things for which Lua doesn't "have the batteries" might surprise you. For instance, Lua lacks bitwise operators; if you need to do bit fiddling, you'll have to drop down into C/C++. This may not be an issue anyway, since it will be embedded, but its something to take into consideration.

George Jempty
There are several libraries for that. And it will be native in Lua 5.2, in testing now. (Granted, you wrote this 18 months ago.)
profjim
+58  A: 

The quoted essay is misleading on the relative stability of Python and Lua. Lua is not updated annually and has in fact grown very stable with the passage of time. (There's a very nice timeline in the team's paper on the history and evolution of Lua.)

Both languages are reasonably well designed and provide all the trappings (memory management, objects, first-class functions, string processing) of civilized programming languages. Here are how I perceive the main points of difference:

  • Python has significant whitespace. Apparently you either love it or you hate it. The people who love it find that it streamlines their programs, and people who teach it report that students are more successful because there is less syntactic clutter to distract them from the code. At the beginning, Lua was designed to be usable by non-programmers, and I find the syntax cluttered and heavyweight. I think the other reasons for using Lua outweigh this disadvantage, but I would love to have a version of Lua with lightweight syntax.

  • Python forces objects down your throat, while Lua lets you take objects or leave them. Not everybody loves (or needs) objects.

  • Python is about to undergo major revision. I don't know if or when Python 3 will stabilize. Lua is stable. Morever, if Lua changes and you don't like it, Lua's implementation is simple enough that you can clone it and maintain it forever. I have a major application which since 1996 has been built on top of a customized version of Lua version 2.5. The old code has never been a problem.

  • Connected to the previous point: the Lua team are great engineers. This manifests in all sorts of ways: the implementation is the smallest, fastest, and easiest to understand; the design is coherent; it's obvious how to extend things. If you had to maintain the code, you could. Guido is just a good engineer. I'm not sure even Guido understands all of Python 2.5.

  • Integrating Lua with C is so easy a child could do it. Lua was designed for this also, from the beginning, and it shows. This means that with a few hours' work, any C or C++ library can become a Lua library. For example, last night, I wrote the code needed to make Posix named semaphores a Lua library. Then in Lua I wrote an atomic queue abstraction that I used to balance workloads across a server farm. Elapsed time from the idea to working code was less than four hours.

  • Python ships with more libraries. If you want lots of libraries for Lua, you have to search web sites (luaforge.net) or maybe even convert a C library. The bitwise operators mentioned by another poster are a good example; Reuben Thomas wrote a very nice bitlib for Lua several years ago now, but it is not so easy to find unless you know where to look (or ask on the mailing list).

  • The entire Lua system, with libraries, is sufficiently small, simple, and well designed that you can master the entire system completely. In part this is true thanks to Roberto's excellent book. Python is too complex to master completely and is going to get more complex. Lua's C implementation, including libraries, is under 14,000 lines of code. Python's is 370,000 lines.

I would choose Lua because of its rock-solid engineering. It's over 20 times smaller than Python, does essenetially the same job, performs better, and I know that if my game outlives the current version of the language, I can maintain the current version of Lua forever.

Until I finished this post, I hadn't realized what a Lua partisan I was!

Norman Ramsey
In what way does Python "force object down your throat"?
kigurai
Like it or not, Python has its own model of objects, which it insists on. Lua provides lower-level mechanisms with which you can choose to build a class-based or prototype-based object system, or none at all. Or maybe I misunderstand Python?
Norman Ramsey
"Python is going to be more complex"? You should drop this point from the list, it is silly. The single goal of Python 3 is to remove historical cruft and reduce complexity.
Deestan
Python lets you define functions. It also allows for at least some prototype-like behavior with classes. Python != Java.
Mr Fooz
@Deestan: I stand by my answer. Lua is over 20 times smaller than Python 2.x. If Python 3 is half the size of Python 2.x, I'll be astonished---and Lua will still be 10x smaller. Getting Python to 80% of its current size is a more realistic goal and would still be an achievement.
Norman Ramsey
"Python has significant whitespace. Apparently you either love it or you hate it." I am neutral about the whitespace. I see why Guido did it, but if I was designing my own language I wouldn't have made that choice. Tell Python whitespacing, I said "Hello".
James McMahon
+9  A: 

I can say that in the Game Industry (as opposed to any other software industry out there) Lua means more than Python on your resume. So if you want to become a professional game developer mastering Lua is a big plus, unless you go work on on EvE or CIV (they use Python), however if you plan to go to another industry knowing Python will be more useful to you in the long run.

Now as a programmer, Lua is more portable than Python (as in you can run Lua on practically anything, like an Xbox360 or a GameBoyAdvance, not just the big OSes out there), Lua is smaller and faster than Python, at the instruction level (of course Python will make your programming life more productive if you need lots of libraries). Lua has less primitive than Python, so its easier to learn, but harder to master (you need serious Meta-programming skills to get all the juice out of Lua), etc, etc...

Conclusion, as a game developer myself I'd recommend Lua.

Robert Gould
+1  A: 

Just for future reference there is a nice script language which I have embedded a couple of times that have a small footprint and executes relatively fast called AngelScript.

http://www.angelcode.com/angelscript/

It has a C/C++ like syntax and is designed to be easy to integrate with your C/C++ code. My experience with it is that it is extremely lightweight and fairly straight forward.

Subtwo
+4  A: 

Years and years ago Lua was the embedded language of choice for embedding in game engines, but started to lose market share to Python simply because it was easier for 3d modelers to write Python code.

In other words, your so-called "horrible white space issues" were a good thing for artist types.

That said, I know that attempts at embedding Python into main event loops in the types of games that needed 3d modelers have generally not worked as well as Lua.

I don't know where things stand now, but I would guess Lua is back to where it was in market share, especially as Python focused more on general applications (as opposed to being embeddable).

Van Gale
+1  A: 

(Responding to old post, but still...)

The lack of full coroutines (generators and lambdas are NOT coroutines) is what decided me. A game scripting language often has to do AI in a per-frame loop, where you make PART of each decision each time a frame "ticks." The cleanest, best paradigm for doing this (IMO, and in the opinion of most on AIGameDev.Net) is the Behavior Tree pattern.

Behavior trees depend upon coroutines for cleanest implementation. You can definitely write a functional behavior tree using nothing but generators (and, I've done this in both C# and Python), but the overhead of remembering where you can and can't yield is unpleasant and forces you to think more about the language than about the behavior you're representing. Lua's coroutine.yield() -- which works ANYWHERE, not just in a generator -- produces a full symmetric coroutine implementation that makes behavior trees a joy to work with.

Thus Lua is better for use in game situations where you can't just run forever, but have to carefully parcel up your work per time-slice (e.g., per frame), and resume where you left off on the next frame.

Note: I know that you can design around these limitations through the use of things like blackboard objects, and I know that generators can produce an implementation of behavior trees. My point is that this design pattern comes naturally and almost without cost in Lua, whereas in other first-order embeddable languages (Python, C#), you have to do a fair amount of work in the game scripts to support it. That transparency is what decided me to choose Lua over Python (and I'd wager what is leading other big game shops like Blizzard to make the same choice).

As a standalone app language, I'd take Python ANY DAY for its rich library support and out-of-the-box object model. As an embedded game language, Lua wins -- hands down.

Tom Canham