• 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!

Lua [1.1] Item:getAttribute(ITEM_ATTRIBUTE_ATTACK) returns 0

BloodGauntlet

Member
Joined
Jul 27, 2016
Messages
45
Reaction score
18
This always returns zero no matter the weapon equipped. Am I doing something wrong or is this a bug?

Code:
function foo (player)
local weapon = player:getSlotItem(CONST_SLOT_LEFT)
local damage = weapon:getAttribute(ITEM_ATTRIBUTE_ATTACK)

return damage
end


Here is the getAttribute function from lua.cpp, the source is unmodified. I'm just posting this for reference.
Code:
int LuaScriptInterface::luaItemGetAttribute(lua_State* L)
{
    // item:getAttribute(key)
    Item* item = getUserdata<Item>(L, 1);
    if (!item) {
        lua_pushnil(L);
        return 1;
    }

    itemAttrTypes attribute;
    if (isNumber(L, 2)) {
        attribute = getNumber<itemAttrTypes>(L, 2);
    } else if (isString(L, 2)) {
        attribute = stringToItemAttribute(getString(L, 2));
    } else {
        attribute = ITEM_ATTRIBUTE_NONE;
    }

    if (ItemAttributes::isIntAttrType(attribute)) {
        lua_pushnumber(L, item->getIntAttr(attribute));
    } else if (ItemAttributes::isStrAttrType(attribute)) {
        pushString(L, item->getStrAttr(attribute));
    } else {
        lua_pushnil(L);
    }
    return 1;
}
 
Last edited:
Solution
This always returns zero no matter the weapon equipped. Am I doing something wrong or is this a bug?
It's working as expected, an item does not have any attributes by default. You can check if an item has an attribute by using the metamethod Item.hasAttribute.

Code:
function foo(player)
    local damage = 0
    local weapon = player:getSlotItem(CONST_SLOT_LEFT)
    if weapon then
        damage = weapon:hasAttribute(ITEM_ATTRIBUTE_ATTACK) and weapon:getAttribute(ITEM_ATTRIBUTE_ATTACK) or weapon:getType():getAttack()
    end

    return damage
end
tried it ingame on 1.2, for some reason attributes doesnt want to work on standard items, but this works
Code:
ItemType(player:getSlotItem(CONST_SLOT_LEFT):getId()):getAttack()
 
This always returns zero no matter the weapon equipped. Am I doing something wrong or is this a bug?
It's working as expected, an item does not have any attributes by default. You can check if an item has an attribute by using the metamethod Item.hasAttribute.

Code:
function foo(player)
    local damage = 0
    local weapon = player:getSlotItem(CONST_SLOT_LEFT)
    if weapon then
        damage = weapon:hasAttribute(ITEM_ATTRIBUTE_ATTACK) and weapon:getAttribute(ITEM_ATTRIBUTE_ATTACK) or weapon:getType():getAttack()
    end

    return damage
end
 
Solution
It's working as expected, an item does not have any attributes by default. You can check if an item has an attribute by using the metamethod Item.hasAttribute.

Code:
function foo(player)
    local damage = 0
    local weapon = player:getSlotItem(CONST_SLOT_LEFT)
    if weapon then
        damage = weapon:hasAttribute(ITEM_ATTRIBUTE_ATTACK) and weapon:getAttribute(ITEM_ATTRIBUTE_ATTACK) or weapon:getType():getAttack()
    end

    return damage
end

Edit: I replied a little hastily and am going through your script atm.
Edit: OK, so I'm starting to understand thank you for that example.

tried it ingame on 1.2, for some reason attributes doesnt want to work on standard items, but this works
Code:
ItemType(player:getSlotItem(CONST_SLOT_LEFT):getId()):getAttack()

I see, thank you, currently I used the ItemType:getId() e.g,

Code:
    local leftSlot = player:getSlotItem(CONST_SLOT_LEFT)
    local uid = leftSlot:getUniqueId()
    local weapon = ItemType(Item(uid):getId())

I need to look into how unique id works.
 
Last edited:
use what ninja did, leftSlot:getType():getAttack(), dont need to use uid at all

i was told that no "regular" items have any attributes at all, so they dont need to be saved to the databse, instead they just get the data from items.xml i think
 
use what ninja did, leftSlot:getType():getAttack(), dont need to use uid at all

i was told that no "regular" items have any attributes at all, so they dont need to be saved to the databse, instead they just get the data from items.xml i think

Yep, makes a lot more sense.
 
Back
Top