I think the problem I want to solve will an easy one for most of you:)
Suppose I have a class Node which has a member function setPosition(float x, float y, float z). I would like to be able to define a variable of class Node in lua and then be able to use setPosition() function also form the Lua.
I know that there are issues that Lua is rather for C not C++ and has its issues with C++ classes but I also know that it is achievable.
Add a comment
|
1 Answer
I'd use Luabind for this. With it you can easily bind C++ classes so they can be created, accessed, and modified in Lua. The code you'd write in C++ might look roughly like this:
module(L) [
class_<Node>
.def(constructor<>)
.def("setPosition", &Node::setPosition)
];
Then you'd be able to say this in Lua:
node = Node()
node:setPosition(x, y, z)
You could also make bindings so that the Lua looks a little more natural and could support things like these:
node1 = Node(x, y, z)
node2 = Node()
node2.position = { x, y, z }
2 Comments
Patryk
Thanks for the response. I have already built and included luabind to my project but the problem is that I have my "game engine" in c++, I have my RenderSystem (variable) in there already defined and I wasnt to access it and call something like this :
Body x = RenderSystem->createBody(); I know already how can I expose a class with Luabind but how can I achieve this ?John Zwinck
Then you should make a binding (perhaps using Luabind) for the RenderSystem class and its createBody function, and then you can call it from Lua and operate on the result. Or perhaps I am misunderstanding what you're looking for?