Call a Lua method on instance from C++

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

Call a Lua method on instance from C++

remigillig
[I couldn't find the search function for this mailing list on
sourceforge, sorry if this has already been asked]

Hello,

I am trying to conceptualize some things with luabind and I would like
to do something specific but there is not much information on how to
achieve that.

What I want to do is be able to call from C++ a method that was
created on an instance of an object in Lua.

e = Entity("Hello")

function e:update()
    print("update Hello")
end

>From the C++ side, I also have an update() function on the class
Entity and I would like to call the Lua defined method inside it.

I tried a couple of things like making a wrapper with base_wrap but I
couldn't do it without creating another class, or also something like
creating a self object directly from the this pointer :

void update()
{
    printf("Entity::update()\n");

    object self(L, *this);

    int t = type(gettable(self, "update"));
    printf("type %i\n", t);
    if (t == LUA_TFUNCTION)
        call_member<void>(self, "update");
}

Well that didn't work (obviously) so I ask my question here because
that would be a great technique for events. Thanks!

Remi Gillig.

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
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 method on instance from C++

DONGIEUX-AKKA Christophe (SAGEM DEFENSE SECURITE)
Hi,

You have to derive your Entity class in Lua. I give you an example.

You may have your Entity class like this:
class Entity
{
  Entity() {}
  ~Entity() {}
  virtual void update() { std::cout << "I am an Entity from c++" << std::endl; }
};

So define a wrapper:
class EntityWrapper : public Entity, public luabind::wrap_base
{
  EntityWrapper() : Entity() {}
  virtual void update() { luabind::call_member<void>( this, "update" ); }
  static void default_update( Entity* p ) { p->update(); }
};

Export the class to Lua:
luabind::module( pLuaState ) [

luabind::class_< Entity, EntityWrapper >( "Entity" )
  .def( luabind::constructor<>() )
  .def( "update", &Entity::update, &EntityWrapper::default_update )

];


And your Lua script:
class 'EntityLua' (Entity)
function EntityLua:__init()
  Entity.__init(self)
end

function EntityLua:update()
  print( "I am an Entity from Lua" )
end

instance = EntityLua()



Then call your script with do_file and get the 'instance' variable created in your Lua script to get the EntityLua instance in c++. Then call the update() method on it.


Hope this helped.
Chris.
________________________________________
De : Remi Gillig [[hidden email]]
Date d'envoi : samedi 14 août 2010 20:47
À : [hidden email]
Objet : [luabind] Call a Lua method on instance from C++

[I couldn't find the search function for this mailing list on
sourceforge, sorry if this has already been asked]

Hello,

I am trying to conceptualize some things with luabind and I would like
to do something specific but there is not much information on how to
achieve that.

What I want to do is be able to call from C++ a method that was
created on an instance of an object in Lua.

e = Entity("Hello")

function e:update()
    print("update Hello")
end

>From the C++ side, I also have an update() function on the class
Entity and I would like to call the Lua defined method inside it.

I tried a couple of things like making a wrapper with base_wrap but I
couldn't do it without creating another class, or also something like
creating a self object directly from the this pointer :

void update()
{
    printf("Entity::update()\n");

    object self(L, *this);

    int t = type(gettable(self, "update"));
    printf("type %i\n", t);
    if (t == LUA_TFUNCTION)
        call_member<void>(self, "update");
}

Well that didn't work (obviously) so I ask my question here because
that would be a great technique for events. Thanks!

Remi Gillig.

------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
#
" Ce courriel et les documents qui lui sont joints peuvent contenir des informations confidentielles ou ayant un caractère privé. S'ils ne vous sont pas destinés, nous vous signalons qu'il est strictement interdit de les divulguer, de les reproduire ou d'en utiliser de quelque manière que ce soit le contenu. Si ce message vous a été transmis par erreur, merci d'en informer l'expéditeur et de supprimer immédiatement de votre système informatique ce courriel ainsi que tous les documents qui y sont attachés."
******
" This e-mail and any attached documents may contain confidential or proprietary information. If you are not the intended recipient, you are notified that any dissemination, copying of this e-mail and any attachments thereto or use of their contents by any means whatsoever is strictly prohibited. If you have received this e-mail in error, please advise the sender immediately and delete this e-mail and all attached documents from your computer system."
#


------------------------------------------------------------------------------
This SF.net email is sponsored by

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user