Page 1 of 1

Help: Overloading a function from C++ in Lua using LuaBind

Posted: Tue Jun 29, 2010 4:15 pm
by Zer0XoL
I am implementing Lua in my engine, so now I am doing so items can be loaded from lua into the game.
What im having problem is that it seems like i cant overload my class specific functions in lua.
thi is my item struct:

Code: Select all

struct item
{
        string name; //name of the item
        bool hasOwner;
        player* Owner; //owner of the item
        Use(){} //this is the function i want to overload in lua
        item(string _name)
        {
                hasOwner = false;
                Owner = NULL;
                name = _name;
        }
};
and this is how i want the lua file too look like kind of:

Code: Select all

item_n0 = item("my item");
function item_n0:Use()
        if(hasOwner == true)
                Owner:SetHP(100);
        end
end
and i have wrapped my item class as this:

Code: Select all

	luabind::module(LuaState)
	[
			luabind::class_<item>("item")
			.def(luabind::constructor<string>())
			.def_readwrite("name",&item::name)
			.def_readwrite("Owner",&item::Owner)
			.def("Use",&item::Use)
	];
I have also tried making my Use function a function pointer, but no succes.
I know it is possible because i have seen it in a lua file for garrysmod, and it uses luabind i heard.
so if anyone knows anyhing and can help me I would be very happy :D

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Wed Jun 30, 2010 2:54 pm
by xiphirx
You may want to turn the struct into a class and make use virtual. That's my guess.

Im a dumbass, disregard this message.

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Wed Jun 30, 2010 3:09 pm
by Zer0XoL
xiphirx wrote:You may want to turn the struct into a class and make use virtual. That's my guess.

Im a dumbass, disregard this message.
well thanks for at least replying :P

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Thu Jul 01, 2010 9:36 am
by dandymcgee
I looked at it, but honestly luabind looks way more complicated than tolua++. It's so much less of a hassle to just parse a cleaned header and tinker with the output as needed. :|

Edit:
Okay maybe LuaBind isn't all that bad.

For Use( int ) and Use( double ) try:

Code: Select all

	luabind::module(LuaState)
	[
			.def("Use", (void(*)(int)) &item::Use)
			.def("Use", (void(*)(double)) &item::Use)
	];
Source: http://www.rasterbar.com/products/luabind/docs.html

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Thu Jul 01, 2010 3:34 pm
by Zer0XoL
dandymcgee wrote:I looked at it, but honestly luabind looks way more complicated than tolua++. It's so much less of a hassle to just parse a cleaned header and tinker with the output as needed. :|

Edit:
Okay maybe LuaBind isn't all that bad.

For Use( int ) and Use( double ) try:

Code: Select all

	luabind::module(LuaState)
	[
			.def("Use", (void(*)(int)) &item::Use)
			.def("Use", (void(*)(double)) &item::Use)
	];
Source: http://www.rasterbar.com/products/luabind/docs.html
well, thanks but this wasnt really what i ment, i want to overload OR REPLACE the function in C++ with a lua function, but the lua function should still be a member of the C++ class or struct so it can acces its private members. I have even tried using a function pointer and then redefine it in lua, this doesnt work because that function doesnt have acces to private members and it crashes :P. I have seen Lua files from Gmod addons do this function redefinition thing and i cant find anything about it.. but thanks anyway.

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Fri Jul 02, 2010 6:09 pm
by dandymcgee
Zer0XoL wrote: well, thanks but this wasnt really what i ment, i want to overload OR REPLACE the function in C++ with a lua function, but the lua function should still be a member of the C++ class or struct so it can acces its private members. I have even tried using a function pointer and then redefine it in lua, this doesnt work because that function doesnt have acces to private members and it crashes :P. I have seen Lua files from Gmod addons do this function redefinition thing and i cant find anything about it.. but thanks anyway.
I guess I don't understand what you're trying to do. Can you give an example of what you would use this functionality for?

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Fri Jul 02, 2010 6:46 pm
by Zer0XoL
dandymcgee wrote:
Zer0XoL wrote: well, thanks but this wasnt really what i ment, i want to overload OR REPLACE the function in C++ with a lua function, but the lua function should still be a member of the C++ class or struct so it can acces its private members. I have even tried using a function pointer and then redefine it in lua, this doesnt work because that function doesnt have acces to private members and it crashes :P. I have seen Lua files from Gmod addons do this function redefinition thing and i cant find anything about it.. but thanks anyway.
I guess I don't understand what you're trying to do. Can you give an example of what you would use this functionality for?
I thought i gave an example in the first comment but.
I want a C++ class member function to be defined as a Lua function, so that i can script Specific objects of that class.
maybe im not doing it the right way but i dont know any other way than to have a function pointer and then make a function in lua and then point to that function, but that is not a member function.
For example, i want to make 2 items:

Code: Select all

Boots = item();
Boots.name = "Boots";
Boots.cost = 100;
Boots.description = "if acivated, it makes you faster";
function Boots:Use()
    Owner():SetSpeed(2);
end

Axe = item();
Axe.name = "Axe";
Axe.cost = 200;
Axe.description = "when activated, changes your sprite";
function Axe:Use()
    Owner():SetSprite("another_sprite.bmp");
end
you see what i mean?
object specific member functions defined in lua.
Owner() returns the owner of the item, in C++ i have the function BindOwner(player *p) to bind the item to its current owner, this is why it needs to be a member function.
All i need is a solution for how to do it, i dont know how :( this kinda stops me from working further on my project.
if there was a simple solution i would want to use it, if not i guess ill have to make something up.

Re: Help: Overloading a function from C++ in Lua using LuaBind

Posted: Wed Jul 07, 2010 5:40 am
by Zer0XoL
Actually, my logic here is invaild, i make objects instead of types.
Therefore doing it this way makes it impossible for 2 players to have the same item.
What i really want is to make a new type upon the one in C++, i dont know how Lua handles class inherit, so ill have o check it out.