Skip to main content
gamedev.net
🔒 Locked

Lua: getting started

Started by 23yrold3yrold 23 replies 3.8k views
Original Post
23yrold3yrold
23yrold3yrold
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.
ashleymatheson
ashleymatheson
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
23yrold3yrold
23yrold3yrold
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.
23yrold3yrold
23yrold3yrold
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.
ashleymatheson
ashleymatheson
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
Sneftel
Sneftel
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.
23yrold3yrold
23yrold3yrold
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.
Fruny
Fruny
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
llyod
llyod
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!!
Sneftel
Sneftel
quote:
Original post by Fruny
lua_ref/lua_getref/lua_unref :
Looks like malloc/dereference/free



More like IncRef/dereference/DecRef. Not that Lua uses reference-counting semantics (thank God), but it has the same effect of getting a reference to an already-extant object.

*** ERROR 500 ***



Don''t listen to me. I''ve had too much coffee.
Sneftel
Sneftel
The C++ class is not only doable, but a good idea for C++ programs. The wrapper doesn''t need to do much at all.



Don''t listen to me. I''ve had too much coffee.
23yrold3yrold
23yrold3yrold
Since it would be a very short example, I''ll ask; can some post a small snippet using ref/getref/unref? I took a shot at it and can''t get it working, and I don''t know if I''m initializing it wrong or ''get''ing it wrong or both or neither or what, and the only example I have in from of me is rather inpenetrable. Anyone?

Chris Barry (crbarry at mts.net)
My Personal Programming Depot
Jesus saves ... the rest of you take 2d4 fire damage.
Sneftel
Sneftel
quote:
Original post by Anonymous Poster
Learn and program in assembler, then Lua will make a lot of sence.


Eeeh, let''s hope not. First off, you''d have to learn assembler for a register-less architecture, since Lua does everything through stacks and tables. A closer parallel would be learning Java bytecode. But, then, most people just learn Java. Similarly, you don''t need to understand assembler to use and integrate Lua.



Don''t listen to me. I''ve had too much coffee.
23yrold3yrold
23yrold3yrold
quote:
Original post by Sneftel
The C++ class is not only doable, but a good idea for C++ programs. The wrapper doesn''t need to do much at all.
Glad to hear This is a little test I came up with (don''t laugh; keep in mind I''ve only been using Lua for three days )



class MyClass
{
public:
MyClass(string filename = "class script.txt"): xpos(0), ypos(0), luastate(lua_open(0))
{
lua_dofile(luastate, filename.c_str());
}

~MyClass()
{
lua_close(luastate);
}

void Update()
{
// push the function onto the stack, then call it
lua_getglobal(luastate, "update");
lua_call(luastate, 0, 0);

// update the x and y member variables
lua_getglobal(luastate, "xpos");
xpos = (int)lua_tonumber(luastate, lua_gettop(luastate));
lua_getglobal(luastate, "ypos");
ypos = (int)lua_tonumber(luastate, lua_gettop(luastate));

// clear the stack
lua_settop(luastate, 0);
}

void Draw(BITMAP* buf)
{
// draw a circle at the x and y coordinates
}

protected:
int xpos;
int ypos;
lua_State *luastate;
};


And accompanying script:


xpos = 0
ypos = 0
xvel = 0.5
yvel = 0.4

function update()
if (xpos < 5) then
xvel = 0.5
elseif (xpos > 635) then
xvel = -0.5
end

if (ypos < 5) then
yvel = 0.4
elseif (ypos > 475) then
yvel = -0.4
end

xpos = xpos + xvel
ypos = ypos + yvel
end


Cool; Lua-Pong Anyway; just those few functions to figure out. I think I''ll troll through those advanced examples again; maybe I''ll understand more now. Still accepting examples of ref/getref/unref .....

Chris Barry (crbarry at mts.net)
My Personal Programming Depot
Jesus saves ... the rest of you take 2d4 fire damage.
Sneftel
Sneftel
Interesting approach.... I would separate the scripting code from the physics code, however. The lua class I wrote has very few functions that are more than one line.



Don''t listen to me. I''ve had too much coffee.
23yrold3yrold
23yrold3yrold
Yeah, but I''m not that good with Lua yet I would of course make the bigger and more frequently called functions in C and call them from Lua; for Pong, I won''t sweat it. Hmmm, I wonder if I can store the address of the object as type userdata and call global functions with it (since I can''t register member functions). I''ve got to figure this stuff out; it''s not like it''s a huge API or something ....

Chris Barry (crbarry at mts.net)
My Personal Programming Depot
Jesus saves ... the rest of you take 2d4 fire damage.
shurcool
shurcool
i was just wondering if a compiled Lua script is secure. what i mean by that, is that you can''t decompile it back to a script, similar to that of a dll file. because that would compromise security, which is not an option for my game (people will make cheats easier than ever).

---
shurcool
wwdev
Sneftel
Sneftel
quote:
Original post by shurcool
i was just wondering if a compiled Lua script is secure. what i mean by that, is that you can''t decompile it back to a script, similar to that of a dll file. because that would compromise security, which is not an option for my game (people will make cheats easier than ever).

---
shurcool
wwdev



Lua scripts, I''m sure, can be easily decompiled. On the other hand, if you''re trusting your game client not to cheat, you''re already making a big mistake. Bytecode might be easier to decompile than object code, but not by much.



Don''t listen to me. I''ve had too much coffee.
shurcool
shurcool
quote:
Original post by Sneftel
Lua scripts, I''m sure, can be easily decompiled.


what is there to decompile in a Lua script? it''s already decompiled. which is why i was talking about the bytecode, or compiled Lua script.

quote:
On the other hand, if you''re trusting your game client not to cheat, you''re already making a big mistake.


where did you get that from? i''m not trusting the client with things like i killed player x, but if one can modify the scripts (which will be a big part of the game: weapons, game modes, etc.), he/she can easily make a wall-hack (not sure about this...) or an aim-bot (definitely possible) very easily. no need to hook anything whatsoever.

quote:
Bytecode might be easier to decompile than object code, but not by much.


thanks, that''s what i wanted to hear... almost. that means that it''s pretty safe, right?

---
shurcool
wwdev

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.