Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Gepard on Sun 04/04/2010 08:12:55

Title: inventory items with weight
Post by: Gepard on Sun 04/04/2010 08:12:55
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:

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...
Title: Re: inventory items with weight
Post by: Khris on Sun 04/04/2010 13:48:37
Calculating the total weight:

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:
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).
Title: Re: inventory items with weight
Post by: Gepard on Sun 04/04/2010 14:17:19
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? :)
Title: Re: inventory items with weight
Post by: Crimson Wizard on Sun 04/04/2010 14:27:21
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,


int totalweight = InvWeight();

Title: Re: inventory items with weight
Post by: Khris on Sun 04/04/2010 14:29:09
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.
Title: Re: inventory items with weight
Post by: Crimson Wizard on Sun 04/04/2010 14:44:44
Khris, you have another mistake, it makes AGS crash:


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:


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.
Title: Re: inventory items with weight
Post by: Khris on Sun 04/04/2010 14:48:12
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.
Title: Re: inventory items with weight
Post by: Crimson Wizard on Sun 04/04/2010 14:51:16
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.
Title: Re: inventory items with weight
Post by: monkey0506 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.
Title: Re: inventory items with weight
Post by: Crimson Wizard on Sun 04/04/2010 14:57:13
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.
Title: Re: inventory items with weight
Post by: monkey0506 on Sun 04/04/2010 14:58:33
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.
Title: Re: inventory items with weight
Post by: Crimson Wizard on Sun 04/04/2010 16:00:42
Maybe.

Actually I found another one:

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


Should be as well:

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?