🎉 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
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.
Advertisement
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.
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.

Learn and program in assembler, then Lua will make a lot of sence.
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.
Java is a better example. Any virtual machine you have to program using a stack will do.
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 = 0ypos = 0xvel = 0.5yvel = 0.4function 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 + yvelend 


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.

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.
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.

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

This topic is closed to new replies.

Advertisement