• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ Why attribute value isn't updated in game?

Acubens

Old Penguin
Joined
May 6, 2008
Messages
1,261
Solutions
13
Reaction score
184
Location
Venezuela
Im trying to add durability system to items, im using OTHire sources, but the lua function idk why isn't working, im not expert in c++ but im trying to
do the best xD

Function in ItemAttribute class:
C++:
void setDurability(uint16_t n) {setIntAttr(ATTR_ITEM_DURABILITY, n); }

Function added to item class:

C++:
void Item::setItemDurability(uint16_t n)
{
    ItemAttributes::setDurability(n);
}

C++:
int LuaScriptInterface::luaGetItemDurability(lua_State *L)
{
    //getItemDurability(uid)
    uint32_t uid = popNumber(L);

    ScriptEnviroment* env = getScriptEnv();

    Item* item = env->getItemByUID(uid);
    if(!item){
        reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
        lua_pushnil(L);
        return 1;
    }

    lua_pushnumber(L, (int32_t) item->getDurability());
    return 1;
}

C++:
int LuaScriptInterface::luaDoSetItemDurability(lua_State *L)
{
    //doSetItemDurability(uid, charges)
    uint32_t value = popNumber(L);
    uint32_t uid = popNumber(L);

    ScriptEnviroment* env = getScriptEnv();

    Item* item = env->getItemByUID(uid);
    if(item){
        // I added this function to item class but still not working.
        item->setItemDurability(value);
        // Debuging it gives all time the value that i have loaded by items.xml
        std::cout << item->getDurability() << std::endl;
        lua_pushboolean(L, true);
    }
    else{
        reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
        lua_pushboolean(L, false);
    }
    return 1;
}

Then when i use in action script this
Lua:
    doSetItemDurability(item.uid, getItemDurability(item.uid) - 1)

It continues showing 500, that i have added on items.xml with:

XML:
<attribute key="durability" value="500"/>

@Nottinghster @Ezzz
 
Last edited:
I'm going to presume you of course registered the Lua functions, and just omitted here for brevity
Lua:
    //getItemDurability(cid)
    lua_register(m_luaState, "getItemDurability", LuaScriptInterface::luaGetItemDurability);


What I'm not seeing is where you patched Item::getDescription ? Are you sure you interfaced it with the viewer?


What happens if you
doSetItemDurability(item.uid, getItemDurability(item.uid) - 1)
and then
debugPrint(getItemDurability(item.uid))
???
 
it shows 500 the same value loaded from items.xml it doest not change with setItemDurability(thing.uid, value)

C++:
int LuaScriptInterface::luaDoSetItemDurability(lua_State *L)
{
    //luaDoSetItemDurability(uid, charges)
    int32_t charges = popNumber(L);
    uint32_t uid = popNumber(L);

    ScriptEnviroment* env = getScriptEnv();

    Item* item = env->getItemByUID(uid);
    if(item){
        int32_t durability = item->getDurability();
        if(durability != 0){
            g_moveEvents->onRemoveTileItem(item->getTile(), item);
        }
        item->setDurability(charges);
        g_moveEvents->onAddTileItem(item->getTile(), item);
        lua_pushboolean(L, true);
    }
    else{
        reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
        lua_pushboolean(L, false);
    }
    return 1;
}

getDurability Function:

C++:
int32_t getDurability() const {
        if (hasAttribute(ATTR_ITEM_DURABILITY)) {
            return getIntAttr(ATTR_ITEM_DURABILITY);
        }
        return items[id].durability;
    }


setDurability Function:

C++:
int32_t setDurability(int32_t n) {
        setIntAttr(ATTR_ITEM_DURABILITY, n);
        return 1;
    }

And nothing.
 
Last edited:
validateIntAttrType is probably cock blocking you.


C++:
void ItemAttributes::setIntAttr(itemAttrTypes type, int32_t value)
{
    if(!validateIntAttrType(type))
        return;

    Attribute* attr = getAttr(type);
    if(attr){
        attr->value = reinterpret_cast<void*>(static_cast<ptrdiff_t>(value));
    }
}

bool ItemAttributes::validateIntAttrType(itemAttrTypes type)
{
    //list of numeric type attributes
    switch(type){
    case ATTR_ITEM_ACTIONID:
    case ATTR_ITEM_UNIQUEID:
    case ATTR_ITEM_OWNER:
    case ATTR_ITEM_DURATION:
    case ATTR_ITEM_DECAYING:
    case ATTR_ITEM_CHARGES:
    case ATTR_ITEM_FLUIDTYPE:
    case ATTR_ITEM_DOORID:
        return true;
        break;

    default:
        return false;
        break;
    }
    return false;
}

setDurability -> setIntAttr -> validateIntAttrType -> 🖕


🤷‍♂️
 
I have it added too in validateIntAttrType

C++:
bool ItemAttributes::validateIntAttrType(itemAttrTypes type)
{
    //list of numeric type attributes
    switch(type){
    case ATTR_ITEM_ACTIONID:
    case ATTR_ITEM_UNIQUEID:
    case ATTR_ITEM_OWNER:
    case ATTR_ITEM_DURATION:
    case ATTR_ITEM_DECAYING:
    case ATTR_ITEM_CHARGES:
    case ATTR_ITEM_DURABILITY: // I added it before
    case ATTR_ITEM_FLUIDTYPE:
    case ATTR_ITEM_DOORID:
        return true;
        break;

    default:
        return false;
        break;
    }
    return false;
}

And nothing :(

Get function works, but set item new value dont work thats strange really.
 
Last edited:
Back
Top