Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind existing C variable to Lua using Lua C API

Tags:

c++

binding

lua

Few years ago I used luabind for binding C++ objects to Lua and vice versa. There was a way to bind existing (allocated through new operator in C++ code) object to Lua and using it in a script like:

binded_object.property = new_value

Now I am working on a new project where I would like to add some scripting. For the sake of simplicity no boost or any heavy templates are desired. So my question is how to do it using only Lua C Api?

Unfortunately, all examples I encountered show how to bind custom C++ type to Lua and then create a new object of that type in the script and finally return the object to C++.

like image 531
Peter Goldwynn Avatar asked Feb 23 '26 01:02

Peter Goldwynn


1 Answers

You want SWIG. No Boost, no heavy templates, just all the bindings you could ever want, autogenerated for your convenience.

Now, your question isn't totally clear: by "only Lua C Api", you might mean "making all the Lua C API calls manually in code I write myself". If that's what you mean, take it from someone who's been there: you don't actually want to do that. It's difficult and unrewarding, and you get nothing whatsoever in exchange. Use SWIG. If you want, you can manually edit the bindings it generates, but use SWIG.

like image 199
David Seiler Avatar answered Feb 24 '26 13:02

David Seiler



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!