2
votes

I'm trying to give a scripting ability to my component system. Of couse each component has a field "parent" which holds access to the parent Actor. I can write the code so I can access my C++ methods from Lua, but I got stuck when I want to return custom userdata, like actor. I have a method getParent(), and I want to return the Actor userdata, so I can write Lua code like this:

parent = getParent()
parent.<some method>

How can I achieve this? With lightuserdata? Or when getParent get called, create a new userdata, and point that pointer to the original one?

1
Is this a question of how to expose a C++ object (class) together with its member functions (such as getParent())? Or am I misunderstanding your problem? is getParent() in your example a global function??W.B.
Try this link, if my understanding is correct;W.B.
What i want to achieve is that i can add and get components from the actor. here is an example of the lua code: player = Actor.new() player:addComponent(ImageComponent.new(player)) imgc = player:getComponent("ImageComponent") imgc.setImagePath("testimg.png") function player:tick() player.setPosition(0,0) end (it could be invalid code, but nevermind now) and then in my c++ class, i would retrieve the tick method of the player object or table or what should i call, and calls it every time the engine calls tick()RobeeZ
I'm sorry, but it's too abstract and vague to me at the moment. Please show some actual scenario of Lua/C++ interaction (best in your question, not comments for readability) and indicate which part you're having problems with. Or at least which is pure Lua object and which is (or should be) a C++ class exposed to Lua.W.B.

1 Answers

0
votes

There's a number of binding library authors that have gone through the pain of creating a component that enables simple exposing of C++ classes to Lua.