cocos2dx中c++向lua传string的问题

来源:互联网 发布:apache 官网下载64位 编辑:程序博客网 时间:2024/04/30 07:07

cocos2dx 3.13

自动生成的lua接口代码传string到lua时不支持含0的字符串。修复方法:


int lua_myproj_CustomLuaEvent_getStrData(lua_State* tolua_S){    int argc = 0;    CustomLuaEvent* cobj = nullptr;    bool ok  = true;#if COCOS2D_DEBUG >= 1    tolua_Error tolua_err;#endif#if COCOS2D_DEBUG >= 1    if (!tolua_isusertype(tolua_S,1,"CustomLuaEvent",0,&tolua_err)) goto tolua_lerror;#endif    cobj = (CustomLuaEvent*)tolua_tousertype(tolua_S,1,0);#if COCOS2D_DEBUG >= 1    if (!cobj)     {        tolua_error(tolua_S,"invalid 'cobj' in function 'lua_myproj_CustomLuaEvent_getStrData'", nullptr);        return 0;    }#endif    argc = lua_gettop(tolua_S)-1;    if (argc == 0)     {        if(!ok)        {            tolua_error(tolua_S,"invalid arguments in function 'lua_myproj_CustomLuaEvent_getStrData'", nullptr);            return 0;        }        std::string ret = cobj->getStrData();//---------------------------------------------------------------------------------        //tolua_pushcppstring(tolua_S,ret); // not suport cpp std::string (contains 0x00)// 修改为if (ret.empty())lua_pushnil(tolua_S);elselua_pushlstring(tolua_S, ret.c_str(), ret.size());//---------------------------------------------------------------------------------        return 1;    }    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "CustomLuaEvent:getStrData",argc, 0);    return 0;#if COCOS2D_DEBUG >= 1    tolua_lerror:    tolua_error(tolua_S,"#ferror in function 'lua_myproj_CustomLuaEvent_getStrData'.",&tolua_err);#endif    return 0;}




0 0
原创粉丝点击