2

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?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
RobeeZ
  • 95
  • 9
  • 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. Jul 23 '14 at 16:01
  • Try [this link](http://stackoverflow.com/questions/22515908/using-straight-lua-how-do-i-expose-an-existing-c-class-objec-for-use-in-a-lua/22558439#22558439), if my understanding is correct; – W.B. Jul 23 '14 at 16:08
  • 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 Jul 23 '14 at 17:07
  • 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. Jul 23 '14 at 17:41

1 Answers1

0

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.

Dmitry Ledentsov
  • 3,620
  • 18
  • 28