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?
Loop over inventory items not in containers
#1
Posté 30 janvier 2016 - 08:12
#2
Posté 30 janvier 2016 - 08:22
// 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
Posté 31 janvier 2016 - 01:39
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
Posté 31 janvier 2016 - 01:44
So you want to check player inventory but do not want to check inside bags of holding the player has, correct?
#5
Posté 31 janvier 2016 - 03:33
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
Posté 02 février 2016 - 07:07
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
Posté 02 février 2016 - 10:54
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
Posté 02 février 2016 - 11:53
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
Posté 03 février 2016 - 05:16
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
Posté 03 février 2016 - 06:14
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
Posté 03 février 2016 - 08:09
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();
}





Retour en haut






