Author Topic: Check if PC has a weapon in inventory or equipped  (Read 431 times)

Legacy_Zeke

  • Jr. Member
  • **
  • Posts: 86
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« on: December 09, 2015, 07:17:23 pm »


               

I'm creating a "Text appears when..." check in a conversation line (logically it says [Slit his throat]) that should only be visible when the PC has a weapon in hands or inventory. 


 


I'm using Get2DAString() function in a loop. I read somewhere that it doesn't have good performance when used in a loop but I can't think of a better way.



int StartingConditional()
{
    int iResult;
    int i;

    object oPC = GetPCSpeaker();
    int iBaseType;

    object oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem)) {
        iBaseType = GetBaseItemType(oItem);
        if ( StringToInt(Get2DAString("baseitems", "WeaponType", iBaseType)) ){
            iResult = TRUE;
        }
        oItem = GetNextItemInInventory(oPC);
    }

    for (i = 0; i < NUM_INVENTORY_SLOTS; ++i) {
        oItem = GetItemInSlot(i, oPC);
        iBaseType = GetBaseItemType(oItem);
        if ( StringToInt(Get2DAString("baseitems", "WeaponType", iBaseType)) ){
            iResult = TRUE;
        }
    }

    return iResult;
}

So far so good, but how can I check if the PC can equip the item? It's nice if he has a Battle Axe in his backpack but I don't want the conversation line to show if he is not able to wield it.



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #1 on: December 09, 2015, 07:41:14 pm »


               

You could make it so that the option is only present if the weapon is equipped. This solves two of your problems. You can remove the whole first loop and the second one is really not needed as you only need to check INVENTORY_SLOT_RIGHTHAND and LEFTHAND for a weapon. And since you are checking for equipped the PC can obviously equip the weapon.


 


After all, it's hard to slit someone's throat with a weapon that's in your backpack still...


               
               

               
            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #2 on: December 09, 2015, 09:50:36 pm »


               

There are existing functions that check for weapons.


 


GetWeaponRanged() checks if an item is a ranged weapon


 


GetMeleeWeapon() - called from x2_i0_spells - checks if an item is a melee weapon


 


You could use these function in a loop to check if the item currently being checked is a melee or ranged weapon. Your script would look like so...


 



   Spoiler
   



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #3 on: December 09, 2015, 10:14:40 pm »


               

GetMeleeWeapon and GetIsRangedWeapon (note the difference in names) don't work with custom content which added new weapon lines. But that would work. Personally I'd probably code it to check for slashing weapon type instead since it would be hard to slit a throat with a sling.


 


There's still no point in looking at all the inventory slots.


 


"if (GetIsRangedWeapon(oItem) == TRUE) " is redundant.   "If (GetIsRangedWeapon(oItem))" is sufficient and should be a few instructions shorter.


 


Edit: fixed to use right function name in examples.



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #4 on: December 09, 2015, 10:16:50 pm »


               In baseitems.2da, if it is a weapon, check that the PC has one of the wielding feats specified in the columns ReqFeat0 to ReqFeat4.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #5 on: December 09, 2015, 11:15:20 pm »


               


In baseitems.2da, if it is a weapon, check that the PC has one of the wielding feats specified in the columns ReqFeat0 to ReqFeat4.




its more, you have to check not only feat prerequsities, but also character size compared to weapon size, item level restriction and any kind of possible itemproperty limitations like race or class and umd if applicable


 


from this reason is much better to only check currently equipped weapon, althought it would be doable but will be very hard to code.


 


nwnx can do this easier but no public plugin has this functionality anyway so...


               
               

               
            

Legacy_Pstemarie

  • Hero Member
  • *****
  • Posts: 4368
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #6 on: December 09, 2015, 11:55:33 pm »


               


GetMeleeWeapon and GetIsRangedWeapon (note the difference in names) don't work with custom content which added new weapon lines. But that would work. Personally I'd probably code it to check for slashing weapon type instead since it would be hard to slit a throat with a sling.


 


There's still no point in looking at all the inventory slots.


 


"if (GetRangedWeapon(oItem) == TRUE) " is redundant.   "If (GetRangedWeapon(oItem))" is sufficient and should be a few instructions shorter.




 


Good catch about custom content - I didn't think of that.


               
               

               
            

Legacy_Zeke

  • Jr. Member
  • **
  • Posts: 86
  • Karma: +0/-0
Check if PC has a weapon in inventory or equipped
« Reply #7 on: December 10, 2015, 08:39:19 am »


               

Thanks for all the suggestions!


 


Hmm, after reading all the advice, I think that really the Item Equipped in INVENTORY_SLOT_RIGHTHAND and LEFTHAND check is the most straightforward and at the same time logical way to do this.