Lua with C++

I've heard people talk about Lua as a scripting language and how useful it is, but I've also heard people say that they integrate Lua with their C++ programs. What are the advantages to doing this? Why do I want to do that (if I even do)?
For example, imagine a batch image processing program. It is able to load an image, perform several transformations on it, and then write it back to disk. Such a program could use Lua to load scripts that tell it exactly what type of transformations to perform and in what order to perform them. Otherwise the programmer would be forced to either design his own language, or use some other method to give instructions to the program.

Another, more common application is game engine programming. Most game engines use some sort of scripting language that defines most aspects of a game. A script for a game could look like this:
1
2
3
4
5
system.add_archive("archive.zip");
sprite=load_sprite("sprite.png");
sprite.show(0,0);
wait(1000);
sprite.unload();
Again, Lua can help you avoid reinventing the wheel.
How do you know when to use scripting for a certain rather than using normal old C++? I can't think of a situation where C++ couldn't be used. Scripting languages run slower than compiled languages, so when is it better?
Just because you can use a particular language to accomplish something, doesn't mean you should. You could write everything in Assembly, but should you?

In my example above, the time spent in the interpreter figuring out what that hypothetical script does is dwarfed when compared to the time spent in the engine actually performing the operations described in the script. The speed of interpretation only starts to be relevant when your script gets very, very complex.

C++ also has the problem that it has to be compiled and linked to be used, neither of which are trivial operations. Would you really use the program in my first example if you had to compile a source just to perform batch operations on images? Maybe the first time you would, but what if you had to run one script, and then a different one, and then another? Scripting works great when the code is expected to change often.
One more use case is executing commands in real time from a console. This is unwieldily complex in compiled languages, but straightforward in interpreted languages.
Oh sweet. Thanks. :) Makes much more sense.
I've been thinking about C++ and Lua lately, but I don't think I've ever made an effort to learn Lua and use it with C++.
We provide optimization web services, i.e. people send us data through a web service and we optimise it and send it back. Not everybody sends data in the same format, or wants the same results back. Our optimizer is written in C++, but we implement the data loading with lua scripts.

We have multiple VMs running optimisers, and the system is designed so that we can upload a new script to a cloud service and then all new requests to all the VMs (for users of that script) will use the new script, while all current/old requests will continue to use the old script. Users on different scripts are completely unaffected. I'm not sure how you could implement anything similar without some kind of scripting language.

Can you imagine if we had to do a full build and deployment each time a new customer came along, or a current customer wanted something slightly different?
Topic archived. No new replies allowed.