Sign in to follow this  
gameplayprogammer

Why use lua?......

Recommended Posts

i was looking to generate .h/.cpp files that stored my game constants and some utility functions. I was going to write a c++ application that did this for me and put the files in the source code directory. I notice that alot of games companies use lua, so i thought it would be good to get experience using it. After looking at the syntax i am wondering, why use it? Is it for people who don't know how to write c++? Is it to decrease build times everytime you change something in code? Any industry veterans out there explain why it is in wide spread us....

Share this post


Link to post
Share on other sites
Lua is a scripting language.

Scripting languages are used to create program sequences beside the sequences you build in C. They are compiled on the fly, which means you can simply change the scripts and rerun the applications without the need of a compiler or linker. It also enables that you have a scripter and a programmer working seperately.

Scripting languages are less efficient, so don't use them for heavy loops, but for GUI's or w/e kind of effect / AI you can use it easily.

Only problem I find is binding it with C, but I've seen some variations on Lua that had better? bindings with C / C++.

PS: another point, a MOD community can easily make mods with scripts! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius

Scripting languages are used to create program sequences beside the sequences you build in C. They are compiled on the fly, which means you can simply change the scripts and rerun the applications without the need of a compiler or linker.


You can change scripts while the application is running. The need to restart is suboptimal.


This enables trial-and-error coding. Many "programmers" can hack-and-slash together pretty useful pieces of code (see script kiddies), but the process of formal software development represents a huge barrier to entry for them.

Another advantage is that these languages tend to be error-tolerant, and are devoid of almost all CS concepts (memory management, data structures, algorithms). Script languages are generally also well suited for copy-paste coding.

If the interface between scripts and application is solid, then scripters will have really hard time breaking anything, while still getting solid results.

Perhaps the most prominent example of this is the wow add-ons, which also puts realistic performance of scripting into perspective.

Share this post


Link to post
Share on other sites
Lua is actually really easy to sandbox, because of the relatively explicit order-of-loading. You can wrap the standard library with your sandboxing code before loading any more scripts into a lua context, and do things like install checks against opening files outside of the game data directory, or even remove the standard library file library completely, and provide only your own data and registry API to the script.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
Original post by Decrius

Scripting languages are used to create program sequences beside the sequences you build in C. They are compiled on the fly, which means you can simply change the scripts and rerun the applications without the need of a compiler or linker.


Another advantage is that these languages tend to be error-tolerant, and are devoid of almost all CS concepts (memory management, data structures, algorithms). Script languages are generally also well suited for copy-paste coding.


I do not agree with your assesment there. I was just looking at the wiki page and it lists the following: first-class functions, garbage collection, closures, proper tail calls and coroutines. It also has the map as its basic data structure. These are certainly CS concepts and exposed far more cleanly and graspably than in C++ (which can only emulate most of these CS concepts with its powerful template programming system).

Share this post


Link to post
Share on other sites
Quote:
Original post by Daerax

I do not agree with your assesment there. I was just looking at the wiki page and it lists the following: first-class functions, garbage collection, closures, proper tail calls and coroutines. It also has the map as its basic data structure. These are certainly CS concepts and exposed far more cleanly and graspably than in C++ (which can only emulate most of these CS concepts with its powerful template programming system).


Of course you can use some of the advanced concepts.

But consider the typical C#, Java model. First, there's interfaces vs. classes. Then there's List and Iterator (Enumeration, and similar) concepts. Then there's various foreach vs. for iterations, there's the pass by value vs. pass by reference (depending on language), and much more...

For programmers, that's utopia. But all of this is completely redundant from scripting perspective.

Consider the "object" as dictionary concept.

player = {}
player["health"] = 100
player["mana"] = 100
player["quests"] = {}

None of the:
public class Player implements Comparable, Serializable {
private long UUID = 0x44297862357860348; // or whatever goes there
private int health = 100;
private int mana = 100;

public int getHealth() { return health; }
public int getMana() { return mana; }
public boolean equals(Object o) {
// LOTS of code
}
public int hashcode() {
// LOTS of code
}
public String toString() {
return
"Mana: " + mana + "health: " + health + ....;
}

}


Neither of the code is exactly correct, but in first case, there's absolutely no mental overhead. You-code-as-you-think. In later case, everything bold is just implementation details.

Granted there's limitations. But for many users, the benefits of more structured languages are just redundant.

This is part of the reason why Python has been gaining momentum.

Share this post


Link to post
Share on other sites
I agree with that but my disagreement - that they do not allow much ("devoid of almost all") CS - has not changed. For the same reasons you mention you can introduce CS concepts with little overhead in a scripting languages. Theres nothing stopping you from using CS concepts when using a scripting language, it makes programming in them even more effective. The tools are present. That even handedness is what makes them good, lenient for the beginner, advanced enough for an experienced person.

I Mean being forced to type all that boiler plate does not make it proper programming in my eyes. Imagine if you had to solve a large set of non linear partial differential equations in thousands of variables every time you had to walk or shoot a basketball. Abstractions are useful to everyone, expert programmer or casual scripter.

Id place python above a mere scripting language. It is a quite expressive programming language. Python is so popular because of it is freely typed nature, adoption of lots of modern CS features and its powerful reflective capabilities. It is because of these reasons that you can code off the top of your head seemingly. But you can also do some really advanced 'C.S. c' things in it though the style seems to still be based around trial and error err 'tests'. Though I wonder how well it scales for large projects.

Share this post


Link to post
Share on other sites
Hey guys, I figured I had something relevant to say on the Lua subject.

After just completing a project where i used Lua/Luabind for all of my Ai behavior; I come away saying that there is no way i would use it again. The major problem that i wasted at least a week of development time on (three weeks total development time) was stupid errors. When i think about it, its more of a Luabind problem, but what a problem it was.

I couldn't debug line to line and when the script did run into a problem it threw an error that told me nothing except that there was something wrong. The amount of time it took to hunt down these errors was disgustingly bad.

Writing the same code in C++ I would have had compile-time error checking, faster code (the game started to lag nearing the end of dev.) and if it did break at run-time, I would have been told where and why it had broken. All for a little more compiling.

/endRant

I guess the thing to take away from this post is to be wary of not nessacerally Lua but definitely Luabind.

Share this post


Link to post
Share on other sites
Quote:
Original post by Trandafira

I couldn't debug line to line and when the script did run into a problem it threw an error that told me nothing except that there was something wrong. The amount of time it took to hunt down these errors was disgustingly bad.


Quote:
I guess the thing to take away from this post is to be wary of not nessacerally Lua but definitely Luabind.


It helps to implement your own stack walker for this very case. Given high level of meta data in LVM, that's somewhat trivial to achieve.

The same is true for other VM-based systems, where errors can be reported reliably and with adequate information, perhaps even full VM state dump.


The problem with run-time detection of errors however remains, but that can go either way, as a benefit or a problem.

Share this post


Link to post
Share on other sites
Thanks for the replies.

Trandafira..bit worried with what your saying, so i will hold off using it for my current project, think i will use txt files that people can edit and write some code that interprets these txt files.

example --

say i include a file called change_character_name.txt in a mods folder somewhere in the application directory.

then the user can open it up and add a line of text like...

"boogieman" change_to "monsterman"

of course there would be a help file somewhere with a list of acceptable syntax.

As for game constants i have written a c++ program that does that instead.

Still WOW wouldn't be using lua if it didn't offer something over txt files.

Share this post


Link to post
Share on other sites
Quote:
Original post by gameplayprogammer
Thanks for the replies.

Trandafira..bit worried with what your saying, so i will hold off using it for my current project, think i will use txt files that people can edit and write some code that interprets these txt files.

example --

say i include a file called change_character_name.txt in a mods folder somewhere in the application directory.

then the user can open it up and add a line of text like...

"boogieman" change_to "monsterman"

of course there would be a help file somewhere with a list of acceptable syntax.

As for game constants i have written a c++ program that does that instead.

Still WOW wouldn't be using lua if it didn't offer something over txt files.


That solution sounds much worse.. you'll have all of the problems that Trandafira mentioned plus more. Plus it's a huge time sink. But anyway, have fun.

Share this post


Link to post
Share on other sites
Quote:
Original post by Trandafira
I guess the thing to take away from this post is to be wary of not nessacerally Lua but definitely Luabind.


The other thing to take away is your inexperiance with Lua vs C++.

Lua has a very complete debugging system; allowing for callbacks when the VM executes lines, when functions are called and return and after a specific number of VM instructions. It also has a very complete introspection library allowing for you to inspect the state of the VM during debugging.

That said, I wouldn't be surprised if most people ignored Lua's debugging system so you aren't alone here. However properly intergrated it can be a very useful tool for tracking down and debugging errors in your code.

As for the speed of the code, well this is where profiling comes in and if you discover a slow down you can always bring the code up into C or C++ for the speed boost. However it is always going to be easier going from Lua to C++ than the other way around.

Share this post


Link to post
Share on other sites
pinacolada, i think you need to read Trandafira post again.

Trandafira wrote
"Writing the same code in C++ I would have had compile-time error checking, faster code (the game started to lag nearing the end of dev.) and if it did break at run-time, I would have been told where and why it had broken. All for a little more compiling. "

Share this post


Link to post
Share on other sites
Yeah I know. You said your plan was to "use txt files that people can edit and write some code that interprets these txt files." So essentially, these text files are going to be really simple scripts.

Are you going to have compile-time checks that verify that these files are properly written? If an error is caused by something in one of these text files, are you going to have something that prints what line the error occured on?

And if your answer is, "I won't have to worry about that because these scripts will be really really simple", then I'd say, you can also avoid those problems if you write simple scripts in Lua. And you won't have to spend time writing a parser and an interpreter.

[Edited by - pinacolada on July 31, 2008 12:08:12 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this