views:

1347

answers:

8

Hi,

I'm playing WoW for about 2 years and I was quite curious about Lua which is used to write addons. Since what I've read so far about Lua was "fast", "light" and "this is great", I was wondering how and when to use it.

What is the typical situation where you will need to embed a script language like Lua in a system ?

+2  A: 

Rapid development for application with real-time constraints. Computer games are one of these ;-)

vartec
other non-game apps using Lua both for extensibility and for making it easy to develop/refactor: apt, nmap, monotone, Adobe LightRoom, love2d, wildpockets
Javier
+2  A: 

It's a valid solution if you want to allow third parties to develop plug-ins or mods for your software.

You could implement an API in whatever language you are using, but a script language like LUA tends to be more simple and accessible for casual developers.

Rafa G. Argente
+11  A: 

When you need end users to be able to define/change the system without requiring the system to rewritten. It's used in games to allow extensions or to allow the main game engine to remain unchanged, while allow content to be changed.

sfossen
+3  A: 

Lua is:

  • Lightweight
  • Easy to integrate, even in an asynchronized environment such as a game
  • Easy to learn for non-programmer staff such as integrators, designers and artists

Since games usually require all those qualities, Lua is mostly used there. Other sitation could be any application that needs some scripting functionality, but developers often opt for a little more heavy weight solution such as .Net or python.

Coincoin
+8  A: 

Embedded scripting languages work well for storing configuration information as well. Last I checked, the Mozilla family all use JavaScript for their config information.

Next up, they are great for developing plugins. You can create a custom API to expose to the plugin developers, and the plugin developers gain a lot of freedom from having an entire language to work with.

Another is when flat files aren't expressive enough. If you want to write data driven apps where behavior is parameterized, you'll get really tired of long strings of conditionals testing for config combinations. When this happens, you're better off writing the rules AND their evaluation into your config.

This topic gets some coverage in the book Pragramtic Programmer.

Ryan Graham
+1  A: 

In addition to the scripting and configurability cases mentioned, I would simply state that Lua+C (or Lua+C++) is a perfect match for any software development. It allows one to make an engine/usage interface where engine is done in C/C++ and the behaviour or customization done in Lua.

OS X Cocoa has Objective-C (C and Smalltalk amalgam, where language changes by the line). I find Lua+C similar, only the language changes by a source file, which to me is a better abstraction.

The reasons why you would not want to use Lua are also noteworthy. Because it hardly has a good debugger. Then again, people hardly seem to need one either. :)

akauppi
A: 

In addition to all the excellent reasons mentioned by others, Embedding Lua in C is very helpful when you need to manipulate text, work with files, or just need a higher level language. Lua has lots of nifty feature (Tables, functions are first class values, lots of other good stuff). Also, while lua isn't as fast as C or C++, it's pretty quick for an interpreted language.

+2  A: 

a scripting language like LUA can also be used if you have to change code (with immediate effect) while the application is running. one may not see this in wow, because as far as i remember the code is loaded at the start (and not rechecked and reloaded while running).

but think of another example: webserver and scripting language - (thankfully) you can change your php code without having to recompile apache or restart apache.

steve yegge did that thing for his own mmorpg engine powering wyvern, using jython or rhino and javascript (can't remember). he wrote the core engine in java, but the program logic in python/javascript.

the effect of this is:

  • he doesn't have to restart the core engine when changing the scripts, because that would disconnect all the players
  • he can let others do the simpler programming like defining new items and monsters without exposing all the critical code to them
  • sandboxing: if an error happens inside the script, you may be able to handle it gracefully without endangering the surrounding application
Schnalle