Luabind and class initialization

Started by
5 comments, last by Tenac 15 years, 9 months ago
Hi, I just needed some help with luabind class initialization if anyone know abouts it. Firstly I have created a base class in c++ (I have also created a wrapper for it with luabind::wrap_base) and I set up the correct modules to expose them. Secondly I have created a class inside a lua script that derives from this c++ base class. Now I wish to be able to create an instance of this lua class from c++ and use it, but I do not know how to do this. Example: base_class (c++) derived_class (lua script) I wish to be able to make an instance of that derived class inside c++: derived_class der; der.print(); Any help would be great, thank you.
Advertisement
Hi again,

Is there anyone that could help me with an answer to this question? I'm still rather stuck on it. Even if the answer is just a simple "thats not possible".

thank you.
Hello,

I'm defining a derived class in lua like this:

class "CDerivedClass" (CBaseClass)

-- base-class constructor args would go into the braces after "super"
function CDerivedClass:__init() super()
-- code goes here!
end

function CDerivedClass:DoStuff()
-- code goes here!
end

-- define other class funcs here

Then you can intialize it like every other class
Hi,

Thank you for your reply! I'm glad to see that it is possible.

One question remains to me though, when you say that I can initialize it like any other class, can i initialize instances of it in cpp code? or would the initialization need to be in a LUA script because the class is made there.

If its possible to initialize a copy in c++ is the code be tricky? I know that once the class script is read in then LUA knows what it is, but I am not sure how to use it.

Sorry if I am asking too many questions, thanks again for your response. :)
Hi again,

I am still stuck on this issue, if anyone could help me with what I stated in my last post (about initializing copies of LUA classes inside C++, or if its even possible) I would be very very thankful.
From reading the documentation it seems this is possible, perhaps using the Luabind objects. I've only looked briefly at Luabind, instead going with another binding library ( more light weight but not as full featured as Luabind, MLuabind ). I'll try to track down some samples for ya, if i can find them.

Worse case u can just do a luaL_dostring and that will execute a string as if it was Lua code, allowing you to bascially do anyhting you want in C++. You'll just have to build the string dynamically, which shouldn't be too much trouble.

-ddn
I used this tutorial to set up lua and luabind in my project, see if it helps you at all:
http://www.nuclex.org/articles/quick-introduction-to-luabind

This topic is closed to new replies.

Advertisement