Author Topic: remove (1) of a stacked item.  (Read 408 times)

Legacy_lordofworms

  • Sr. Member
  • ****
  • Posts: 422
  • Karma: +0/-0
remove (1) of a stacked item.
« on: January 03, 2012, 03:06:38 pm »


               egad,
first: is it possible to search these forums? I try using the little search box above next to the Jump to: etc drop downs and I never get results..maybe I am looking in the wrong place?
I just hate asking questions if they have already been answered.


ok, my question.
is it possible to remove (1) item from a stack of items when doing scripts?

like if I have (5) flourspar misc items stacked and in an inventory, if I use a
destroyobject or a takeitem it seems to grab the whole stack, not just the (1) I want removed.

is this possible? or should I look into not using stackable items (this is for a potion crafting system I am 98% done with except this bug that kind ruins the effect.)

Thanks everyone, happy new year too!'Image'Image
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #1 on: January 03, 2012, 03:12:06 pm »


               How about this?

// Sets stack size of an item.
// - oItem: item to change
// - nSize: new size of stack.  Will be restricted to be between 1 and the
//   maximum stack size for the item type.  If a value less than 1 is passed it
//   will set the stack to 1.  If a value greater than the max is passed
//   then it will set the stack to the maximum size
void SetItemStackSize(object oItem, int nSize)
               
               

               
            

Legacy_Alex Warren

  • Sr. Member
  • ****
  • Posts: 326
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #2 on: January 03, 2012, 03:27:52 pm »


               int nStack = GetItemStackSize(oItem);
if(nStack == 1)
    DestroyObject(oItem);
else
{
    nStack--;
    SetItemStackSize(oItem, nStack);
}
               
               

               


                     Modifié par Alex Warren, 03 janvier 2012 - 03:30 .
                     
                  


            

Legacy_lordofworms

  • Sr. Member
  • ****
  • Posts: 422
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #3 on: January 03, 2012, 05:14:22 pm »


               wow, THANK you!!  that looks like just the ticket!
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #4 on: July 10, 2013, 08:44:27 pm »


                Okay, after countless hours of searching just this... I still don't get it. I'm learning the basics of scripting and can only do certain things I've seen done before (Generally, I know something works, I have no idea why it works.) 

Well anyway, I'm building my own module and using the script generators which mostly get my basic needs filled, BUT this is something it can't do. It steals the whole damn stack of items if I use the DestroyObject functions. And whatever I've tried and how I've modified it, I just can't get it to remove 1 item from a stack. 

What I'm doing, is I made my custom lock picking and trap disarming system, and I have "Lockpick" items that you can stack (Because failure to open, breaks one, and if I used non-stackables it'd soon populate all of your inventory to carry enough picks.) and that's where I need this. 

This is what I've got so far to work with, it's a modified (and non working) version of Lightfoot8's nice script to remove all the stacked items and pass rewards per stack. So I thought I could reverse it so it removes only 1 item of all the stacks... but of course my skills aren't good enough. If someone would give me a ready script with everything that it needs, and some simple explanations (not necessary, but it would be great '<img'>) as to why and how it works so I can learn this myself, that'd be seriously appreciated. (Attached is what I tried to do, turns out this is only a more complicated "destroy every item in stack with this tag" script, lol.)

#include "nw_i0_plot"
void main()
{   
string sTag = "jer_rg_lockpick"; // Tag of items to take.   
object oPC = GetPCSpeaker();   
int nToTake = 1; // Number of items to take.   
if ( nToTake) 
  {     
TakeNumItems(oPC,sTag,nToTake);
  }
}

Basicly, what I got under work, is a custom treasure chest system (for locked chests) that, once you "use" the chest, it brings up a conversation. On the conversation, you can pick: Try to pick the lock, try to force the lock and see if it is trapped. I've got the chest have variables (IsLocked) and (IsTrapped) Where "IsLocked" is 1 and "IsTrapped" is 0. So when the convo gets up, it rolls a dice for the trap (currently 36% possibility for the chest to be trapped) and if it does hit the precent, it sets IsTrapped to 1, which means that you will trigger the trap (1d100 dmg) if you try to force or pick the lock without seeing the trap and disarming it (This has tools too. A probe for Search and a Trap Toolkit for disarming, without them, you can't do it.) To see the trap, you must pass a DC (depends on the chest) for search. (All this done in conversation) and you can't see the roll and if it's successfull '<img'> But if you can't see it, and if there is no trap to see, you get "You spot no trap". Or if you see it, "You spot a trap" and then you can disarm it if you pass the check and have the toolkit. If you fail, it explodes on your face. '<img'> 
Similar checks and all for the lockpicking, if you fail, you should lose 1 pick because it breaks. When the trap is disarmed or the lock is picked, those variables are set to 0 (And the chest will reset after a time to locked state after you close it. and sets the variable to 1 again) I think this idea I have is a fun one, and everything else seems to work except this lockpick breaking script. I hope I provided enough info for you guys to help me, heh. I'm looking for an "On actions taken" from conversation script that takes just 1 item from the stack of lockpicks. Thanks in advance.
               
               

               


                     Modifié par JerrodAmolyan, 10 juillet 2013 - 07:59 .
                     
                  


            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #5 on: July 10, 2013, 09:23:30 pm »


               1: Try to create new threads instead of resurrecting old ones.
2: If you must commit thread necromancy, be sure to read the thread you're posting in first.

Alex's post contained the solution to your problem. Modifying it for your purposes would leave you something like this:

void TakeItemsFromStack(object oItem, int nItemsToTake)
{
    int nStack = GetItemStackSize(oItem);
   
    if (nStack <= nItemsToTake)
        DestroyObject(oItem);
    else
        SetItemStackSize(oItem, nStack - nItemsToTake);
}

void main()
{    
    object oPC   = GetPCSpeaker();    
    object oItem = GetFirstItemInInventory(oPC);

    while (GetIsObjectValid(oItem))
    {
        if (GetTag(oItem) == "jer_rg_lockpick")
        {
            TakeItemsFromStack(oItem, 1);
            break;
        }
           
        oItem = GetNextItemInInventory(oPC);
    }
}

               
               

               


                     Modifié par Squatting Monk, 10 juillet 2013 - 08:25 .
                     
                  


            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #6 on: July 10, 2013, 09:35:48 pm »


               Thanks, I'll give that a shot ! Yeah I know this is an old thread. But I figured I won't make a new thread since there's one about it already.

I did read this, but that's just it. I'm a total noob at scripts, I don't know almost anything so I needed help '<img'>
               
               

               
            

Legacy_JerrodAmolyan

  • Full Member
  • ***
  • Posts: 175
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #7 on: July 10, 2013, 09:38:10 pm »


               And it totally works ! Thanks mate, you're my new hero ! '<img'>
               
               

               
            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
remove (1) of a stacked item.
« Reply #8 on: July 10, 2013, 09:46:49 pm »


               No problem. Everyone's gotta start somewhere. Glad it worked for you.