The Separated Inventory (Each Player Character Has Its Own Inventory)

Started by Eon_Star, Fri 25/10/2024 23:41:54

Previous topic - Next topic

Eon_Star

Hello,

I just made a new game sample where two different player characaters can have their own inventory.

I tought it may be useful for game developers looking for a simple solution. So here is the game file (Game file was updated on 26.10.2024):


https://eon-star.itch.io/kellys-quest-separated-inventory


Happy coding everyone  :-D

#inventory #custom inventory #separate inventory #multiple inventories

eri0o

I haven't looked but that is the default in AGS, each character has its own inventory. In the inventory window there's a property that you just set the character the inventory is supposed to match to - you can just pass the player character too after you change it.

I know because I used this to make my first game in ags, which was a card game and each player had its own hand using each ones inventory.

Eon_Star

Yeah,

Thanks for the comment.

I just wanted to give a hint to those who are new to the topic. I will have a tutorial video on Youtube how it was made.

Cheers  ;)

Crimson Wizard

I'm sorry, but checking your game project I can see that it's more complicated than it should be.

First of all, if InvWindow.CharacterToUse is set to null (or -1 in the editor), then it is supposed to switch to a new playable character automatically. This is the default behavior, as mentioned in the manual:
https://adventuregamestudio.github.io/ags-manual/InvWindow.html#invwindowcharactertouse
QuoteThis is either set to a specific character, or it can be set to null, in which case the inventory window will track the current player character (this is the default).

So in theory you don't even have to change this property, unless you want to display a non-player's inventory.

Then, for some reason you are not using 1 inventory window, but 2 inventory controls on same GUI, one for each character, although you only display 1 of them at the same time. Why use 2 inventory windows then? And if you  are using 2 anyway, then why keep setting their CharacterToUse in script? This is simply not necessary, you could just set them once in the editor and never touch again.

To make this inventory change work you only need 1 InventoryWindow control, and either assign CharacterToUse to -1 in the editor (this means "use active player character"), or do "InventoryWindow.CharacterToUse = player;" in script right after calling "somechar.SetAsPlayer();".



Another thing, you are using boolean variables to track which character is currently a player. There's no need to do this, as you can simply use standard "player" variable to find that out like:

Code: ags
if (player == cBob)
{
   // do something
}

if (player == cKelly)
{
   // do other thing
}


EDIT:
There's an unfortunate problem though. While testing this scenario I found a bug in the current version of AGS engine, which was not reported earlier. It appears that if you use InventoryWindow which switches to a new player character automatically, then it does not redraw right away. This may make it look like it's not working, although it should.

I must fix this engine bug in the next 3.6.1 patch.

For now the workaround may be to actually switch CharacterToUse in script...

EDIT2:
Actually, this problem is much older than I thought. Looks like almost any version of AGS had some issue of not redrawing inventory window immediately after player changes. In earlier versions you had to move mouse around to make it redraw... Could be that's why people used to set CharacterToUse manually in script?

Eon_Star

Hi,

Thanks for the advice. I looked for a while to find the right scripting to do it. I might check the usage again.

This was a real bugger for me to find out.

Edit:

I wanted to create 2 different inventories for 2 different player characters. They would not share the same inventory. But your comment is important. You helped a lot.

Edit 2:


I couldn't find another way to do this. So I used a script that would work for me. I was not aware of the bug.

Thank you.  :-D

Crimson Wizard

Here's a temporary fixed build download, if you'd like to try:
https://cirrus-ci.com/task/5818327266754560

With this fix you only need to setup 1 InventoryWindow control, and make sure it's "CharacterToUse" property is set to "-1" in the editor.
It should switch automatically when you change player character.

Eon_Star

Thanks Crimson Wizard.

I will wait for the next patch.

I really like your consideration and efforts about the development of the editor.

Keep up the good work.

Cheers :)

SMF spam blocked by CleanTalk