Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing C struct from embedded lua

Tags:

c++

struct

lua

I'd like to embed lua to allow scripting in my C++ application. In particular, I have two structs which I'd like to pass as arguments to a given lua function. One will be read-only, the other will be read/write. Highly simplified examples of these structs follow:

struct inData
{
    int x;
    int y;
    //many other fields follow
};

struct outData
{
    int a;
    double b;
    //other fields follow
};

Both of these structs are created in the C++ code and will be processed there both before and after calling the lua functions. How can I pass these structs to a lua function such that the function can do things like this:

if(inData.x > 5) then outData.a = 1 end

and have the outData instance actually retain the changes after returning from the lua function?

like image 947
alanc10n Avatar asked May 19 '26 06:05

alanc10n


2 Answers

boring + boilerplate + multi-language = SWIG.

http://www.swig.org/Doc1.3/Lua.html#Lua_nn13

like image 130
Ben Voigt Avatar answered May 21 '26 22:05

Ben Voigt


You could also create a Lua table that reflects this same data structure for passing information between C and Lua.

like image 38
Judge Maygarden Avatar answered May 21 '26 22:05

Judge Maygarden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!