2
votes

So I've been using this game engine for quite some time, I can create a game using that with either the built-in events or I can use C++ but I've recently tried to embed Lua 5.3 to it, but I'm having problems on how to register a certain C++ class to Lua, example: In the game engine's C++, I would change the background color like this:

#include "GDCpp/RuntimeScene.h"

void changeBackground(RuntimeScene & scene)
{
     scene.SetBackgroundColor(250,100,85)
}

But my problem is, how can I do that in Lua ? how can I register that function and class in Lua 5.3?

1

1 Answers

1
votes

Hava a look on this example.

You create/request a metatable with the class name, push member functions (except constructor) into him and register a constructor function which returns your class as userdata, linked with the metatable.

Setting field "__index" on self table is required that later member access leads to the metatable, not userdata. "__gc" happens on garbage collection - your destructor. Because Lua is written in C, allocating userdata memory doesn't invoke constructors, so the class instance was put on the heap and the address passed to Lua.