• 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 Get Magic Level from an item

Unknown Soldier

Mapping a map
Joined
Oct 30, 2010
Messages
297
Solutions
11
Reaction score
670
Hi,

how do I get magic level attribute from an item in TFS 1.4.2?
It seems that there is no method like getMagicLevel, so I can't get it like for instance, armor value item:getType():getArmor()

Thanks in advance
 
Solution
There is a method called getAbilities (github)

magicLevel = itemType:getAbilities().stats[4]
If you wonder how it works, here is a quick explanation

The function pushes all the stats, skills, absorbs into sub-tables
In this case we need the magicLevel points which is stored on the stats sub-table (git)

So lastly, we need to retrieve the value through a numeric index which in this case is 4 [enum -> (STAT_MAGICPOINTS<3> + 1) ]
There is a method called getAbilities (github)

magicLevel = itemType:getAbilities().stats[4]
If you wonder how it works, here is a quick explanation

The function pushes all the stats, skills, absorbs into sub-tables
In this case we need the magicLevel points which is stored on the stats sub-table (git)

So lastly, we need to retrieve the value through a numeric index which in this case is 4 [enum -> (STAT_MAGICPOINTS<3> + 1) ]
 
Solution
There is a method called getAbilities (github)

magicLevel = itemType:getAbilities().stats[4]
If you wonder how it works, here is a quick explanation

The function pushes all the stats, skills, absorbs into sub-tables
In this case we need the magicLevel points which is stored on the stats sub-table (git)

So lastly, we need to retrieve the value through a numeric index which in this case is 4 [enum -> (STAT_MAGICPOINTS<3> + 1) ]

Thanks! Love your help! It seems to be working and the script returns correct values of magic level attribute of an item.

I understand now how it works, but how do I know behind which index value hides which attribute? 4 is magic level, how about the rest? Couldn't find any clue in sources, or it's already too late for such tasks for me...
 
Last edited:
I understand now how it works, but how do I know behind which index value hides which attribute?
Just by looking at the enums you can realize which index you need for each attribute

the stats sub_table uses the stats_t enum to create the array <git> - By default, enums start at 0 value
C++:
enum stats_t {
    STAT_MAXHITPOINTS, // 0
    STAT_MAXMANAPOINTS, // 1
    STAT_SOULPOINTS, // 2
    STAT_MAGICPOINTS, // 3

    STAT_FIRST = STAT_MAXHITPOINTS, // 0
    STAT_LAST = STAT_MAGICPOINTS // 3
};
However, those enum values are increased +1 when creating the array on Lua, just to start at index 1 instead 0

That takes us back to the magic level attribute. To parse that attribute value you would use 4 as index -> [STAT_MAGICPOINTS + 1]

Edit -> Here is a full list: Untitled (xjd2hyhe) - PasteCode.io (https://pastecode.io/s/xjd2hyhe)
 
Just by looking at the enums you can realize which index you need for each attribute

the stats sub_table uses the stats_t enum to create the array <git> - By default, enums start at 0 value
C++:
enum stats_t {
    STAT_MAXHITPOINTS, // 0
    STAT_MAXMANAPOINTS, // 1
    STAT_SOULPOINTS, // 2
    STAT_MAGICPOINTS, // 3

    STAT_FIRST = STAT_MAXHITPOINTS, // 0
    STAT_LAST = STAT_MAGICPOINTS // 3
};
However, those enum values are increased +1 when creating the array on Lua, just to start at index 1 instead 0

That takes us back to the magic level attribute. To parse that attribute value you would use 4 as index -> [STAT_MAGICPOINTS + 1]

Edit -> Here is a full list: Untitled (xjd2hyhe) - PasteCode.io (https://pastecode.io/s/xjd2hyhe)

It is very clear now, thank you so much, I am sure that many people will find this useful.

One more thing, which is way beyond my initial question, just came out as a curiosity. Those are the attributes that are taken from the values given in items.xml, right? How about getting also the additional attibutes that were given through a scrip, in-game?
 
One more thing, which is way beyond my initial question, just came out as a curiosity. Those are the attributes that are taken from the values given in items.xml, right? How about getting also the additional attibutes that were given through a scrip, in-game?
Yes, those values are strictly from items.xml

To get modified values on the attribute armor for example --
The most common method is to substract the base value from the current value

Quick example:
Lua:
local baseValue = item:getType():getArmor()
local dynamicValue = item:getAttribute(ITEM_ATTRIBUTE_ARMOR)

if dynamicValue > 0 then -- # if value != 0 it means the armor attribute has been modified
    local modifiedValue = dynamicValue - baseValue
    if modifiedValue < 0 then
        print("the item armor has been reduced in " .. math.abs(modifiedValue) .. " points.")
    else
        print("the item armor has been increased in " .. modifiedValue .. " points.")
    end
else
    print("the item armor has not been modified.")
end
 
Yes, those values are strictly from items.xml

To get modified values on the attribute armor for example --
The most common method is to substract the base value from the current value

Quick example:
Lua:
local baseValue = item:getType():getArmor()
local dynamicValue = item:getAttribute(ITEM_ATTRIBUTE_ARMOR)

if dynamicValue > 0 then -- # if value != 0 it means the armor attribute has been modified
    local modifiedValue = dynamicValue - baseValue
    if modifiedValue < 0 then
        print("the item armor has been reduced in " .. math.abs(modifiedValue) .. " points.")
    else
        print("the item armor has been increased in " .. modifiedValue .. " points.")
    end
else
    print("the item armor has not been modified.")
end

Perfect, you killed it, that's something extra useful!
 
Back
Top