Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem binding a c++ function with luabind

Started by CDarwin May 4, 2013 at 5:30 PM 2 replies 2.2k views
Original Post
CDarwin
CDarwin

Hello,

I'm pretty new to luabind. I'm having a little problem binding one of my string functions(convert to uppercase). Just couldn't figure it out.

[source]void STools::UCase(char *txt)
{
for (;;)
{
u_int32 t = *reinterpret_cast(txt);
...
txt++;
}
}
[/source]


Here's the luabind code
[source]luabind::module(myLuaState) [
luabind::def("convert_upper", STools::UCase)
]; [/source]


Compiler Error

[source]error C2664: 'void (char *)' : cannot convert parameter 1 from 'const char *' to 'char *' [/source]

Any help is appreciated!

Gambini
Gambini

http://www.rasterbar.com/products/luabind/docs.html#out-value

That explains how your C++ functions can't take non-const ref/pointers if you want to bind them to Lua. So, the out_value function policy will add a return value in Lua code, rather than modifying the input.

Where your C++ code would look like

char* str = "hi";
UCase(str);
std::cout << str; //prints 'HI'

Your Lua code would look like

local str = "hi"
local ucstr = UCase(str)
--// or you can have str = UCase(str)
print(ucstr) //prints 'HI'
CDarwin
CDarwin

Thanks a lot Gambini,

I've tried your suggestion but I'm still getting compiler errors. Any other suggestions?

out_value(_1) causes the following error:

[SOURCE] error C2352: 'luabind::native_converter_base::match' : illegal call of non-static member function[/SOURCE]

out_value(_2) causes the following:

[SOURCE]error C2664: 'void (char *)' : cannot convert parameter 1 from 'const char *' to 'char *'[/SOURCE]

Gambini
Gambini

It looks like out_value doesn't work well with MSVC (here).

You'll have to either re-think the function or use Lua's "string.upper" function.

Another option is to make your own string class with UCase as a member function. That would bind perfectly fine to Lua, since UCase wouldn't need to take any parameters and only modify the object data.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.