Sign in to follow this  
TonyFish

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

Recommended Posts

TonyFish    126
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
codeandroid    140
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
TonyFish    126
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this