getCreatureStorage returning NIL

Discussion in 'Support' started by Way20, Apr 16, 2018.

  1. Way20

    Way20 Active Member

    Joined:
    Sep 29, 2014
    Messages:
    139
    Likes Received:
    48
    Best Answers:
    1
    On my server when the player doens't have the storage, instead of returning -1 its returning nil, so if I use
    Code (Text):
    1. math.max(0, getCreatureStorage(cid, storage))
    I got this error.

    Code (Text):
    1. bad argument #3 to 'max' (number expected, got nil)
    I think that is source related, can anyone give me a hint to where I should look?
     
    Last edited: Apr 16, 2018
  2. Old-Server

    Old-Server New Member

    Joined:
    Mar 19, 2018
    Messages:
    12
    Likes Received:
    7
    Best Answers:
    1
    i guess this function could solve it but
    Code (Lua):
    1. function getStore(cid,storage)
    2.   return getCreatureStorage(cid,storage) or -1
    3. end
    else i guess check luascript.cpp or creature.cpp
     
  3. Way20

    Way20 Active Member

    Joined:
    Sep 29, 2014
    Messages:
    139
    Likes Received:
    48
    Best Answers:
    1
    I already made a function similar but I wanna fix it through source editing. And I already looked in luascript and creature.cpp.

    Luascript.cpp
    Code (Text):
    1. int32_t LuaInterface::luaGetCreatureStorage(lua_State* L)
    2. {
    3.     //getCreatureStorage(cid, key)
    4.     std::string key = popString(L);
    5.     ScriptEnviroment* env = getEnv();
    6.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
    7.     {
    8.         std::string strValue;
    9.         if(!creature->getStorage(key, strValue))
    10.         {
    11.             lua_pushnumber(L, -1);
    12.             lua_pushnil(L);
    13.             return 2;
    14.         }
    15.  
    16.         int32_t intValue = atoi(strValue.c_str());
    17.         if(intValue || strValue == "0")
    18.             lua_pushnumber(L, intValue);
    19.         else
    20.             lua_pushstring(L, strValue.c_str());
    21.     }
    22.     else
    23.     {
    24.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
    25.         lua_pushboolean(L, false);
    26.     }
    27.  
    28.     return 1;
    29. }
    Creature.cpp
    Code (Text):
    1. bool Creature::getStorage(const std::string& key, std::string& value) const
    2. {
    3.     StorageMap::const_iterator it = storageMap.find(key);
    4.     if(it != storageMap.end())
    5.     {
    6.         value = it->second;
    7.         return true;
    8.     }
    9.  
    10.     value = "-1";
    11.     return false;
    12. }
    It seens fine to me, I don't know what else I can do. :(
     
  4. Sarah Wesker

    Sarah Wesker S.E ©

    Joined:
    Mar 16, 2017
    Messages:
    137
    Likes Received:
    45
    Best Answers:
    10
    Code (Text):
    1.  
    2. int32_t LuaInterface::luaGetCreatureStorage(lua_State* L)
    3. {
    4.     //getCreatureStorage(cid, key)
    5.     uint32_t key = popNumber(L);
    6.     ScriptEnviroment* env = getEnv();
    7.     if(Creature* creature = env->getCreatureByUID(popNumber(L)))
    8.     {
    9.         std::string strValue;
    10.         if(creature->getStorage(key, strValue))
    11.         {
    12.             int32_t intValue = atoi(strValue.c_str());
    13.             if(intValue || strValue == "0")
    14.                 lua_pushnumber(L, intValue);
    15.             else
    16.                 lua_pushstring(L, strValue.c_str());
    17.         }
    18.         else
    19.             lua_pushnumber(L, -1);
    20.     }
    21.     else
    22.     {
    23.         errorEx(getError(LUA_ERROR_CREATURE_NOT_FOUND));
    24.         lua_pushboolean(L, false);
    25.     }
    26.  
    27.     return 1;
    28. }
     

Share This Page

Loading...