Author Topic: Item Variable problem.  (Read 327 times)

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
Item Variable problem.
« on: July 13, 2013, 09:49:46 am »


                I have a major problem with item variables. It seems that when you purchase an item from a store, it no longer has it's variable (And I'm using a script that should add the variable if the resref is right, when you equip it, it doesn't seem to work sadly.)

Should I use NWNX or something... ? Or what should I do to make the items' variables be reset when you buy them (or when you save your character and reload the module) ?

Any ideas and help is appreciated greatly.

For the record, the problem I'm having has to do with Black Powder Guns v2. 
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
Item Variable problem.
« Reply #1 on: July 13, 2013, 02:47:17 pm »


               Nevermind, the script that I found was flawed a bit. I modified it today and now it seems to work fine. If anyone else has the variable problem with the black powder guns (When you restart a module, and when you purchase the items from a merchant.) Here's my fix (just replace the gun scripts from guns2_mod_def_equ with this:

   // -------------------------------------------------------------------------
   // Black Powder Gun Hak weapon and ammunition testing
   // -------------------------------------------------------------------------
      object oWeapon = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND,oPC);
      object oAmmunition;
      int nGunTest;            // is this a gun?
      int nGunBulletTest;      // is this ammunition for a gun?
      int nBulletSlot;         // is the ammo based on bolts or bullets?
      int nGunWasLoaded;       // is this gun loaded already?
      int nAmmoLoaded;         // is appropriate ammo loaded?
      int nNPCBullet;          // only NPCs can load this ammo
      int nWeaponType = GetBaseItemType(oWeapon);
      int nGun;
      // match weapon type to ammunition type
      if ((nWeaponType == BASE_ITEM_HEAVYCROSSBOW) || (nWeaponType == BASE_ITEM_LIGHTCROSSBOW))
         {
         oAmmunition = GetItemInSlot(INVENTORY_SLOT_BOLTS, oPC);
         nBulletSlot = INVENTORY_SLOT_BOLTS;
         }
     if (nWeaponType == BASE_ITEM_SLING)
         {
         oAmmunition = GetItemInSlot(INVENTORY_SLOT_BULLETS, oPC);
         nBulletSlot = INVENTORY_SLOT_BULLETS;
         }
      // get remaining variables defining guns and ammunition
         nGunTest = GetLocalInt(oWeapon, "nGun");
         nGunBulletTest = GetLocalInt(oAmmunition, "nGunBullet");
         nGunWasLoaded = GetLocalInt(oWeapon, "nLoadStatus");
         nAmmoLoaded = GetIsObjectValid(oAmmunition);
         nNPCBullet = GetLocalInt(oAmmunition, "nNPCBullet");

      // corrects weapons or ammunition purchased from a merchant,
      // restoring local variables erased by the game engine.
      // NOTE: Weapon and ammunition resrefs must begin with "gun_"

       if ((nGun != 1) && ((GetStringLeft(GetResRef(oWeapon), 4) == "gun_") || (GetStringLeft(GetResRef(oWeapon), 4) == "pst_")))
       {

           SetLocalInt(oWeapon, "nGun", 1);
       }

       if ((nGunBulletTest != 1) && ((GetStringLeft(GetResRef(oAmmunition), 4) == "gun_") || (GetStringLeft(GetResRef(oAmmunition), 4) == "pst_")))
       {
           nGunBulletTest = 1;
           SetLocalInt(oAmmunition, "nGunBulletTest", 1);
       }


      // make sure that weapon and ammunition match

       if (nNPCBullet == 1)
       {
         AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(nBulletSlot, oPC)));
         AssignCommand(oPC, ActionUnequipItem(oWeapon));
         SendMessageToPC(oPC, "Those bullets don't fit your weapon.");
       }

       else if ((nGunTest != 1) && (nGunWasLoaded == 0) && (nGunBulletTest == 1))
       {
         AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(nBulletSlot, oPC)));
         AssignCommand(oPC, ActionUnequipItem(oWeapon));
         SendMessageToPC(oPC, "You don't have a firearm ready.");
       }

       else if ((nGunTest == 1) && (nGunWasLoaded == 0) && (nAmmoLoaded == FALSE))
       {
         SendMessageToPC(oPC, "Your gun isn't loaded.");
       }

       else if ((nGunTest == 1) && (nGunWasLoaded == 0) && (nGunBulletTest != 1))
       {
         AssignCommand(oPC, ActionUnequipItem(GetItemInSlot(nBulletSlot, oPC)));
         AssignCommand(oPC, ActionUnequipItem(oWeapon));
         SendMessageToPC(oPC, "Your gun won't fire that ammunition.");
       }

       else if ((nGunTest != 1) && (nGunWasLoaded == 1) && (nGunBulletTest == 1))
       {
         SetLocalInt(oWeapon, "nLoadStatus", 0);
         AssignCommand(oPC, ActionEquipMostDamagingMelee());
       }

       else if ((nGunTest == 1) && (nGunWasLoaded == 1) && (nGunBulletTest != 1))
       {
         SetLocalInt(oWeapon, "nLoadStatus", 0);
         AssignCommand(oPC, ActionEquipMostDamagingMelee());
       }

       else SetLocalInt(oWeapon, "nLoadStatus", 1);

   // -------------------------------------------------------------------------
   // End of Black Powder Guns code
   // -------------------------------------------------------------------------


   // -------------------------------------------------------------------------
   // Generic Item Script Execution Code
   // If MODULE_SWITCH_EXECUTE_TAGBASED_SCRIPTS is set to TRUE on the module,
   // it will execute a script that has the same name as the item's tag
   // inside this script you can manage scripts for all events by checking against
   // GetUserDefinedItemEventNumber(). See x2_it_example.nss
   // -------------------------------------------------------------------------
   if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
   {
       SetUserDefinedItemEventNumber(X2_ITEM_EVENT_EQUIP);
       int nRet =   ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem),OBJECT_SELF);
       if (nRet == X2_EXECUTE_SCRIPT_END)
       {
           return;
       }
   }
}

First equipping of the item is flawed a little, but it fixes it for the next equip. And the gun's usable again.

Anyone with similar variable problems, use this as a reference to make items set up variables if they miss the variable on equipping.
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
Item Variable problem.
« Reply #2 on: July 13, 2013, 02:49:08 pm »


               Just sharing these because I don't want anyone else to have this problem, since I found it incredibly annoying and it halted my progress for a long while.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Item Variable problem.
« Reply #3 on: July 13, 2013, 06:13:33 pm »


               

JerrodAmolyan wrote...

 I have a major problem with item variables. It seems that when you purchase an item from a store, it no longer has it's variable (And I'm using a script that should add the variable if the resref is right, when you equip it, it doesn't seem to work sadly.)

 
Any ideas and help is appreciated greatly.

 



Since you have stated that you have already solved the problem.  I wiil just list a few of the known problems/pitfalls with variables, Starting with the one that I think stated your original problem.  

Problem 1: Items marked for unlimited sale.  
Items that are marked unlimited in a store are striped of all local variables, This is not a problem for Items that do not have the unlimited flag set.

Problem 2: Stackable  Items
Stacked Items are an interesting case.  It is important to keep in mind that,  a stack of three items is a single instance of the item with the stack size set to three.   This means that all three of the items share the same Var Table.  To further compound the issue NWN does not check to see if Vars differ on items before it stacks them together.  when two items are stacked, the original item instance has its stack size increased and the item being added has its instance destroyed.   This means that both items will not have the same var table as the original item and any data that differed on the added item is simply lost.

Problem 3: Exported Characters
It has been reported that Vars on items in a characters inventory are not being saved to the characters .bic file when exported form single player games.  

It was also noted that if the Item was in a container in the characters inventory that the vars would then be saved.
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
Item Variable problem.
« Reply #4 on: July 13, 2013, 07:16:12 pm »


               Interesting. Thanks for elaborating this. Sadly, on multiplayer modules setting items as infinite is mostly a MUST, since even if it would nto be so, you have to assume there will be more than one player playing there, and missing something like a weapon type entirely (this case: a gun) would be awful. But now that I solved this problem (I'm amazed I managed to, usually when I go and fiddle with things, I manage to destroy it completely.) I think it's going to be all good. Just need to keep track of all items that I assign with variables, and add their variables to reset in the equip item script.
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Item Variable problem.
« Reply #5 on: July 13, 2013, 08:20:06 pm »


               For multiplayer modules, the problems with item variables can be solved with nwnx_fixes (either for Linux or Windows).
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
Item Variable problem.
« Reply #6 on: July 14, 2013, 12:17:08 am »


               I'm thinking about nwnx, actually. I'm just still so far from anything that can be called "done" with the module, so I haven't really yet sacrificed alot of thought for it. As I have mostly no clue how it works or what it actually is. I'd need to read the documentation. The SQL databank stuff seems interesting, I understand you can save stuff there if you use nwnx and then recall that stuff after server resets ?
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Item Variable problem.
« Reply #7 on: July 14, 2013, 01:17:23 am »


               Yep. It seems daunting at first, but it gives you tons of power and flexibility.