hand over all inv items to another character? [SOLVED]

Started by spook1, Wed 14/06/2006 22:28:50

Previous topic - Next topic

spook1

I want to hand all the inv items the players has colected over to another character.

I want to do something like:

for i = 0 to cEgo.InventoryItemCount{
Ã,  cSister.InventoryQuantity = cEgo.InventoryQuantity
next i

can this be done??

monkey0506

#1
Code: ags
int i = 0;
while (i < Game.InventoryItemCount) { // or, i < GetGameParameter(GP_NUMINVITEMS, 0, 0, 0)
  cSister.InventoryQuantity[i] += cEgo.InventoryQuantity[i];
  cEgo.InventoryQuantity[i] = 0;
  i++;
  }
UpdateInventory();


That will transfer all of Ego's inventory items to Sister (increasing Sister's inventory so that if she already has, say, 10 coins, and Ego has 20, she will have 30, not 20; and then setting Ego's inventory quantity to 0 (since he is giving them to her)).

[EDIT:]

There is no for loop in AGS (yet), so you can use a while loop instead.

And if Game.InventoryItemCount returns an error, that is because it wasn't implemented until AGS 2.72 BETA 3 (AGS 2.72 is still unreleased and can be downloaded here).  If you are using an older version of AGS you can use the old-style command, GetGameParameter(GP_NUMINVITEMS, 0, 0, 0) in place of Game.InventoryItemCount.

Hope this helps  ;)

[EDIT:]

Just a thought, if you are going to be doing inventory transfers often throughout the game, you may consider this custom function:

Code: ags
import bool TransferInventory(Character *From, Character *To, bool Copy = false);

bool TransferInventory(Character *From, Character *To, bool Copy) {
  if ((From == null) || (To == null)) return false;
  int i = 0;
  bool break = false;
  while (i < Game.InventoryItemCount) {
    if (From.InventoryQuantity[i] != 0) break = true;
    i++;
    }
  if (!break) return false;
  i = 0;
  while (i < Game.InventoryItemCount) {
    To.InventoryQuantity[i] += From.InventoryQuantity[i];
    if (!Copy) From.InventoryQuantity[i] = 0;
    i++;
    }
  UpdateInventory();
  return true;
  }


The function will return false if either Character is null or if the "From" Character does not have any inventory items to transfer.  It will return true otherwise (after transfering all of the Character's inventory).

From and To are pointers to Characters (Character*), such as cEgo, character[5], or player.  Copy is an optional boolean value which if set to true will only make a copy of the inventory (the items will not be removed from the From character's inventory).  Because it is optional, you do not have to include it when calling the function, and will default to false (clearing the From Character's inventory on transfer).

And please don't take my explanations in a derrogatory manner, there are a lot of forum users who don't understand these types of things, so if you did already know about them, please don't take it as an insult.  If you didn't understand it, please feel free to ask if you need further explanation (though my internet time of late is very limited, there are a lot of others who I'm sure would be glad to help :=)

[EDIT:]

D'oh!  Forgot that you have to use an import to make optional parameters in AGS.  If you place the import statement in the script header, and the function in the global script, you will be able to call it from all of your room scripts as well as the global script.  You could also move it to a module if you need to access it from a module script (placing it in or above the module you need to call it from, and removing it from the global script and script header).

[EDIT:]

Thanks to Ashen for pointing out the need to call UpdateInventory.

spook1

Thanks a lot for the very elaborate answer.
I have tried your solution, but still I cannot see the inventory.
Is it that I am looking a wrong inventory window or something?

I think I transferred all inventories to cBb11, but yet in the window I see an empty inventory window.

The inventroy window is custom created.

Any suggestions??


here is what I do:

Code: ags

function room_b() {
  // script for Room: Player enters room (before fadein)
  
character[BB11].SetAsPlayer(); 


int i = 1;
while (i < Game.InventoryItemCount) {
     cBb11.InventoryQuantity[i] = cEgo.InventoryQuantity[i] ;
     i++;
}
SetFlashlightDarkness (GetFlashlightMinLightLevel() + 255);

SetMouseCursor(eModeInteract);

Ashen

Two things:
1) Does the Inventory Window have an Owning Character set? (Check the 'Character ID' property in the GUI editor. -1 means it changes with the player character, otherwise it's locked to a specific character.)

2) Probably more likely:
Quote from: The Manual, InventoryQuantityUsually, you should use the AddInventory and LoseInventory functions to modify the character's inventory; however, if you need to add or remove a large number of items in one go, directly changing this array can be an easier method.
If you change this array directly, the on-screen inventory will not be updated. In this case, you must call UpdateInventory to see any new or removed items.
So, what if you add UpdateInventory(); after the while loop?
I know what you're thinking ... Don't think that.

strazer

#4
Also check this BFAQ entry.

monkey0506

Code: ags
function room_b() {
  // script for Room: Player enters room (before fadein)
 
character[BB11].SetAsPlayer();


int i = 1;
while (i < Game.InventoryItemCount) {
     cBb11.InventoryQuantity[i] = cEgo.InventoryQuantity[i] ;
     i++;
}
SetFlashlightDarkness (GetFlashlightMinLightLevel() + 255);

SetMouseCursor(eModeInteract);


Okay, regarding your code, first off, you do know that "character[BB11]" and "cBb11" are the same thing (so, "cBb11.SetAsPlayer()" works the same as "character[BB11].SetAsPlayer()", and is shorter as well)?  It doesn't really matter, but the character array is primarily implemented for backwards compatibilty (and of course could also be used for loops and whatnot).  Just doesn't seem to fit with the style of the surrounding code is all.

Next, you have the line "cBb11.InventoryQuantity = cEgo.InventoryQuantity".  This would make BB11's inventory an exact copy of EGO's (any items BB11 already held would be lost).  And you don't remove the items from EGO.  If this is what you want, that's fine, just the way I understood you wanted to "hand...the inv items...over", which to me means that EGO would no longer have them.

But other than that, just check what Ashen and strazer pointed out about having an OwningCharacter set on the InvWindow and making sure the InvWindow is big enough to hold at least one item.

"Thanks a lot for the very elaborate answer."

And you are very welcome! :)  Let me know how it works out!

spook1

Thanks for helping.
The updateinventory() did the trick.

In fact I was not very clear sting that I wanted to hand over all inv items. Actually I needed a temporary copy of the player character, and therefore also a copy of the inventory.


SMF spam blocked by CleanTalk