Jump to content
  • Advertisement
Sign in to follow this  
thejahooli

Calling a C++ objects methods from Lua.

This topic is 3034 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!