Call a lua function and pass a C++ Class object

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

Call a lua function and pass a C++ Class object

Beric Holt-2

I have a function declared in Lua

I want to be able to call that function from C++ and pass in a C++ class object, but I’m struggling how to do it.

 

The lua function looks like this:

--------AI.Lua starts----------

function UpdateActor(pActor)

                local pActorName = pActor.Name;

--             etc etc

                Return 0;

end

--------AI.Lua ends----------

 

 

CActor is declared in my luabinder:

---------Luabinding Code starts------

module(L)

                [

                                class_<CActor>("CActor")

                                                .property("Name",                                                                                         &CActor::GetActorName, &CActor::SetActorName)

//            etc etc

                                ,

                                def("Ptr2Actor",                                                                                                               &Ptr2Actor),

                ];

---------Luabinding Code starts------

 

I get an error when I call the lua function like this

---------C++ calling code starts----------

                CActor *actor = GetMyActor();

int ret = luabind::call_function<int>(luaVM, “UpdateActor", actor);

---------C++ calling code ends----------

 

 

This method works fine if I’m calling a C++ method through luabind.

 

Can anyone offer me any suggestions on how I should be doing this.  One possibility is that I need to somehow derive a lua function from a C++ function, but I haven’t yet figured out how to do that.

 

Thanks for any help that you can offer.

 

Cheers

Beric

http://buzzrick.true.geek.nz

 



Beric Holt
Solution Delivery

INTERGEN: LEVEL 1, CONSULTANCY HOUSE, 7 BOND STREET, PO BOX 205, DUNEDIN 9054, NEW ZEALAND.
TEL +64 3 477 5648 DDI 3255 FAX +64 3 477 5649 MOB +64 21 242 7420 WWW.INTERGEN.CO.NZ

  New Zealand Microsoft Partner Awards 2008 - Partner of the Year

Want me to do something? Send me an action item: www.actionthis.com
Catch the latest on our blog: www.intergen.co.nz/blog
Be kind to the planet: Do you really need to print this?

This electronic message together with any attachments is confidential. If you are not the intended recipient: (i) do not copy, disclose or use the contents in any way (ii) please let us know by return e-mail immediately and then destroy the message. Intergen is not responsible for any changes made to this message and/or any attachments after sending by Intergen.

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Call a lua function and pass a C++ Class object

James Porter-2
Beric Holt wrote:
> I have a function declared in Lua
>
> I want to be able to call that function from C++ and pass in a C++ class
> object, but I’m struggling how to do it.

It works fine for me, can you provide more information about what kind
of error you're getting?

- Jim

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Call a lua function and pass a C++ Class object

Beric Holt-2
I'm getting an exception in the proxy_function_caller->Ret operator code (on line 157 of call_function.hpp of luabind-0.8.1).  The exception description is "luabind::cast_failed at memory location 0x0017ebd0."

It looks like it's trying to check the return type of the function that I'm trying to call.  Because the function is declared in Lua script, and not explicitly declared with a luabind def statement, it doesn't know the full function signature.

(It's also entirely possible that I'm barking up the totally wrong tree).

Cheers
Beric


-----Original Message-----
From: James Porter [mailto:[hidden email]]
Sent: Monday, 11 May 2009 5:10 p.m.
To: [hidden email]
Subject: Re: [luabind] Call a lua function and pass a C++ Class object

Beric Holt wrote:
> I have a function declared in Lua
>
> I want to be able to call that function from C++ and pass in a C++ class
> object, but I'm struggling how to do it.

It works fine for me, can you provide more information about what kind
of error you're getting?

- Jim

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Call a lua function and pass a C++ Class object

Beric Holt-2
In reply to this post by James Porter-2
Weird.  I just tried calling the function with no return type (instead of an int) and it worked.

I changed
int ret = luabind::call_function<int>(luaVM, "UpdateActor", actor);
into
luabind::call_function<void>(luaVM, "UpdateActor", actor);

So how do I get this going with a return value?

Cheers
Beric

-----Original Message-----
From: Beric Holt
Sent: Tuesday, 12 May 2009 4:56 p.m.
To: [hidden email]
Subject: RE: [luabind] Call a lua function and pass a C++ Class object

I'm getting an exception in the proxy_function_caller->Ret operator code (on line 157 of call_function.hpp of luabind-0.8.1).  The exception description is "luabind::cast_failed at memory location 0x0017ebd0."

It looks like it's trying to check the return type of the function that I'm trying to call.  Because the function is declared in Lua script, and not explicitly declared with a luabind def statement, it doesn't know the full function signature.

(It's also entirely possible that I'm barking up the totally wrong tree).

Cheers
Beric


-----Original Message-----
From: James Porter [mailto:[hidden email]]
Sent: Monday, 11 May 2009 5:10 p.m.
To: [hidden email]
Subject: Re: [luabind] Call a lua function and pass a C++ Class object

Beric Holt wrote:
> I have a function declared in Lua
>
> I want to be able to call that function from C++ and pass in a C++ class
> object, but I'm struggling how to do it.

It works fine for me, can you provide more information about what kind
of error you're getting?

- Jim

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Call a lua function and pass a C++ Class object

James Porter-2
In reply to this post by Beric Holt-2
Beric Holt wrote:
> I'm getting an exception in the proxy_function_caller->Ret operator code (on line 157 of call_function.hpp of luabind-0.8.1).  The exception description is "luabind::cast_failed at memory location 0x0017ebd0."
>
> It looks like it's trying to check the return type of the function that I'm trying to call.  Because the function is declared in Lua script, and not explicitly declared with a luabind def statement, it doesn't know the full function signature.

It's probably trying to cast to an integer, but the thing you're
returning isn't an integer (this applies to returning nothing as well).
Make sure all your control paths return an object of the expected type
(an int in this case). Note that this does *not* apply if you don't use
the return value of the function. That is:

----- Lua -----
function foo(arg)
     arg:do_something()
     -- Note the lack of a return statement.
     -- The same thing would happen with this, too:
     -- return "Hello, C++!"
end

----- C++ -----
luabind::call_function<int>(state,"foo",arg); // succeeds
int i = luabind::call_function<int>(state,"foo",arg); // FAILS

----- End code -----

Specifically, what happens is that call_function creates an instance of
proxy_function_caller, which has a conversion operator for int (or
whatever return type you specified). When that conversion operator is
called, it will attempt to convert the return value of the Lua function
to your requested type, and will throw luabind::cast_failed if it can't.

- Jim

------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user