1

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.

1 Answer 1

1

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 }
Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.