Author Topic: Frustrated attempt at counting items in a placable's inventory.  (Read 396 times)

Legacy_Loathesome Pickle

  • Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« on: February 28, 2012, 03:03:09 pm »


               Hello again,

 I've been trying to get this working for days but it seems no matter how I change things it -refuses- to give me what I need. 

 What I need it to do is to check the inventroy and look for objects tagged as cooking ingredients (hence "mzs_cooking_") Then I need to know if those items are in excess of what the container can hold (defined by the iCapacity variable) and we can go from there. However it's just -not- printing anything out when I try to load it. I've placed this script on a placables 'on close' for reference. Any help would be -hugely- appriciated. It's driving me quite litterally mad. >.>

int GetCookingItems(object oTarget)
{

   int nNumItems = 0;
   object oItem = GetFirstItemInInventory(oTarget);
   string sObjects = GetTag(oItem);
   string sIngredientL = GetStringLeft(sObjects, 12);

   while (GetIsObjectValid(oItem) == TRUE)
   {
       if (sIngredientL == "mzs_cooking_")
       {
           nNumItems +1;
       }
       oItem = GetNextItemInInventory(oTarget);

   }

  return nNumItems;
}

void main()
{
object oPC = GetLastClosedBy();
object oCooker = OBJECT_SELF;
object oContents = GetFirstItemInInventory(OBJECT_SELF);

string sObjects = GetTag(oContents);
string sIngredientL = GetStringLeft(sObjects, 12);
int nNumItems;
int iCount;
int nMarkCount = 1;

string sCount = IntToString (nNumItems);


   if(GetCookingItems(oCooker) >= nMarkCount)
   {
   SendMessageToPC (oPC, "Too much " +sCount+ " ");
   }
}
               
               

               
            

Legacy_Alex Warren

  • Sr. Member
  • ****
  • Posts: 326
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #1 on: February 28, 2012, 03:20:56 pm »


               Tag test must be inside while loop. The script should look like this:

void main()
{
object oTest = GetFirstItemInInventory(OBJECT_SELF);
int iCount = 0;
while(GetIsObjectValid(oTest))
{
    if(GetStringLeft(GetTag(oTest), 12) == "mzs_cooking_")
{
        iCount++;
}
    oTest = GetNextItemInInventory(OBJECT_SELF);
}
SendMessageToPC (GetLastClosedBy(), "Cooking item count " +IntToString(iCount)+ " ");
}

*I haven't compiled/tested this script. It's just an example.
               
               

               


                     Modifié par Alex Warren, 28 février 2012 - 03:22 .
                     
                  


            

Legacy_Loathesome Pickle

  • Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #2 on: February 28, 2012, 03:33:55 pm »


               Huh, so much easier to read and it works.

Thanks a lot, Alex. I'll have to keep this in mind in the future. Much obliged!
               
               

               
            

Legacy_Loathesome Pickle

  • Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #3 on: February 29, 2012, 04:13:59 am »


               Alright. . . I've been messing about with this all day and I'm still no closer to getting it to work as I need it to. '<img'>
 Sorry to sound like a pain but uh. . . Well, here goes. What I need to do is get variables (ints in this case) from the loop so I know what ingredients we're trying to cook. I've tried several different things but nothing seems to return a useful/usable thing. Can anyone toss some ideas my way that might help? I've been poking around on the internet but all I'm finding are tutorials or -very- complex (primarily forge) scripts on the Vault that're just way, way out of my leauge tat this moment. Heh Any help would be greatly appriciated!

while(GetIsObjectValid(oTest))
{

   if(GetStringLeft(GetTag(oTest), 12) != "mzs_cooking_")
   {
   SendMessageToPC (GetLastClosedBy(), "You can't cook this.");
   return;
   }

   if(GetTag(oTest) == "mzs_cooking_base")
   {
   iBaseCount ++;
   }

   if(GetStringLeft(GetTag(oTest), 12) == "mzs_cooking_" &&  GetStringRight (GetTag(oTest), 5) != "_base")
   {

//Trying to get ints from individual items, in any order, hasn't worked here.
   iCount ++;
   }


oTest = GetNextItemInInventory(OBJECT_SELF);
}
   if (iBaseCount == 0)
   {
     SendMessageToPC (GetLastClosedBy(), "You need something to cook in.");
   }

   else if (iBaseCount > 1)
   {
     SendMessageToPC (GetLastClosedBy(), "You can't have more than one cooking appliance on the stove at a time.");
   }

   else if (iBaseCount = 1)
   {
       if(iCount > iCapacity)
       {
       SendMessageToPC (GetLastClosedBy(), "There's too much stuff to cook properly.");
       return;
       }

           else if (iCount <= iCapacity)
           {
           SetLocalString (OBJECT_SELF, "Cooking", sBaseType);

           string sIng1 = GetTag(oTest);
           SetLocalString (OBJECT_SELF, "Ingredient1", "" +sIng1+ "");

//Nor here.

           SendMessageToPC (GetLastClosedBy(), "DEBUG: Base Type: " +sBaseType+ "");
           SendMessageToPC (GetLastClosedBy(), "DEBUG: Capacity is: " +IntToString(iCapacity)+ ", Item count is: " +IntToString(iCount)+ " ");
           }
    }

               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #4 on: February 29, 2012, 04:50:43 am »


               Well I kinda cleaned up and fixed a couple things. You didn't have all the script posted so I just put the missing variables at the top and gave em generic definitions. Probably can clean it up a bit more but not sure what the situation is. Is this an OnClosed script?

Edit: Ok just reread above and cleaned the script up a bit more. Hopefully it gets you closer.


void main()
{
    object oPC = GetLastClosedBy();
    object oPot = OBJECT_SELF;
    int iCount = 0;//?
    int iBaseCount = 0;//?
    int iCapacity = 0;//?
    string sBaseType = "";//?
    object oTest = GetFirstItemInInventory(oPot);

    while(GetIsObjectValid(oTest))
    {

        if(GetStringLeft(GetTag(oTest), 12) != "mzs_cooking_")
        {
            SendMessageToPC (oPC, "You can't cook this.");
            return;
        }

        if(GetTag(oTest) == "mzs_cooking_base")
        {
            iBaseCount ++;
        }

        if (GetStringLeft(GetTag(oTest), 12) == "mzs_cooking_" &&
            GetStringRight (GetTag(oTest), 5) != "_base")
        {
            //Trying to get ints from individual items, in any order, hasn't worked here.
            iCount ++;
        }
        oTest = GetNextItemInInventory(oPot);
    }

    if (iBaseCount == 0)
    {
        SendMessageToPC (oPC, "You need something to cook in.");
    }

    else if (iBaseCount > 1)
    {
        SendMessageToPC (oPC, "You can't have more than one cooking appliance on the stove at a time.");
    }

    else if (iBaseCount == 1)
    {
        if(iCount > iCapacity)
        {
            SendMessageToPC (oPC, "There's too much stuff to cook properly.");
            return;
        }

        else if (iCount <= iCapacity)
        {
            SetLocalString (OBJECT_SELF, "Cooking", sBaseType);

            string sIng1 = GetTag(oTest);
            SetLocalString (OBJECT_SELF, "Ingredient1", "" +sIng1+ "");

            //Nor here.

            SendMessageToPC (oPC, "DEBUG: Base Type: " +sBaseType+ "");
            SendMessageToPC (oPC, "DEBUG: Capacity is: " + IntToString(iCapacity)+
                                                 ", Item count is: " + IntToString(iCount)+ " ");
        }
    }
}
               
               

               


                     Modifié par GhostOfGod, 29 février 2012 - 06:16 .
                     
                  


            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #5 on: February 29, 2012, 02:08:17 pm »


               

GhostOfGod wrote......
   else if (iBaseCount = 1) 

There's a missing =

else if (iBaseCount == 1)

 
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #6 on: February 29, 2012, 06:14:42 pm »


               

Failed.Bard wrote...

GhostOfGod wrote......
   else if (iBaseCount = 1) 

There's a missing =

else if (iBaseCount == 1)

 


Oh I missed that one. Good caatch. Fixed.
               
               

               


                     Modifié par GhostOfGod, 29 février 2012 - 06:15 .
                     
                  


            

Legacy_Loathesome Pickle

  • Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #7 on: March 01, 2012, 01:03:35 pm »


               Morning and thanks for the replies!

 What I'm trying to do is set it up so that the loop goes through the inventory of the script caller and finds out which ingredients are inside. After that it writes the ingredient list to the cooker itself so they can be retrieved later. However I'm not entirely sure how to do that. . . Would I require another loop or something else? So far all I've been able to get out of it is the first item it finds. Is there any way to retrieve -all- the items within the inventory?

 Something maybe akin to

while (GetIsObjectValid (oInv))
 {
sItemTag = GetTag(oInv);
sItemName = GetName(oInv, FALSE);

SetLocalString ("Ingredient 1", sItemName);
   
        // Debug Line
         SpeakString ("" + sItemTag + ":" + sItemName + "");
         oInv = GetNextItemInInventory(oCooker); 
}

 I've tried this one and variations on it but I never seem to get it to write the list of all inventory items within the caller. It needs to specifically ignore any 'base' items (tagged mzs_cooking_base) but retain all the ingredient items. (usually tagged mzs_cooking_ingredient) there are variables on the Ingredient items themselves that identify them as what they are. That'll be passed to the oven placable and then handled by another script later on in the process.


 Sorry if this is a bit confusing, I haven't had my morning coffee. ':?'
               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #8 on: March 01, 2012, 08:23:00 pm »


               Below fixes your system so that it will record all ingredients and have an identifier telling how many ingredients to look for.

void main()
{
int nCount = 0;
while (GetIsObjectValid (oInv))
{
 nCount++;
 sItemTag = GetTag(oInv);
 sItemName = GetName(oInv, FALSE);
 SetLocalString (OBJECT_SELF, "Ingredient " + IntToString(nCount), sItemName);
 // Debug Line
 SpeakString ("" + sItemTag + ":" + sItemName + "");
 oInv = GetNextItemInInventory(oCooker);
 }
if(nCount) SetLocalInt(OBJECT_SELF, "Total Ingredients", nCount);
}
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #9 on: March 02, 2012, 01:42:40 am »


               

WhiZard wrote...

Below fixes your system so that it will record all ingredients and have an identifier telling how many ingredients to look for.

void main()
{
int nCount = 0;
object oInv = GetFirstItemInInventory(oCooker);
while (GetIsObjectValid (oInv))
{
nCount++;
sItemTag = GetTag(oInv);
sItemName = GetName(oInv, FALSE);
SetLocalString (OBJECT_SELF, "Ingredient " + IntToString(nCount), sItemName);
// Debug Line
SpeakString ("" + sItemTag + ":" + sItemName + "");
oInv = GetNextItemInInventory(oCooker);
}
if(nCount) SetLocalInt(OBJECT_SELF, "Total Ingredients", nCount);
}


               
               

               


                     Modifié par Lightfoot8, 02 mars 2012 - 01:43 .
                     
                  


            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #10 on: March 02, 2012, 08:01:27 pm »


               <waiting...>

Er, where is oCooker defined?

<...until he can't wait no more>
               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #11 on: March 02, 2012, 10:35:42 pm »


               

Rolo Kipp wrote...

<waiting...>

Er, where is oCooker defined?

<...until he can't wait no more>


It isn't declared, nor are sItemTag or sItemName declared as strings.  I am assuming what was posted was a short snippet from a longer script
               
               

               
            

Legacy_Loathesome Pickle

  • Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Frustrated attempt at counting items in a placable's inventory.
« Reply #12 on: March 03, 2012, 03:33:08 am »


               Oh crud-- yeah, oCooker is really just an OBJECT_SELF reference. And the two strings I forgot to put in the script as I tend to have a very abstract method when I'm trying to get my head around something I'm not familiar with. Whoops >.>;;;

Well, thanks a lot for the help! I'll see if I can't get something workable and then I'll post it up here incase someone needs it in the future. Thanks a lot for your time!