Tomasu / LuaGlue

C++11 Lua 5.2 Binding Library
zlib License
79 stars 22 forks source link

Setting Lua properties from C++ #34

Closed JMLX42 closed 10 years ago

JMLX42 commented 10 years ago

Hello,

I need to be able to set values from my C++ code. Considering 'rotate' is my "script" class:

function rotate:start(node)
 self.lookAt = Vector3.create()

  -- do some stuff...
end

I would like to be able to do the same thing as self.lookAt = ... but from my C++ code. My goal is to be able to init. my object with values set by other scripts/my C++ code:

function rotate:start(node)
  -- self.lookAt might have been init. from C++ already...
  self.lookAt = self.lookAt or Vector3.create()

  -- do some stuff...
end

How can we do this?

Tomasu commented 10 years ago

Assuming you're using LuaGlue as it was intended, lookAt should just be a C++ class member (or getter/setter). then you can set that in C++ when ever you want, and lua will see the value you set.

JMLX42 commented 10 years ago

The thing is as you can see in the sample code the self.lookAt is created/set from Lua and is dynamic. My LuaScript C++ class is a generic class, it just provides methods a "script" will require but has no script-specific bindings. The goal is to be able to create Lua scripts without any specific C++ code.

To explain a bit more the process:

So declaring explicit bindings in my C++ is not an option. I think I'll create a LuaScript::getInstance() method that returns the C++-side stub object used for binding. In other words the actual Lua "self" of my script object. This way my Lua code can get a script instance and set some values on it.

Still, it would be useful to have C++ LuaGlueClass::set()/get() methods just like the LuaGlueClass::invoke ones... What do you think?

Tomasu commented 10 years ago

I suppose. But really, this funky wrapper class you have is just weird :P

Tomasu commented 10 years ago

Ok, this should be implemented now (in ad75202)

Some api additions:

You can fetch globals now via: glue.getGlobal<TYPE>("name") Get LuaGlueClass<CLASS>* via: glue.getClass<CLASS>("name") get properties via: glue_class->getProperty<TYPE>("name", obj) set properties via: glue_class->setProperty("name", obj, value)

other changes in ad75202 include some std::string handling improvements, and const fixes.