Aller au contenu

Photo

Need a GetIsItemInBag


  • Veuillez vous connecter pour répondre
18 réponses à ce sujet

#1
PhantomSkyfire

PhantomSkyfire
  • Members
  • 7 messages
I require a simple function that can determine whether an inputted item is in an inventory container such as a Bag of Holding. I remember having a working function at one point but I seem to have misplaced it and this one always seems to return FALSE:

int GetIsItemInBag(object oItem)
{
    if (GetIsObjectValid(oItem) == FALSE)
    {
         return FALSE; //:: Sanity check!
    }
    else return (GetObjectType(GetItemPossessor(oItem)) == OBJECT_TYPE_ITEM);
}

Any suggestions, fixes, or code snippits?

Modifié par PhantomSkyfire, 13 mars 2011 - 12:49 .


#2
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
I imagine you would need to run two (nested) loops in your code (ie. a loop within another loop). The first loop would look for bag items in the player's inventory (by checking if the found item does have an inventory itself). The second loop would search through the inventory of the found bag.

#3
Baragg

Baragg
  • Members
  • 271 messages
Just to ask, why would you want to check if it is in a bag on the pc, instead of just checking if the pc is in possession of said item?

#4
PhantomSkyfire

PhantomSkyfire
  • Members
  • 7 messages

_Knightmare_ wrote...

I imagine you would need to run two (nested) loops in your code (ie. a loop within another loop). The first loop would look for bag items in the player's inventory (by checking if the found item does have an inventory itself). The second loop would search through the inventory of the found bag.

Augh. I was afraid I'd have to do that. Well, at least this way I know I can find the right bag, instead of just any bag. Thanks..

Baragg wrote...

Just to ask, why would you want to check
if it is in a bag on the pc, instead of just checking if the pc is in
possession of said item?

My module dumps dozens of tokens and OOC widgets onto the player, and to keep it from being overwhelming I'm using a script to auto-sort items OnAcquire. The problem is that OnAcquire runs for every item in a PC's inventory as soon as they enter the module, and I need some sort of check to see if the item is already sorted into a container before I go messing with it.

Modifié par PhantomSkyfire, 09 mars 2011 - 08:23 .


#5
Baragg

Baragg
  • Members
  • 271 messages
I will try to work something up for ya.

#6
Baragg

Baragg
  • Members
  • 271 messages
Do you happen to have a list of tags of the items you are looking to find? I can input the tags directly into the script, and send feedback on any that are not if you wish.

Modifié par Baragg, 10 mars 2011 - 03:39 .


#7
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
I think this may do what you want.  Compiled, Untested.

object GetInInventoryContainer(object oItem, object oBagHolder)
{
  object oContainer = GetFirstItemInInventory(oBagHolder);
  while(GetIsObjectValid(oContainer))
  {
     if (GetHasInventory(oContainer))
     {
        object oInTheBag = GetFirstItemInInventory(oContainer);
        while(GetIsObjectValid(oInTheBag))
        {
          if (oItem == oInTheBag) return oContainer;
          oInTheBag = GetNextItemInInventory (oContainer);
        }
     }
     oContainer = GetNextItemInInventory (oBagHolder);
  }
  // Note: At this point the item is not in a container
  // and oContainer = OBJECT_INVALID
  return oContainer;
} 


#8
Baragg

Baragg
  • Members
  • 271 messages
You beat me to it light here is what I came up with:

[nwscript]
int DetermineIfItemInBag(object oPC, object oItem)
{
object oFirst = GetFirstItemInInventory(oPC);
object oBagItem;
string sTag = GetTag(oItem);

if(GetItemPossessedBy(oPC, sTag) != OBJECT_INVALID)
{//if the PC actually possess the item in their invo somewhere
while(oFirst != OBJECT_INVALID)
{//while the PCs inventory items are valid
if(GetHasInventory(oFirst))
{//if object oFirst has an inventory run through its items
oBagItem = GetFirstItemInInventory(oFirst);
while(oBagItem != OBJECT_INVALID)
{
if(oBagItem == oItem) return TRUE;
else oBagItem = GetNextItemInInventory(oFirst);
}
}
else oFirst = GetNextItemInInventory(oPC);
}
}

return FALSE;
}[/nwscript]

#9
Baragg

Baragg
  • Members
  • 271 messages
K, how do I keep the forums from removing my indents?

#10
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
I use the [ code] [ /code] blocks to keep the endent from going By By. 

One comment on your code.  I see no point for useing: 

if(GetItemPossessedBy(oPC, sTag) != OBJECT_INVALID)

To make sure the item is in the inventory before running the rest of the code.  

The reason is that  GetItemPossessedBy loops through the PC inventory looking for the item.  ( also It may just be another item with the same tag as oItem and not oItem) .  There is really no reason to have that function loop through,  then loop through again with your own code. 

#11
GhostOfGod

GhostOfGod
  • Members
  • 863 messages

Baragg wrote...

K, how do I keep the forums from removing my indents?


I always paste scripts into the standard form as apposed to the quick reply. I haven't had any indent problems even without using "/quote" "/code".

#12
Baragg

Baragg
  • Members
  • 271 messages
Lol, in the old forums I originally used the code blocks, but then learned to use the nwscript blocks to make the script look right. Now I gotta get use to using code blocks again.

#13
PhantomSkyfire

PhantomSkyfire
  • Members
  • 7 messages

Baragg wrote...

Do you happen to have a list of tags of the items you are looking to find? I can input the tags directly into the script, and send feedback on any that are not if you wish.

There is no specific list of tags. My criteria is any item with a tag prefixed with "dmfi_" , "hlslang_", "twig_" or "ttkn". Currently the list does not include any "twig_" or "ttkn_" items, though.

To everyone else: I am a competant scripter and can write simple loops like this on my own, but I apperciate everyone taking their time to help me. Double-checking your code will save me some time from writing my own from scratch, at least.

#14
Baragg

Baragg
  • Members
  • 271 messages
As is often the case with me I tend to over think things and had envisioned a grand script with a listing of tags, and other functions, lol.

#15
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

PhantomSkyfire wrote...

To everyone else: I am a competant scripter and can write simple loops like this on my own, but I apperciate everyone taking their time to help me. Double-checking your code will save me some time from writing my own from scratch, at least.


If you do not want code and a function, you realy should not request a function or code.

PhantomSkyfire wrote...

I require a simple function that can determine whether an inputted item is in an inventory container such as a Bag of Holding. I remember having a working function at one point but I seem to have misplaced it and this one always seems to return FALSE:

.....

Any suggestions, fixes, or code snippits?



#16
PhantomSkyfire

PhantomSkyfire
  • Members
  • 7 messages

Lightfoot8 wrote...

PhantomSkyfire wrote...

To everyone else: I am a competant scripter and can write simple loops like this on my own, but I apperciate everyone taking their time to help me. Double-checking your code will save me some time from writing my own from scratch, at least.


If you do not want code and a function, you realy should not request a function or code.

PhantomSkyfire wrote...

I require a simple function that can determine whether an inputted item is in an inventory container such as a Bag of Holding. I remember having a working function at one point but I seem to have misplaced it and this one always seems to return FALSE:

.....

Any suggestions, fixes, or code snippits?

Well, "to everyone who offered up a code snippit involving a loop" is what I meant when I was refering to simple loops. However, now that you mention it, I suppose I did ask for code snippits so I guess I kinda deserved that.:crying:

#17
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

PhantomSkyfire wrote...

Well, "to everyone who offered up a code snippit involving a loop" is what I meant when I was refering to simple loops. However, now that you mention it, I suppose I did ask for code snippits so I guess I kinda deserved that.:crying:


Well Im Still confused,  Only two people have given code in this thread. Bragg and Me.  So the only Everyone else that I see is ME.  My code was not by any means a snippit.  It is a working function that returns the bag that the item is in.  True it does not return a TRUE/FALSE as requested, it returns a valid/invalid object.  I just thought that having the bag returned would be more usefull then a TRUE/FALSE.   You can always use GetIsObjectValid to return your true false.    

#18
PhantomSkyfire

PhantomSkyfire
  • Members
  • 7 messages

Lightfoot8 wrote...

PhantomSkyfire wrote...

Well, "to everyone who offered up a code snippit involving a loop" is what I meant when I was refering to simple loops. However, now that you mention it, I suppose I did ask for code snippits so I guess I kinda deserved that.:crying:


Well Im Still confused,  Only two people have given code in this thread. Bragg and Me.  So the only Everyone else that I see is ME.  My code was not by any means a snippit.  It is a working function that returns the bag that the item is in.  True it does not return a TRUE/FALSE as requested, it returns a valid/invalid object.  I just thought that having the bag returned would be more usefull then a TRUE/FALSE.   You can always use GetIsObjectValid to return your true false.    


I'm confused now too! I think I'll just give you a hug as thanks for your work and we can get on our way. :wub:

#19
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
Lightfoot8, -1 rep.

Sounds good. Guess I am jumping to the wrong conclusions again.

Have you gotten everything working the way you need it to?