Multiple Inventories For One Character--Scripting Problems (SOLVED)

Started by pslim, Thu 20/07/2006 06:53:34

Previous topic - Next topic

pslim

Hey there.

In my current game, there is only one main character, but I need him to have two inventories. Having the secondary inventory show up is no problem, and I have that all set up, but interaction is another thing altogether.

I'm handling inventory clicks in script. At the moment, if I click on an item in the secondary inventory (owned by an invisible character whose only function is to have this inventory), the game crashes with an error saying that my PC doesn't have the item, and refers to the line of the global script where I've told it to set the active inventory to what the player just clicked on.

There's an old thread from 2003 that deals with exactly this issue, and Scorporius posted what looks like a brilliant solution, but I can't get it to work.

He proposes having a separate character for Ego's main inventory AND for the secondary inventory, and to copy the contents of one or the other into Ego's actual coded inventory depending on which inventory set the player intends to bring up. The code he posted (with some changes due to AGS updates that I muddled through after many error messages) is as follows:

Code: ags
function CopyInventory(int CharID1, int CharID2) {Ã,  Ã, //Copy Inventory
int INV_MAX = 300;
int i=1;
while (i<INV_MAX) {
character[CharID1].InventoryQuantity[ i ] = character[CharID2].InventoryQuantity[ i ];
i++;
}
}


This, he says, will copy one character's inventory onto another's. Then you're supposed to call the following when you open the GUI associated with the inventory you want to use:

Code: ags
Ã,  CopyInventory(1, 12); //copy inventory
Ã,  UpdateInventory(); 
Ã,  gui[2].Visible = true; //open inventory 



It compiles, but when I push the button with the above code associated with it, I get a crash and an error that says "Character.InventoryQuantity: invalid inventory index."


Any help would be greatly appreciated. I've been at this all night.Ã,  :-X
 

Khris

Try decreasing INV_MAX. Start with 100, then increase until it crashes again.

strazer

Of course you can't copy an item that doesn't exist.
There's the built-in AGS_MAX_INV_ITEMS constant you can use instead of INV_MAX.

pslim

Thanks KhrisMUC and strazer, for the comments, but I tried setting INV_MAX to 100, and using AGS_MAX_INV_ITEMS instead, and both gave the same error when the button was clicked.

QuoteOf course you can't copy an item that doesn't exist.

I found this to be a rather cryptic comment when I first read it, but then I realized that you might mean that I should set INV_MAX to the total number of inventory items I have currently defined... and it worked! So I just need to update this every time I define a new inventory item.

Thanks you guys!

And thanks to Scorporius, who probably won't read this! You're my hero!
 

strazer

Yes, I was thinking of Game.InventoryItemCount. :P
AGS_MAX_INV_ITEMS is the maximum allowed number of inv items, not the actual number of items defined in your project.
Sorry for the confusion. I'm glad you were able to work it out anyway.

Khris

Couldn't you just use invMain.CharacterToUse = cOne; / invMain.CharacterToUse = cTwo; directly before displaying invMain?

pslim

Quote from: KhrisMUC on Thu 20/07/2006 23:10:17
Couldn't you just use invMain.CharacterToUse = cOne; / invMain.CharacterToUse = cTwo; directly before displaying invMain?

It's my understanding that all that does is show One's or Two's inventory, depending on which way you set it. In that case, no, because One still doesn't have any of the contents of Two's inventory on his person, so when I tell the game to set the active inventory to whatever he picked out of Two's box, I get a crash and an error saying One doesn't have that item.

At least, that's how I interpreted that command in the manual when I saw it. If that's not actually how it works I may have put myself through a lot of grueling awfulness for no reason. <g>
 

Khris

That's right, sorry, the only alternative would be to switch the player character. That isn't so bad, just assign identical views to both characters and make sure their rooms, coords and loops are the same before switching.
But using Game.InventoryItemCount is the best solution, imo.

SMF spam blocked by CleanTalk