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...
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).
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? :)
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();
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.
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.
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.
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.
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.
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.
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.
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?