Home » Community » Forums » » An Introduction to Lua
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic

Page:   1 2 »»

 Last Thread Next Thread 
 An Introduction to Lua
Post Reply 
Very interesting article, and written in an easy, accessible manner. Well done!

How does Lua do as far as object-orientation is concerned? How easily can Lua script code be bound to invoke methods of specific or general instances of a class? What design patterns have been developed to address the problems of structuring such embedding projects?

Cheers.

 User Rating: 2027   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

All I can say is Wow!! Very good work.

Up to this point I've always thought that adding a scripting system to my program would be difficult, but its obviously not.

I do have the same questions as Oluseyi though.

Thanks a lot.

 User Rating: 1084   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Earlier in the news here at game dev there was a article about LUA Bind, this is what will be used for C++ object oriented connections.. its only in a BETA stage, I have not used either Lua or Lua Bind I have just researched them and I beleive I'll be implementing them into my future project..

This article is a very good article for someone trying to figure out what a scripting langauge can do for them.. it brings you to that prefect point where you know if its for you or not.. you know the general process of it all and your ready to get into the nitty gritty and decide how you will use it..

(This is what this article did for me when I found it on the net outside of Gamedev)

Well Done

Please visit Turt99 Productions

 User Rating: 1115   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Great article. I always thought that integrating a scripting system would take to long, and be too hard for me to understand at this point, but that article cleared it all up, and I am going to go back to one of my earlier projects and mess around with this a bit. Thank you for the wonderful article.

------------------------------
p.s. excuse me english. im from L.A.

 User Rating: 1008   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

This was a very good and fun article to read. My hat is off to you sir! I will be holding my breath until you next article.
(Please dont be to long )

I've currently been using SeeR for scripting for my game.
However, SeeR, afaik, doesnt have a community, so its
difficult to get help and support.

Lua looks to be a very good language, and has a large community for help and support, and i thank you for opening my eyes to it


PSST!

LuaBind

Also found ToLua



[edited by - JackRabbit on April 30, 2003 4:50:48 PM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Wow. I sent the article in about ... oh, I dunno, 3 months ago. I thought it had fallen off the face of the planet.

Yeah, another article ... yeah I guess I should work on putting another one together to talk about luaBind (or toLua) and Lua 5.0



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by IAmEvilHomer
Wow. I sent the article in about ... oh, I dunno, 3 months ago. I thought it had fallen off the face of the planet.

Yeah, sorry about that Ash. Over the course of about 8 months I built up a big backlog of article, and it was somewhere in the middle. As soon as I realized we had an article from you waiting in the queue, and on a subject people have shown a lot of interest in recently, I pushed it to the head of the queue. The delay won't be as long next time.

 User Rating: 2088   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

It's always a pleasure to see articles about Lua.

Nice job Ash. I was wondering why you didn't write about Lua 5.0, which has been released in its final version early this month. But I've already realized that from the last two posts.

For those not aware: Lua 5.0 features, among other things, native support to multithreading and a very simple but effective coroutines API.

Coroutines allow one to execute multiple tasks within a script with the illusion that they're executing concurrently.

They're also useful to export blocking functions to Lua script and not have your application thread block (such as a Sleep(), GetTarget() etc for your scripts).

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

In regards to:

int l_addNPC( lua_State* luaVM)
{
   theNPCManager->AddNPC( lua_tostring(luaVM, -1) );
   lua_pushnumber( luaVM, 0 );
   return 1;
}
 


If theNPCManager->AddNPC takes no arguments, do I still need to call lua_tostring in order to move the stack along? I've got something like this, just a simple information returning function:

void		l_getServerState(lua_State *vm){
	lua_pushnumber( vm, server->getServerState() );
}
 


Thanks for any help.

 User Rating: 1015    Report this Post to a Moderator | Link

Not to bash Lua or anything but, some arguments you put in the intro are just not very valid. You really put it like it was impossible to work without it.

With C++:

"Brandon notices a couple of issues in gameplay that don't quite work. So Joey goes back to the code, makes the changed and re-compiles. This process is going to take a day, at the very least, unless it's a trivial change. Also, depending on the internal process, you may have to wait a full day for the engine/game code to be re-compiled."

You're saying that:

1) Re-coding this feature the game designer doesn't like will most likely take a full day, because apparently its a big change.

2) Re-compiling an engine with gameplay code might take up to 24 hours

I dont know where you got that from... But unless you're recompiling 2 000 000 lines of code on a 386 with 4 megs of ram running win2k using a 2GB swap drive using compression to save space....

I have compiled the whole Quake 2 source before, along with the gameplay code, and the rendering libraries, it takes less than 2 minutes on my not-so-new computer both in release and in debug. Recompiling the gameplay code only is much faster, no more than 30 seconds.

And now, with Lua:

"The designer notices something that he's not quite happy with, Joey opens up a console, makes a couple of changes to the script, and re-runs the game to see the new behavior. The changes can be implemented immediately and displayed immediately, rather than waiting for a re-compile. Additionally, if Joey was particularly bright, the scripting system could be a tool available to the level designers when they build the level. That way, with a little re-education, the designers could set events in the game, like triggers, doors, in-game events to happen without programmer intervention."

So now you say that:

1) Changes can be recompiled immediatly.....

But now only the gamecode is in Lua, so why did you even mention recompiling the engine code earlier? Game designers usually prefer to put the game code in an external library (see: Half-Life, Quake, Quake2, Quake3, RTCW).

2) The changes can be displayed immediatly...

You said earlier that it would take up to a day to recode.... So either Brandon doesn't mind waiting a long time, or Lua has some sort of magic that can instantly code anything in a single line.

What I'm saying here, is that if it takes a day to code it in C, it wont take 5 minutes to code it with a scripting language, it will most likely take a long time too..

3) Lua allows to make interesting map scripts. Triggers, doors opening, etc.

In half-life you could do all those things as well, using an interesting entity system, which was not actually scripting, you didnt have any commands... This system was very easy to use for the non programmer because it was intuitive and interactive.

I dont believe you covered the disadvantages as well... Such as the fact that your gamecode is automatically open source. And learning Lua is also a new learning process, even though it might not be so complicated, you need to take the time to learn it.

To conclude... Lua seems like a cool scripting language to mess with, something cool to know for the gaming industry... But it doesnt seem like a requirement for every indie game developper. Now please dont take my comments wrong, I believe the point of this thread is to actually discuss the article.

[edited by - Max_Payne on May 1, 2003 10:18:57 PM]

 User Rating: 1438   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

There are some great examples of how to interface Lua and C++. The Wiki is the place to be for Lua idioms and snippets.

 User Rating: 1322   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

"1) Changes can be recompiled immediatly.....

But now only the gamecode is in Lua, so why did you even mention recompiling the engine code earlier? Game designers usually prefer to put the game code in an external library (see: Half-Life, Quake, Quake2, Quake3, RTCW)."


WITHOUT Lua, the gamecode might be integrated in the main "project", and so recompiling would take a long time..
WITH Lua, designers, game coders etc can simply update the script file and reload the game (pretty instant).
A Lua script could be seen as an external library -- Quake had the QuakeC scripting language, Quake3 has some bytecode format... You should note that all of the games mentioned are by id sotfware also


"2) The changes can be displayed immediatly...

...

What I'm saying here, is that if it takes a day to code it in C, it wont take 5 minutes to code it with a scripting language, it will most likely take a long time too.."

I think Ash is referring to the way you can just re-run the application and any changes in the script will be apparant...?


 User Rating: 1015    Report this Post to a Moderator | Link

Finally an article worth reading. Thanks a bunch. I await for your next article regarding Lua.

There's a question though:
How do you load the lub file (in your example), is it the same as loading the lua file? Considering that the lub file is a binary file, so I am not really sure if you can use the same function (lua_dofile).


Current project: A puzzle game.
% completed: ~10%
Status: Active.


 User Rating: 1348   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by Max_Payne
Not to bash Lua or anything but, some arguments you put in the intro are just not very valid. You really put it like it was impossible to work without it.




Well, like I said in the article, they are fairly exaggerated examples. However, there is one shop (here in lovely BC) that actually has a process very similar to the one I just described. And yes, the build process (PS 2 build) does take several hours to compile. That's source code, assets and the like.

quote:

You're saying that:

1) Re-coding this feature the game designer doesn't like will most likely take a full day, because apparently its a big change.



yep. Not that hard to imagine, is it? Changing the way an AI system works *could* take the better part of a day. Just to get the required features.

quote:

2) Re-compiling an engine with gameplay code might take up to 24 hours



Most "dailys" are built overnight. A lot of studios test games based on that daily. Again, mileage may vary. But this is a common practise, not just in games, but in 'tradional' software development firms as well.

quote:

I have compiled the whole Quake 2 source before, along with the gameplay code, and the rendering libraries, it takes less than 2 minutes on my not-so-new computer both in release and in debug. Recompiling the gameplay code only is much faster, no more than 30 seconds.



Recompile time on Torque - full build, which I would do on a daily basis in production:

Just a hair over an hour on my 933 Intel 3 box. 256 megs of RAM.

quote:

And now, with Lua:



So now you say that:

1) Changes can be recompiled immediatly.....
[\quote]

Nope. What I'm saying is that there *is* no re-compilation. There is just re-execution.

[quote]
2) The changes can be displayed immediatly...

You said earlier that it would take up to a day to recode.... So either Brandon doesn't mind waiting a long time, or Lua has some sort of magic that can instantly code anything in a single line.
[\quote]

No, what I'm saying is that I don't have to force myself (as the engine coder, or even the game coder) to have to recompile anything. I start my app (or even leave the application running) and change behaviours on the fly.

[quote]
What I'm saying here, is that if it takes a day to code it in C, it wont take 5 minutes to code it with a scripting language, it will most likely take a long time too..
[\quote]

Sorry if you got that impression. Wasn't the intent. Again, meant to be an exaggerated example.

[quote]
3) Lua allows to make interesting map scripts. Triggers, doors opening, etc.

In half-life you could do all those things as well, using an interesting entity system, which was not actually scripting, you didnt have any commands... This system was very easy to use for the non programmer because it was intuitive and interactive.



Um, that's all parameter based. It doesn't really allow you more *complex* constructs that a scripting language does, like if/then/else conditions, loops, and the like. I've worked in that kind of environment before (Lithtech or whatever it's called now was a prime example of it). And personally, I'd never go back to it.

quote:

I dont believe you covered the disadvantages as well... Such as the fact that your gamecode is automatically open source. And learning Lua is also a new learning process, even though it might not be so complicated, you need to take the time to learn it.



It is? No, no it's not. You'd better double check the agreement there. Because that's not the case. Otherwise you'd see people like Lucasarts, Bioware, and Relic (just to name a few) putting all their gamecode on-line. Lua has no restrictions on use. As for the complexity, yeah, Lua can, at times, be a bugger to learn. But then again, learning how to link a DLL to your engine is no cakewalk either.

quote:

To conclude... Lua seems like a cool scripting language to mess with, something cool to know for the gaming industry... But it doesnt seem like a requirement for every indie game developper. Now please dont take my comments wrong, I believe the point of this thread is to actually discuss the article.


Hey, no harm, no foul. You brought up some interesting points. I tend to go a little *over the top* when I write. Just my style, I guess. It gets people talking. And that's good, I think.

Crap, I didn't mean for the response to be this long.


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by alnite
Finally an article worth reading. Thanks a bunch. I await for your next article regarding Lua.



Thanks. I try to make my articles interesting to read.

quote:

How do you load the lub file (in your example), is it the same as loading the lua file? Considering that the lub file is a binary file, so I am not really sure if you can use the same function (lua_dofile).



Yep. You can. Part of the lua file parsing determines if the file is in binary format (well, byte code compiled is a better definition) or not.

That make sense?


 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
It is? No, no it's not. You'd better double check the agreement there. Because that's not the case. Otherwise you'd see people like Lucasarts, Bioware, and Relic (just to name a few) putting all their gamecode on-line. Lua has no restrictions on use.


I believe he meant that all Lua scripts you distribute with your game are automatically open source.

Well, it's not necessarily true either. First, you will usually distribute your game with all Lua scripts pre-compiled (that is, as bytecode chunks). It doesn't make things really safe but makes hacking more inconvenient. But you can easily store your scripts in custom format files, including compressed and crypted files.

[edited by - HellRaider on May 3, 2003 4:41:44 AM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by HellRaider
I believe he meant that all Lua scripts you distribute with your game are automatically open source.


Good point. I really need to write my responses a little earlier in the evening. I missed that point completely


[edited by - iamevilhomer on May 3, 2003 3:07:32 PM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
Original post by Max_Payne
Not to bash Lua or anything but, some arguments you put in the intro are just not very valid. You really put it like it was impossible to work without it.

Ash has already responded, but I'd like to back him up. For a large project with many people working on it, his arguments are completely valid.

Some of the reasons why having to recompile for every change a designer wants does require more time than you suggest:

1- On any large project, the code base is going to be constantly in flux, as people check in additions and changes. It's not uncommon to have the head version of the code to be broken in some way, making it impossible to immediately add the changes to the latest version of the codebase. Rolling back to a stable version might be an option, but in many cases the designer is going to want to see how his changes work in the latest version of the code.

2- Usually, making a new build isn't as simple as recompiling and passing out an exe. It usually involves making the changes, testing to see if they work, doing regression testing to make sure they didn't break something else, checking in the changes (which usually requires merging), building (which by itself, on a commercial game, *does* take at lease a couple of hours, at least for a full build) and for consoles, burning a disk.

3- You need to be able to find a programmer who has the time to make the changes and do the build. Usually, they'll all have a lot of things to do, and unless one of the higher-ups tells them that it's a priority, it'll take them a few days to fit you in.

The bottom line is that the ability for a designer to be able to make changes without having to get a programmer or anyone else involved is going to save time and avoid frustration. Lua, and scripting in general, isn't a magic bullet, but it has proven to be a very effective means of addressing these problems.

If your argument is only in regards to small, independent development teams, then I'd agree that the overhead of incorporating a scripting language into your basecode probably isn't worth it. But this site isn't just for indies

 User Rating: 2088   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

quote:
Original post by Max_Payne
1) Re-coding this feature the game designer doesn't like will most likely take a full day, because apparently its a big change.

2) Re-compiling an engine with gameplay code might take up to 24 hours

I dont know where you got that from... But unless you're recompiling 2 000 000 lines of code on a 386 with 4 megs of ram running win2k using a 2GB swap drive using compression to save space....



There are languages out there that allow new code to be compiled into the game image while the game is still running .

As an example, take the standard event loop that all games have. (Here is an example in C using SDL):

while ( SDL_WaitEvent(&event) >= 0 ) {
        switch (event.type) {
            case SDL_ACTIVEEVENT: {
                if ( event.active.state & SDL_APPACTIVE ) {
                    if ( event.active.gain ) {
                        printf("App activated\n");
                    } else {
                        printf("App iconified\n");
                    }
                }
            }
            break;
                    
            case SDL_MOUSEBUTTONDOWN: {
                handle_mousebutton();
            }
         }
}
 


Now, imagine starting the game as usual. You then determine that the handle_mousebutton() function isn't doing what it is supposed to (i.e. it has a bug), so you just make the necessary changes, recompile the function (while the game is still running) and the next time that the event loop is entered, the new function is called transparently. I do this all the time in Lisp.


 User Rating: 1061   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

quote:
There are languages out there that allow new code to be compiled into the game image while the game is still running.


Yeah, and you can do that with Lua too. Not only change a function while the game is running but also write scripts that will write and compile Lua functions dynamically in run-time.

PS: It may not be exactly as in Lisp since Lua doesn't compile to native binary code, but the idea is the same.

[edited by - HellRaider on May 3, 2003 11:01:47 PM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The link to http://kakariko.ne.client2.attbi.com/lua/Lua_Workshop_files/frame.html seems to be down.

Overall, nice article, although I had been hoping it was a different one to the one I'd already read

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


 User Rating: 1905   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

quote:
Original post by Kylotan
The link to http://kakariko.ne.client2.attbi.com/lua/Lua_Workshop_files/frame.html seems to be down.


That's odd, because I just tried it, and it works.
quote:

Overall, nice article, although I had been hoping it was a different one to the one I'd already read


working on it.

[edited by - iamevilhomer on May 5, 2003 12:14:25 PM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The link works now for me, so it must have just been a temporary hitch.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]


 User Rating: 1905   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

I downloaded Lua 5.0 and the API has changed. The code in the tutorials does not compile or link.

For example, the first sample program doesn't compile because lua_open() does not take an argument in the 5.0 version.

The second example does not work because lua_dostring has been deprecated and is now declared in lauxlib.h.


[edited by - Jambolo on May 7, 2003 1:39:07 AM]

 User Rating: 1839   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link
Page:   1 2 »»
All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: