Taking a C++ class as a parameter from a lua call

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Taking a C++ class as a parameter from a lua call

Noah Schwartz
Hi,
 
     I was looking through the luabind documentation for a way to have lua call a C++ function and pass as an argument a lua-exposed C++ class.
 
     For example, I might have some lua function that will take as an argument an array of some C++ class. In that lua function I would call some C++ and pass an argument to it one of the classes in the array. I saw that luabind has an object class but I dont see how to use this to facilitate this kind of functionality. Is there a way for me to do an object_cast from the luabind object to the desired C++ class?
 
Thanks,
Noah
Reply | Threaded
Open this post in threaded view
|

Re: Taking a C++ class as a parameter from a lua call

Arvid Norberg-2

On Apr 5, 2006, at 21:08, Noah Schwartz wrote:

> Hi,
>
>      I was looking through the luabind documentation for a way to  
> have lua call a C++ function and pass as an argument a lua-exposed C
> ++ class.
>
>      For example, I might have some lua function that will take as  
> an argument an array of some C++ class. In that lua function I  
> would call some C++ and pass an argument to it one of the classes  
> in the array. I saw that luabind has an object class but I dont see  
> how to use this to facilitate this kind of functionality. Is there  
> a way for me to do an object_cast from the luabind object to the  
> desired C++ class?

You can either define the C++ function as taking a luabind::object  
and then try to cast it to different types to see if any of them  
succeeds. Taking a luabind::object means that you can pass in any lua  
value, and you would have to add your own error handling in case the  
argument type that's passed in doesn't match the function's  
expectations.

The other (probably more common) way of doing it is to let the  
function take the desired C++ class and register it as such with  
luabind. Then luabind will match and convert the lua value into the C+
+ type. If the argument type doesn't match, luabind will either look  
for another overload of the function (if there are any) or generate  
an error message.


--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user