passing objects/classes to/from lua and c++
hey:
does anyone have a good url showing a decent example of how to pass class/object instances to a lua function, and possibly passing class instances back to cpp?
thanks!
nekoflux
here is my attempt to clarify with some example pseudo code:
lets say I register the lua function in c++, so that my c++ code can call init(),a lua function. I also want to pass an instance of a class (DataObj) to this init function so that it is accessible and manipulatable in the lua
heres the cpp for the class I want pass to lua:
heres the pseudo code for the lua init() function that will be called from cpp:
Is this even possible? Am I doing this in a retarded way? Perhaps there is a simpler way to go about this...
perplexed,
neko
lets say I register the lua function in c++, so that my c++ code can call init(),a lua function. I also want to pass an instance of a class (DataObj) to this init function so that it is accessible and manipulatable in the lua
heres the cpp for the class I want pass to lua:
class DataObj{public: void div(); void avg();}
heres the pseudo code for the lua init() function that will be called from cpp:
function init()dat = lua_popstack(); //pops an instance of Dataclass dat.div(); --execute method of class that was passed to luaend
Is this even possible? Am I doing this in a retarded way? Perhaps there is a simpler way to go about this...
perplexed,
neko
nekoflux
>> Is this even possible? Am I doing this in a retarded way? Perhaps there is a simpler way to go about this...
Yes it is possible, don''t worry it took me ages to understand how passing data between languages worked. Don''t worry about lua_popstack, or in fact about any of the lua api, if you choose luabind it does it all for you...
-- Luabind Module
function init()
yourname:div();
yourname:avg();
end
Yes it is possible, don''t worry it took me ages to understand how passing data between languages worked. Don''t worry about lua_popstack, or in fact about any of the lua api, if you choose luabind it does it all for you...
extern "C"{#include <lua.h>#include <lualib.h>#include <lauxlib.h>}#include <luabind\luabind.hpp>#include <iostream>using namespace std;using namespace luabind;lua_State* L;// Object to transfer to luaclass DataObj{public: void div() { cout << "div" << endl; } void avg() { cout << "avg" << endl; }};int main(){ // Open Lua L = lua_open(); { lua_baselibopen(L); lua_iolibopen(L); lua_strlibopen(L); lua_mathlibopen(L); // Prepare Luabind open(L); // Export the definition of DataObj to Lua module(L) [ class_<DataObj>("DataObj") .def(constructor<>()) .def("div", &DataObj::div) .def("avg", &DataObj::avg) ]; // Retrieve all global objects in Lua object globals = get_globals(L); // Set the yourname variable in the global namespace globals["yourname"] = DataObj(); // Invoke script (error handling omitted) lua_dofile(L, "test.lua"); } // Close Lua (Luabind shutsdown automatically) lua_close(L); return 0;}
-- Luabind Module
function init()
yourname:div();
yourname:avg();
end
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement