• How does one make an entire c++ class available to lua
    2 replies, posted
I am working on a little game library that includes a behaviour class that is supposed to handle the behaviour of certain objects. I am not sure how to implement this though. I want to use lua to: *)Control gameobjects *)Control amatures and animations (for example someone could script a animation behaviour to always move the IK Bones down to the floor when playing a walk animation) I am not very familliar with how lua works under the hood. All I know is that there is a lua "stack" on which I can push some functions and variables from c++. But in LUA there arent any classes, are there? Well then how can there be structures that interact with the c++ object below? Also another problem I am facing is keeping this stuff dry. I need a way to use the behaviour class to control multiple types of OTHER objects. Normally this wouldnt be a problem with templates but since I also need to make different functions available to lua when a different class is passed, this doesnt seem possible. So do I need to write a seperate class for every object I want to control?
The closest thing to classes you'll see in the engine side of Lua is userdata. It can be affected by metatables like tables, but the core object is a pointer to actual data in C, and can't inherently be used as tables. For each class, you'll have to write a metatable and typeid for your userdata, then you push it to either the global environment or the registry. After this, you write Lua pushcfunc calls adding your C methods to the metatable and adapting the lua args for those methods. In other words, you'll need Lua bindings for each class method -- you can't just push the entire class.
Lua is great but there are much more user-friendly scripting languages to implement into C++. I am a huge fan of AngelScript, it is a C-like scripting language and it is veryyyy easy to bind C++ objects to it (only real downside is that its slower than Lua but I haven't noticed it to be a problem). You still have to bind each method/member but the C++ class and its data is directly exposed to the scripting language. [CODE] RegisterObjectType("InputManager", 0, asOBJ_REF | asOBJ_NOHANDLE); RegisterGlobalProperty("InputManager input", GameEngineInterface::get()->inputManager()); RegisterObjectMethod("InputManager", "bool onActionPress(string)", asMETHOD(InputManager, isActionPressed), asCALL_THISCALL); RegisterObjectMethod("InputManager", "bool onActionRelease(string)", asMETHOD(InputManager, isActionReleased), asCALL_THISCALL); [/CODE] And in the script: [CODE] if (input.onActionPress("moveForward")) { // bla bla bla... } [/CODE]
Sorry, you need to Log In to post a reply to this thread.