Aller au contenu

Photo

Loop over inventory items not in containers


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

#1
Psionic-Entity

Psionic-Entity
  • Members
  • 195 messages

Just a simple question. I have a script to check whether or not a PC has an item, but I don't want it to look in any containers (preferrably just certain containers but limiting them all is better than limiting none). Is there any way to do this?



#2
kevL

kevL
  • Members
  • 4 052 messages
// Determine whether oObject has an inventory.
// * Returns TRUE for creatures and stores, and checks to see if an item or placeable object is a container.
// * Returns FALSE for all other object types.
int GetHasInventory(object oObject);

else, show us yer scripz.
  • GCoyote aime ceci

#3
Psionic-Entity

Psionic-Entity
  • Members
  • 195 messages

That function tells me whether an item is a container, what I want to figure out is whether or not an item is inside a container, or how to do a loop over inventory items without counting ones inside containers.



#4
kamal_

kamal_
  • Members
  • 5 238 messages

So you want to check player inventory but do not want to check inside bags of holding the player has, correct?



#5
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

Did you try something similar to this? If I understood correctly, it should do what you're asking for:

void main()
{
	object oPC = OBJECT_SELF;
	object oITEM = GetFirstItemInInventory(oPC);
	int nSLOT = 1;
	string sNAME;
	while (nSLOT <= 128)
	{
		sNAME = GetName(oITEM);
		if (sNAME == "The Item I'm Looking For!")
		{
			// do your magic here!
		} 
		oITEM = GetNextItemInInventory(oPC);
		nSLOT = nSLOT + 1;
	}
}

Of course you may have to change a few things if the caller of the script is not the PC or if you want to identify the item you're looking for by Tag rather than name, but this is the structure more or less.

 

EDIT: You can also try with this one, but since I'm not sure how the inventory cycling works (if it counts empty slots as well or automatically skips to the next item found) the one I posted earlier should be "safer". If the second one works, it's probably more efficient.

void main()
{
	object oPC = OBJECT_SELF;
	object oITEM = GetFirstItemInInventory(oPC);
	string sNAME;
	while (oITEM != OBJECT_INVALID)
	{
		sNAME = GetName(oITEM);
		if (sNAME == "The Item I'm Looking For!")
		{
			// do your magic here!
		} 
		oITEM = GetNextItemInInventory(oPC);
	}
}


#6
Psionic-Entity

Psionic-Entity
  • Members
  • 195 messages

So you want to check player inventory but do not want to check inside bags of holding the player has, correct?

 

Yep, exactly.

 

 

 

Did you try something similar to this? If I understood correctly, it should do what you're asking for:

 

The second one is a standard loop over items in inventory. I'm looking for one that doesn't count items that are in containers such as magic bags.



#7
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

That's the inventory of the PC though, not the one of the bag so it shouldn't count them already no? Or did you test it and it keeps track of containers as well? Admittedly I have not tested this myself.

Edit: just tested it and you're right, it still counts as the PC's inventory even if they are inside a container.



#8
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

Ok, I think I've got this. I tested this script and it should work, tell me how it goes:

void main()
{
   	object oPC = OBJECT_SELF;
   	object oITEM = GetFirstItemInInventory(oPC);
	object oBAG;
   	string sTAG;
	int nBAG;
   	while (oITEM != OBJECT_INVALID)
   	{
		sTAG = GetTag(oITEM);
		if (GetHasInventory(oITEM) == TRUE)
		{
			nBAG = 0;
			oBAG = GetFirstItemInInventory(oITEM);
			while (oBAG != OBJECT_INVALID)
			{
				nBAG = nBAG + 1;
				oBAG = GetNextItemInInventory(oITEM);
			}
		}
		else if (nBAG > 0)
		{
			nBAG = nBAG - 1;
		}
		else if (sTAG == "The tag of the item I'm looking for!")
		{
			// Do your magic here!
		}
   		oITEM = GetNextItemInInventory(oPC);
   	}
}

It may screw up in case of containers inside containers, but if I remember correctly, the NWN2 engine does not allow containers to go inside other containers.



#9
Psionic-Entity

Psionic-Entity
  • Members
  • 195 messages

If I understand correctly that script should work because items in bags always come immediately after the bag item in the inventory loop? If so that's a really useful fact.



#10
Clangeddin86

Clangeddin86
  • Members
  • 220 messages

Yes, I tested it with some floating text with the name of the items and tried moving around containers and items in and out of them and that's what I found out as well.



#11
kevL

kevL
  • Members
  • 4 052 messages
an alternative - from one of TonyK's AI scripts, EquipShield() in 'hench_i0_equip'

    object oItem = GetFirstItemInInventory();
    while (GetIsObjectValid(oItem))
    {
        // skip past any items in a container
        if (GetHasInventory(oItem))
        {
            object oContainer = oItem;
            object oSubItem = GetFirstItemInInventory(oContainer);
            oItem = GetNextItemInInventory();
            while (GetIsObjectValid(oSubItem))
            {
                oItem = GetNextItemInInventory();
                oSubItem = GetNextItemInInventory(oContainer);
            }
            continue;
        }

        // do stuff.

        oItem = GetNextItemInInventory();
    }