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