🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lua: getting started

Started by
23 comments, last by 23yrold3yrold 21 years, 10 months ago
Well, I''ve gotten Lua compiled, embedded and working, and done some simple things with it. Like using get/setglobal, or writing a little script that calls C++ functions using lua_register. T''is fun Now I was just curious if there was some definitive tutorial site out there. The Lua equivalent of NeHe, if you guys know of one. So far I''ve found most of the official documentation a bit lackluster eg. I have no idea how to use ref, getref or unref, or even exactly what they do. And should I keep a lua_State as a member variable in any class needing a script? If anyone knows a really good site for learning Lua in and out I''d really love to hear about it. Chris Barry (crbarry at mts.net) My Personal Programming Depot

Jesus saves ... the rest of you take 2d4 fire damage.

Advertisement
b - to - the - umpski

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Jesus saves ... the rest of you take 2d4 fire damage.

Not quite sure what you''re asking for here. Are you looking for a simple starting point? Using Lua in Games?

www.lua.org is a good spot to start.

I''ve actually done a couple of lessons on using LUA in a game-oriented environment. I''d be willing to put them up if people were interested.

Humble,
Yet l33t
Humble,Yet l33t
I checked there, of course. I''ve been all over it. I''m just having trouble with some of it, since there''s no real tutorial site that takes you from start to finish with all the functions. The reference manual is only a small help. If there''s no good Lua tutorials out there then I guess I''ll figure it out eventually (and then write one ).

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Jesus saves ... the rest of you take 2d4 fire damage.

Last call before I continue the search for info on my own . Just so you know I''ve actually been figuring this stuff out for myself, these are the functions I''ve pretty much figured out:
  lua_State *lua_open (int stacksize);  void lua_close (lua_State *L);  int  lua_gettop (lua_State *L);  int  lua_stackspace (lua_State *L);  void lua_push* (lua_State *L, double n);  *    lua_to*  (lua_State *L, int index);  int  lua_dofile  (lua_State *L, const char *filename);  int  lua_dostring (lua_State *L, const char *string);  int  lua_dobuffer (lua_State *L, const char *buff, size_t size, const char *name);  void lua_getglobal (lua_State *L, const char *varname);  void lua_setglobal (lua_State *L, const char *varname);  #define lua_register(L, n, f)

Now, here''s some I''m still trying to wrap my head around. The return value and parameters on some of these don''t make much sense to me. If anyone can give me an example of how to use any of these, I would love to hear it:
  int  lua_call  (lua_State *L, int nargs, int nresults);  void lua_rawcall(lua_State *L, int nargs, int nresults);  int  lua_ref    (lua_State *L, int lock);  int  lua_getref (lua_State *L, int ref);  void lua_unref  (lua_State *L, int ref);  void lua_rawget (lua_State *L, int index);  void lua_rawset (lua_State *L, int index);

Most of the rest are trivial, but these confuse me. So does the way the stack works, and what advantages there are to multiple lua_State''s. There''s no real tutorials for this that I''ve been able to find, neither at lua.org nor at lua-users.org (the ones there are pretty beyond me, or use more Lua code when I want C code). Can anyone give me an idea how to use any of these?

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Jesus saves ... the rest of you take 2d4 fire damage.

Well, if ya can wait another day or so, I''ll have a tutorial up and running. you''re more than welcome to get ahold of me via ICQ should you have questions. I''m not master at lua, but I get by ...

Humble,
Yet l33t
Humble,Yet l33t
quote: Original post by 23yrold3yrold
  int  lua_call  (lua_State *L, int nargs, int nresults);  void lua_rawcall(lua_State *L, int nargs, int nresults);  int  lua_ref    (lua_State *L, int lock);  int  lua_getref (lua_State *L, int ref);  void lua_unref  (lua_State *L, int ref);  void lua_rawget (lua_State *L, int index);  void lua_rawset (lua_State *L, int index); 

Most of the rest are trivial, but these confuse me. So does the way the stack works, and what advantages there are to multiple lua_State''s. There''s no real tutorials for this that I''ve been able to find, neither at lua.org nor at lua-users.org (the ones there are pretty beyond me, or use more Lua code when I want C code). Can anyone give me an idea how to use any of these?


lua_call: push the name of the function, push the arguments, then call lua_call. Not too difficult once you understand the stack.

The rest of those you won''t need for awhile; the raw methods , in particular, are useless until you''re using tag methods, at which point they will become self-explanatory. The ref methods are a bit more useful, but I think you''ll get them once you''re more used to how Lua allocates and deallocates things.

The way the stack works: One of the big things they left out of the docs is how much Lua "sanitizes" the stack when exposing it to C-functions. When Lua calls a C function, the stack as your function sees it ENTIRELY consists of the arguments. And you don''t have to leave it in any state when you return, except that the return values need to be at the top (indices -1, -2, etc.).

One thing I found useful when starting out with Lua: an actual STACK of cards. well, not a stack, but a vertical line of them on my desk, with the stored value written on each. As you go through your code, move the cards around. Visual aids are your friend here.

Multiple lua_States: Eh... if you don''t know what you need them for, you don''t need them. Useful if your program is doing segmented operations, such as a web server or something, where one lua "session" doesn''t need to interact with the others. I don''t think there''s very much need for multiple states in a game.




Don''t listen to me. I''ve had too much coffee.
All right. I was toying with the notion of a C++ class with a lua_State member variable which just kept its other variables and AI, etc. in a script as Lua globals. It works rather well so far; just wondering if there were any drawbacks.

The ref functions I still can''t wrap my head around, but I''m working on it. I''ve got lua_call pretty much licked though, but those negative indicies still boggle me. Haven''t yet figured out how that stack quite works.

And here I thought this thread was gonna float off the page

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Jesus saves ... the rest of you take 2d4 fire damage.

lua_ref/lua_getref/lua_unref :
Looks like malloc/dereference/free



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Wouldn''t LUA states be useful to run "threads" of scripts simultaneously? Or is there another way. Forgive my ignorance, haven''t yet tried Lua.



2DNow Banner 2D Game Developers Place -- NOW WITH IOTDs!!

This topic is closed to new replies.

Advertisement