• Advertisement
Sign in to follow this  

LUA: Instantiating lua objects from c++ [SORTED] I think

This topic is 4953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I do this in c++? Let's say I have a c++ class (baseclass) which i extend in lua (derivedclass). I'd like to create an instace of derivedclass from the c++ code. I've looked through the docs but haven't had much success. Could someone please help em with this mater. Thanks Tony [Edited by - TonyFish on August 3, 2004 11:39:36 AM]

Share this post


Link to post
Share on other sites
Advertisement
You can instantiate it in a Lua VM (by executing code in the VM), but it isn't possible to create a C++ object for it (using the derived interface etc) if that's what you want to do.

Correct me if I'm wrong.

-psy

Share this post


Link to post
Share on other sites
So I can't do something like:

a = create_new_lua_object("extended");
a.somefunc();

Share this post


Link to post
Share on other sites
I got an answer out of someone in the luabind mailinglist:

Instantiating a lua class is just a matter of calling the constructor like any other function. For example:

object my_class(get_globals(L)["my_class"]);

Since the object won't be stored anywhere in lua, you need to adopt the object. Otherwise it will be collected by lua.
i.e.

my_base* p = object_cast<my_base*>(my_class(), adopt(result));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement