Easier way to do this (item quantities + custom scripts) **ON HOLD**

Started by Knox, Thu 17/02/2011 15:07:19

Previous topic - Next topic

Knox

Ok, I've been working on this on my own and so far so good, but I've run into a problem I've ran into before...I'm sure Khris you'll see its the same kind of problem I run into often in AGS (because I still have a hard time undoing my old ways of seeing variables). Here is what I have, it's a system to update item quantities and playing random messages when the player has reached the maximum amount for that item:

Code: ags
void randMaxMessage(String sThisItem)
{
  int iRand = Random(4);
  if (iRand == 0) Display("You've got as many %s as you can possibly carry...", sThisItem);
  else if (iRand == 1) Display("You can't hold anymore %s...", sThisItem);
  else if (iRand == 2) Display("That's enough %s...", sThisItem);
  else if (iRand == 3) Display("You dont' need to carry any more %s...", sThisItem);
  else if (iRand == 4) Display("You've got enough %s...", sThisItem);
}
  
void setMaxQuantity(int iItemQVar, int iMaxQuantity, int iThisAmount, String sItem)
{
  if (iItemQVar <= iMaxQuantity) iItemQVar = iItemQVar + iThisAmount;
  else randMaxMessage(sItem);
}

void AddInventory_Quantity(this InventoryItem*, int iAmount)
{
  //Display("Current clipQuantity is: %d...add %d to that number", iClipQuantity, iAmount);
  if (this == iPatrolClip) setMaxQuantity(iClipQuantity, 8, iAmount, "magazine clips");
  else if (this == iTaserCartridge) setMaxQuantity(iTaserCartridgeQuantity, 8, iAmount, "cartridges");
  else if (this == iAlcoholPrepPad) setMaxQuantity(iAlcoholPrepPadQuantity, 8, iAmount, "prep pads");
  else if (this == iBenzalkoniumWipe) setMaxQuantity(iBenzalkoniumWipeQuantity, 8, iAmount, "wipes");
  else if (this == iBusinessCard) setMaxQuantity(iBusinessCardQuantity, 8, iAmount, "business cards");
}

int Quantity(this InventoryItem*)
{
  if (this == iPatrolClip) player.InventoryQuantity[this.ID] = iClipQuantity;
  else if (this == iTaserCartridge) player.InventoryQuantity[this.ID] = iTaserCartridgeQuantity;
  else if (this == iAlcoholPrepPad) player.InventoryQuantity[this.ID] = iAlcoholPrepPadQuantity;
  else if (this == iBenzalkoniumWipe) player.InventoryQuantity[this.ID] = iBenzalkoniumWipeQuantity;
  else if (this == iBusinessCard) player.InventoryQuantity[this.ID] = iBusinessCardQuantity;
  
  iQ = player.InventoryQuantity[this.ID];
  return iQ;
}


The problem area is when I reach this line:  iItemQVar =  iItemQVar + iThisAmount;
I want to set my global variable (that is "inside" a variable iItemQVar) to its amount plus a new iThisAmount...

The problem is, of course, iItemQVar doesnt store the name of  the global variable, it would be the same as saying "8 = 8 + x"...how do I do it so I can store the value of the global variable (ex, iPatrolClipQuantity) AND its name so it can be used in that fashion? grrrr. I wish I could use variables in AGS this way, sniff sniff  :'(

I still have trouble grasping this concept, so be please be easy one me...it'll eventually sink in! :P
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

First let me get straight what you want to achieve:

There are several things the player can carry an amount of 8 tops, right?
And if you want to add, say 3 clips, and the player has 7, they'll get 1?
And if they already have 8, it says "You can't hold anymore x"?

Because even if the code you're trying to use did work, in the former case, they'd end up with 10. So I just wanna make sure I understand what you're trying to do.

Knox

Ive got this line that I call when I want to add a certain amount of an inventory item:

Code: ags
iPatrolClip.AddInventory_Quantity(1, 8);


So it will add 1 item to whatever amount I already have of it. If I already have 9 clips, it wont add anymore and give me the "maximum message".

Wow, I just realized though I need a "check" to the iAmount cause if I entered:
Code: ags
iPatrolClip.AddInventory_Quantity(500, 8);

that would not keep the maximum at 9...(8 here means 9)

The "8" number actually translates to 9 max (not sure why, maybe because I have a while loop set at 0 and not 1 or something)...this number will change depending on the item, so the max number of patrol clips could actually be 4, the benzalkonium wipes 5, etc...

I just havent worked out those numbers yet since I rather lay down my system first (I only have a few items that can have multiple copies, but that list isnt complete yet). Once I know it works, I can set the maximum to any number I want to any item.




**EDIT:

I just thought about it a second...the maximum quantity of items should always be 9...forget what I said above.

**2nd EDIT:
Also, the global variables "iPatrolClip, iBusinessCard", etc...need to exist because I need to store their previous values so I can compare those values with updated ones with the "player.InventoryQuantity[this.ID]" later on in other scripts.
--All that is necessary for evil to triumph is for good men to do nothing.

Khris

In that case you don't even need to use any variables at all.

Code: ags
void AddInventory_Quantity(this InventoryItem*, int iAmount)
{
  int current = player.InventoryQuantity[this.ID];
  if (current >= 9) randMaxMessage(this.Name);
  else {
    int to_be = current + iAmount;
    if (to_be > 9) to_be = 9;
    player.InventoryQuantity[this.ID] = to_be;
    UpdateInventory();
  }
}


Edit: just saw the 2nd edit, do you mean the i___Quantity variables? Why do you need to store inventory quantities in a separate variable?

Knox

Hi Khris,

Ok, I need some time to look at all of this and think some more...Your code might be saving me HUGE amounts of time, but I need to rethink/restructure a few other related things which is a bit daunting cause its connected to quite a few custom scripts I already wrote...doh!

I'll keep you posted though  :)
--All that is necessary for evil to triumph is for good men to do nothing.

SMF spam blocked by CleanTalk