Having Troubles with special functions / script

#include “x2_inc_itemprop”

//Returns the Bonus of the nPropertyType (AC ONLY)
int GetACPropertyBonus(int nValue){
switch(nValue){
case 0: { return 1; } break;
case 1: { return 2; } break;
case 2: { return 3; } break;
case 3: { return 4; } break;
case 4: { return 5; } break;
case 5: { return 6; } break;
case 6: { return 7; } break;
case 7: { return 8; } break;
case 8: { return 9; } break;
case 9: { return 10; } break;
case 10: { return 11; } break;
case 11: { return 12; } break;
case 12: { return 13; } break;
case 13: { return 14; } break;
case 14: { return 15; } break;
case 15: { return 16; } break;
case 16: { return 17; } break;
case 17: { return 18; } break;
case 18: { return 19; } break;
case 19: { return 20; } break;
case 20: { return 20; } break; }
return 0;
}

//--------------------------------------------------------------------
int GetIPValue(object oItem, itemproperty ip){
int nPropertyType = GetItemPropertyType(ip);
switch(nPropertyType){
case ITEM_PROPERTY_AC_BONUS:{
return GetACPropertyBonus(GetItemPropertyCostTableValue(ip));
} break;
case ITEM_PROPERTY_ATTACK_BONUS:{
return IPGetWeaponEnhancementBonus(oItem, ITEM_PROPERTY_ATTACK_BONUS);
} break;
case ITEM_PROPERTY_ENHANCEMENT_BONUS:{
return IPGetWeaponEnhancementBonus(oItem,
ITEM_PROPERTY_ENHANCEMENT_BONUS);
}break;
}
return 0;
}

void main()
{
object oPC = GetLastOpenedBy();
if(!GetIsPC(oPC)){ return; }
object oItem = GetFirstItemInInventory(OBJECT_SELF);
int nType, nVal;
itemproperty ip = GetFirstItemProperty(oItem);
while(GetIsItemPropertyValid(ip)){ nVal = 0;
nType == GetItemPropertyType(ip);
if(nType == ITEM_PROPERTY_AC_BONUS || nType == ITEM_PROPERTY_ATTACK_BONUS ||
nType == ITEM_PROPERTY_ENHANCEMENT_BONUS){
SendMessageToPC(oPC, “\n” + "Property Bonus = " +
IntToString(GetIPValue(oItem, ip)));
}
ip = GetNextItemProperty(oItem); }

}

//==============================

I put one item in a chest with +3 AC +4 AB & +5 EB on one item, this scripts shows 0 Bonus for each property…

Any help on fixing this?

Thanks Genisys / Guile

The problem are the calls to IPGetWeaponEnhancementBonus() because IPGetWeaponEnhancementBonus() uses GetFirstItemProperty()/GetNextItemProperty() to cycle through the item properties but you are already using GetFirstItemProperty()/GetNextItemProperty() and you can’t nest them.

But actually you don’t need IPGetWeaponEnhancementBonus(): you already have the property. To get the bonus look at the implementation of IPGetWeaponEnhancementBonus(). It should show you how to get the bonus. And it should give you an idea how to properly get the AC bonus from the AC bonus property (without that ugly case construct ;)).

Another mistake:
nType == GetItemPropertyType(ip);
should be
nType = GetItemPropertyType(ip);

Your function -

//Returns the Bonus of the nPropertyType (AC ONLY)
int GetACPropertyBonus(int nValue){
switch(nValue){
case 0: { return 1; } break;
case 1: { return 2; } break;
case 2: { return 3; } break;
case 3: { return 4; } break;
case 4: { return 5; } break;
case 5: { return 6; } break;
case 6: { return 7; } break;
case 7: { return 8; } break;
case 8: { return 9; } break;
case 9: { return 10; } break;
case 10: { return 11; } break;
case 11: { return 12; } break;
case 12: { return 13; } break;
case 13: { return 14; } break;
case 14: { return 15; } break;
case 15: { return 16; } break;
case 16: { return 17; } break;
case 17: { return 18; } break;
case 18: { return 19; } break;
case 19: { return 20; } break;
case 20: { return 20; } break; }
return 0;
}

is way too complicated for what you want to achieve, with way too many exit points from the function. Try this instead -

//Returns the Bonus of the nPropertyType (AC ONLY)
int GetACPropertyBonus(int nValue)
{
    int iReturnThis = 20;
    
    if(nValue < 19)
        iReturnThis = 1 + nValue;
        
    return iReturnThis;
}

It could probably be simplified even further by the use of the

?:

operator to this

//Returns the Bonus of the nPropertyType (AC ONLY)
int GetACPropertyBonus(int nValue)
{
    return ((nValue == 20) ? nValue : (nValue + 1));
}

TR

I think this is all you need:

void main()
{
    object oPC = GetLastOpenedBy();
    if(!GetIsPC(oPC)){ return; }
    object oItem = GetFirstItemInInventory(OBJECT_SELF);
    if (GetIsObjectValid(oItem))
    {
        int nType;
        itemproperty ip = GetFirstItemProperty(oItem);
        while(GetIsItemPropertyValid(ip))
        {
            nType = GetItemPropertyType(ip);
            switch (nType)
            {
            case ITEM_PROPERTY_AC_BONUS:
            case ITEM_PROPERTY_ATTACK_BONUS:
            case ITEM_PROPERTY_ENHANCEMENT_BONUS:
                SendMessageToPC(oPC, "\nProperty Bonus = " +
                    IntToString(GetItemPropertyCostTableValue(ip)));
                break;
            }
            ip = GetNextItemProperty(oItem);
        }
    }
}

For a more beautiful output you can use

void main()
{
    object oPC = GetLastOpenedBy();
    if(!GetIsPC(oPC)){ return; }
    object oItem = GetFirstItemInInventory(OBJECT_SELF);
    if (GetIsObjectValid(oItem))
    {
        int nType, nVal;
        string sIPropName;
        itemproperty ip = GetFirstItemProperty(oItem);
        while(GetIsItemPropertyValid(ip))
        {
            nType = GetItemPropertyType(ip);
            switch (nType)
            {
            case ITEM_PROPERTY_AC_BONUS:
            case ITEM_PROPERTY_ATTACK_BONUS:
            case ITEM_PROPERTY_ENHANCEMENT_BONUS:
                nVal = GetItemPropertyCostTableValue(ip);
                // get name of item property
                sIPropName = GetStringByStrRef(StringToInt(Get2DAString("itempropdef", "Name", nType)))
                                + GetStringByStrRef(StringToInt(Get2DAString("iprp_meleecost", "Name", nVal)));
                SendMessageToPC(oPC, "\n"+sIPropName);
                break;
            }
            ip = GetNextItemProperty(oItem);
        }
    }
}

Thank you, that worked excellent, here’s what I ended up putting together…

// You can fetch the +1/+2/etc Bonus for AC / AB / EB only!
int GetPropertyBonus(object oItem, int nProperty = ITEM_PROPERTY_AC_BONUS){
if (GetIsObjectValid(oItem)){
itemproperty ip = GetFirstItemProperty(oItem);
while(GetIsItemPropertyValid(ip)){
if(GetItemPropertyType(ip) == nProperty){
return GetItemPropertyCostTableValue(ip); }
ip = GetNextItemProperty(oItem); } }
return 0; // No bonus if we don’t find a valid property!
}