For a project I'm working on, I need to expose some C++ classes in another library to Lua. Unfortunately, one of the most important classes in this library has lots of Unions and Enums (the sf::Event class from SFML), and from a quick Google search I've discovered there is nothing on exposing C++ Unions to Lua. I don't mind if it's being exposed with the Lua/C API, with a library or with a binding generator, as long as it works. But, I'd prefer not to use a binding generator, because I want to be able to create an object in C++, then expose that instance of the object to Lua (unless that's possible with a binding generator)
相关问题
- Sorting 3 numbers without branching [closed]
- How to get the return code of a shell script in lu
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
If you are willing to use boost::variant rather than a union, you could try using my LuaCast library.
I'll try to add unions as a base type as well.
For registering C/C++ functions you need to first make your function look like a standard C function pattern which Lua provides:
Every function that needs to be registered in Lua should follow this pattern. Return type of
int
, single parameter oflua_State* L
. And count of returned values. Then, you need to register it in Lua's register table so you can expose it to your script's context:For registering simple variables you can write this:
After that, you're able to call your function from a Lua script. Keep in mind that you should register all of your objects before running any script with that specific Lua state that you've used to register them.
In Lua:
Result:
20
I guess this could help you!