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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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
Image
Im Blue
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

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

Post 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.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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
Image
Im Blue
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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.
Image
Im Blue
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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.
Image
Im Blue
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

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

Post 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.
Image
Im Blue
Post Reply