从矢量加载所有元素

时间:2018-06-12 11:39:51

标签: c++

所以这些代码是游戏的一部分。资源路径的加载存储在向量中,并由指定的mob vnum加载。

示例:

If (vnum === 99)
    std::string = "Path/path/vnum_99_folder"

这是我的代码

void GetRaceResourcePathes(unsigned vnum, std::vector <std::string> &vec_stPathes)
{
    if (vnum >= 30000)
    {
        vec_stPathes.push_back("d:/work/npc2/");
        vec_stPathes.push_back("d:/work/npc/");
        vec_stPathes.push_back("d:/work/monster/");
        vec_stPathes.push_back("d:/work/monster2/");
    }
    else
    {
        vec_stPathes.push_back("d:/work/npc/");
        vec_stPathes.push_back("d:/work/npc2/");
        vec_stPathes.push_back("d:/work/monster/");
        vec_stPathes.push_back("d:/work/monster2/");
    }
}

上面的代码是资源路径,存储在矢量中..然后返回大小和加载路径。

这是

CRaceData* CRaceManager::LoadRaceData(DWORD vnum)
{
    auto fRaceName = m_kMap_dwRaceKey_stRaceName.find(vnum);

    if (m_kMap_dwRaceKey_stRaceName.end() == fRaceName)
    {
        return NULL;
    }

    auto &c_rstRaceName = fRaceName->second;

    if (c_rstRaceName.empty())
    {
        return NULL;
    }

    std::vector <std::string> vec_stFullPathName;
    GetRaceResourcePathes(vnum, vec_stFullPathName);

    CRaceData* pRaceData = CRaceData::New();
    pRaceData->SetRace(vnum);

    for (int i = 0; i < vec_stFullPathName.size(); i++)
    {
        std::string stFullPathName = vec_stFullPathName[i];
        {
            auto RaceSrcName = m_kMap_stRaceName_stSrcName.find(c_rstRaceName);

            if (m_kMap_stRaceName_stSrcName.end() == RaceSrcName)
            {
                stFullPathName += c_rstRaceName;
            }
            else
            {
                stFullPathName += RaceSrcName->second;
            }
        }

        stFullPathName += "/";
        std::string stMSMFileName = stFullPathName + c_rstRaceName + ".msm";

        if (!pRaceData->LoadRaceData(stMSMFileName.c_str()))
        {
            if (i != vec_stFullPathName.size() - 1)
            {
                TraceError("CRaceManager::RegisterRacePath : vnum[%u] LOAD MSMFILE[%s] ERROR. Will Find Another Path.", vnum, stMSMFileName.c_str());
                continue;
            }

            TraceError("CRaceManager::RegisterRacePath : vnum[%u] LOAD MSMFILE[%s] ERROR", vnum, stMSMFileName.c_str());
            CRaceData::Delete(pRaceData);
            return NULL;
        }

        std::string stMotionListFileName = stFullPathName;
        stMotionListFileName += pRaceData->GetMotionListFileName();

        LoadRaceMotionList(*pRaceData, stFullPathName.c_str(), stMotionListFileName.c_str());
        return pRaceData;
    }

    TraceError("CRaceManager::RegisterRacePath : vnum[%u] HAVE NO PATH ERROR", vnum);
    CRaceData::Delete(pRaceData);
    return NULL;
}

我想删除那些vnum == check ..并在同一时间加载所有路径..而不指定vnum ..

void GetRaceResourcePathes(unsigned vnum, std::vector <std::string> &vec_stPathes)
{
    vec_stPathes.push_back("d:/work/npc/");
    vec_stPathes.push_back("d:/work/npc2/");
    vec_stPathes.push_back("d:/work/monster/");
    vec_stPathes.push_back("d:/work/monster2/");
}

如果我像上面那样更改GetRaceResourcePathes,则只会加载第一行...&#34; work / npc /&#34;其他3个被忽略..

有什么问题?为什么不加载所有4个路径?

1 个答案:

答案 0 :(得分:2)

你的专栏:

return pRaceData;

从for循环内部返回。因此循环在第一次迭代后永远不会继续,LoadRaceData函数将返回第一次加载的结果。