inventory items with weight

Started by Gepard, Sun 04/04/2010 08:12:55

Previous topic - Next topic

Gepard

Hi there, I would like to know how to do an inventory that will handle object properities. So far I managed to add a properties for each object (weight, food, water...).

Now I need my script to calculate how much is my character carrying. Sum up the weight property of each item. I need this because if my character finds a new object and wants to pick it up, the script must check if he can carry it or not:

Code: ags
if (carry+10 <= maxcarryweight) {...


Also, I have a button in inventory that is supposed to recycle/eat/drink selected item... Anyway, after pressing this button I would like to use the getproperty function to subtract item weight from the carrying weight AND lose the selected inventory (active inventory item, because player first needs to select this item and than click the button). In case this is an edible item I want its food or drink property to decrease the hunger or thirst value respectively.

I really dont know how to do this.

Can you help me please? Thx...
Drink up me 'arties! Yo ho!

Khris

#1
Calculating the total weight:

Code: ags
int InvWeight() {
  int i = 1, w, iw;
  while(i <= Game.InventoryItemCount) {
    iw = inventory[i].GetProperty("weight");
    w += iw * player.InventoryQuantity[i];   // add item's weight times quantity to total weight
    i++;
  }
  return w;
}


Recycle button code:
Code: ags
function Button_OnClick(...) {

  if (player.ActiveInventory == null) return;  // exit function if no item is selected

  int t;

  // edible?
  t = player.ActiveInventory.GetProperty("food");   // assuming food is not zero for food and an int
  if (t) hunger -= t;
  if (hunger < 0) hunger = 0;
  
  // drinkable?
  t = player.ActiveInventory.GetProperty("water");   // assuming water is not zero for water and an int
  if (t) thirst -= t;
  if (thirst < 0) thirst = 0;

  player.LoseInventory(player.ActiveInventory);

  carry = InvWeight();  // update inv weight variable
}


This way you can have stuff that quenches hunger and thirst (e.g. fruit).

Gepard

Thanks for the reply!

I ran the code and ran into two problems. I placed the first code into global repeatedly exec. and after I tried saving the game it wrote the classic Nested functions not supported problem (closing brace).

As for the second one a more sophisticated error appeared:

Error: run_text_script1: error -6 running function 'recycle_Click':
Error: Null pointer referenced

Any ideas? Please? :)
Drink up me 'arties! Yo ho!

Crimson Wizard

#3
Gepard, you don't need to put everything to repeatable_execute function. Or rather must not.

Create 2 separate functions as Khris pointed out. Then simply call them in the places you need it.

For example,

Code: ags

int totalweight = InvWeight();


Khris

1. The code doesn't go into another function; it's a function of its own. Just put it in Global.asc outside any function.
Then put this into rep_ex_always:
  carry = InvWeight();
(assuming that carry is a global int variable)

2. Did you get that error in-game?
I forgot to check if player.ActiveInventory is not null; the code above is now corrected.

Crimson Wizard

#5
Khris, you have another mistake, it makes AGS crash:

Code: ags

while(i < Game.InventoryItemCount) {
    iw = inventory[i].GetProperty("weight");
    w += iw * player.InventoryQuantity[i];   // add item's weight times quantity to total weight
    i++;
  }


Must be something like:

Code: ags

while(i < Game.InventoryItemCount) {
    iw = inventory[i].GetProperty("weight");
    w += iw * player.InventoryQuantity[i + 1];   // add item's weight times quantity to total weight
    i++;
  }


InventoryQuantity array has 1-based indexation.

Khris

Hmm, did it actually crash on you?

player.InventoryQuantity[] is 0 if the player doesn't have the item; all that causes is 0 to be added to the total weight, which is exactly what it's supposed to do.

Crimson Wizard

#7
InventoryQuantity array has 1-based indexation. Updated my post above.

AGS manual:
Quote
The array index is the inventory item number, from the Inventory pane in the editor.

monkey0506

CW yours will crash at the end of the loop coz inventory[Game.InventoryItemCount + 1] is outside the appropriate range. You shouldn't add 1 to the index, rather you should initialize i to 1 instead of letting it default to 0.

Crimson Wizard

Quote from: monkey_05_06 on Sun 04/04/2010 14:54:58
CW yours will crash at the end of the loop coz inventory[Game.InventoryItemCount + 1] is outside the appropriate range. You shouldn't add 1 to the index, rather you should initialize i to 1 instead of letting it default to 0.

It won't get to Game.InventoryItemCount + 1. i will always be < Game.InventoryItemCount.

monkey0506

Oh sorry nevermind then. But you could do i = 1, while (i <= Game.InventoryItemCount)..which is what I always do..so sometimes that's what I expect even when other methods produce the same results.. ::) Heh.

Crimson Wizard

Maybe.

Actually I found another one:
Code: ags

iw = inventory[i].GetProperty("weight");


Should be as well:
Code: ags

iw = inventory[i + 1].GetProperty("weight");


Strange thing, I can't find any article related to global "inventory" array in the manual. Is there any?

SMF spam blocked by CleanTalk