• Advertisement

Archived

This topic is now archived and is closed to further replies.

passing objects/classes to/from lua and c++

This topic is 5120 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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:

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 lua
end


Is this even possible? Am I doing this in a retarded way? Perhaps there is a simpler way to go about this...

perplexed,

neko

Share this post


Link to post
Share on other sites
>> 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...


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 lua

class 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

Share this post


Link to post
Share on other sites
ah, i was about to ask how to get global varibles from a lua script and you''re example above appears to have answered the question for me before i even asked it, cheers

Share this post


Link to post
Share on other sites

  • Advertisement