Sign in to follow this  

Calling a C++ objects methods from Lua.

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

I want to be able to call a function in one of my C++ objects in Lua. For example:
class Player
{
public:
    ...
    static int SetHealth(lua_State *L)
    {
        health += lua_gettop(L);
    }
private:
    int health;
}



How would I do in the lua_register function to let it know what to do. I am writing my own binds by the way because I would like to be able to understand how to do it myself before using a binding library. [Edited by - thejahooli on March 3, 2010 1:22:58 PM]

Share this post


Link to post
Share on other sites
Hey!

Finally someone who actually wants to know what is going on under the hood! haha jk

I'm going to take stab at explaining this and maybe someone can correct/add anything im missing.

First things first the Lua stack is how lua interacts with C++, and there is a lua registry that is used to register globals. If you look at Luna.h (a very lightweight binding) you will see how it manipulates these things to expose class methods...

Since your method is part of a class, you will need to pass a member function pointer to lua.

The idea is that you store this data in a lua table that somewhat represents the structure of your class. and this table is treated as a global.

EDIT: noticed i submitted it too early, PIL is a great reference.

[Edited by - AverageJoeSSU on March 4, 2010 12:40:06 PM]

Share this post


Link to post
Share on other sites

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