Sign in to follow this  

[Lua]How to use object pointer in lua with luabind?

This topic is 3149 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

Hello everbody. I'm just start using luabind, I want to do something like this: class Object { public: Object(); static Object* GetInstance(); void Print(); private: static Object* mObject; int mValue; }; Object* Object::mObject = NULL; Object::Object() { mObject = this; } Object* Object::GetInstance() { return mObject; } void Object::Print() { cout << mValue << endl; } (I know the code doesn't make sense, just for test). I know how to export method to lua ,but I don't know how to receive class object pointer in lua. What I want to do is have an Object class instance in lua and receive the return value of GetInstance(): // In lua // How to define the objectPointer? // And how to use this pointer call Print() function? objectPointer = Object::GetInstance() Many thanks. [Edited by - Erosnick on May 1, 2009 4:18:41 AM]

Share this post


Link to post
Share on other sites
In lua, the concept of a pointer is hidden from you. Essentially, luabind will make lua see a pointer as a normal table.

Anyway, lua syntax is slightly different from C++ syntax - to get what you want, you'll have to do something like:


/* Somewhere in your binding stuff */
luabind::def("getInstance", Object::getInstance), //Get instance should be a static or free function anyway...
luabind::class_<Object>("Object")
.def("print", &Object::print)



/* Lua codez */
--Note how we have to call member functions with the : operator.
--Static functions, however, can be called with operator .
obj = getInstance();
obj:print();

--Specifically, what we are doing with the : operator is just syntatic sugar for
-- obj.print(obj), since lua dosn't know how to automatically pass the 'this' pointer into member functions.

Share this post


Link to post
Share on other sites

This topic is 3149 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.

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