Author Topic: Save Inventory State  (Read 461 times)

Racoon

  • you may say I´m a dreamer..
    • I can help with animation
    • I can help with making music
    • I can help with play testing
    • I can help with story design
    • I can help with translating
Save Inventory State
« on: 06 Feb 2020, 21:53 »
Hey there,

Is there a way to save the inventory state in a room and call it up again when entering that room?
I want the character to loose all inventory and to get it back when reentering.
If there already is a post about this problem please link it, I couldn´t find anything.

Thank you :)

Re: Save Inventory State
« Reply #1 on: 06 Feb 2020, 22:11 »
One solution is perhaps to create a dummy character (place it in room -1, which does not exist, so you'll never see it on screen) and move all inventory items to its inventory, and then back when you return.

More resource efficient solution with slightly more scripting, is to create a dynamic array of inventory item IDs in the room and use it as a "item cash".

In the room script
Code: Adventure Game Studio
  1. int Cash[]; // array, contains number of item instances (copies) per item ID
  2. // the size of array, when it is filled, is always equal to Game.InventoryItemCount
  3.  
  4. function Room_BeforeFadein()
  5. {
  6.      // if cash already exists, then move items to player
  7.      if (Cash != null)
  8.      {
  9.           for (int i = 0; i < Game.InventoryItemCount; i++) {
  10.                player.InventoryQuantity[i] = Cash[i];
  11.           }
  12.           UpdateInventory(); // update inventory window(s), if any exist
  13.           Cash = null; // reset the cash
  14.      }
  15. }
  16.  
  17. function Room_Leave()
  18. {
  19.      // move items to the cash
  20.      Cash = new int[Game.InventoryItemCount];
  21.      for (int i = 0; i < Game.InventoryItemCount; i++) {
  22.           Cash[i] = player.InventoryQuantity[i];
  23.           player.InventoryQuantity[i] = 0;
  24.      }
  25.      UpdateInventory(); // update inventory window(s), if any exist
  26. }
  27.  
« Last Edit: 06 Feb 2020, 22:13 by Crimson Wizard »

Re: Save Inventory State
« Reply #2 on: 07 Feb 2020, 00:25 »
I love how there's often multiple solutions to one problem. I was thinking you could use global variables to determine upon entering the room which items would be added.

so say you name a global variable:

IsPencilInInventory

 and set the variable to be false (bool or int, i can't remember off the top of my head).

Then when they pick up the pencil the variable is set to true. when they leave the room, you code in to have the character lose the inventory item. now when they go back in, you have code set to check if the pencil is set to true, and if so you add the inventory item. you would do the same for all inventory items.

if the character needs to lose the item upon using it or whatever, then set it to false when you code in to remove that item so it doesn't reappear if they go back in the inventory room again.

i am about to go to sleep so this advice might have a flaw but i think it sounds solid in theory. I think the inventory items might appear in a different order, is all.
« Last Edit: 07 Feb 2020, 00:30 by ManicMatt »

eri0o

Re: Save Inventory State
« Reply #3 on: 07 Feb 2020, 00:40 »
Depending on what you want to achieve may be easier to switch player character to one that looks just like him/her, and hide the original player character on and then revert.

Mandle

  • NO PIXEL LEFT BEHIND!!!
    • Mandle worked on one or more games that won an AGS Award!
    •  
    • Mandle worked on one or more games that was nominated for an AGS Award!
Re: Save Inventory State
« Reply #4 on: 07 Feb 2020, 16:02 »
Depending on what you want to achieve may be easier to switch player character to one that looks just like him/her, and hide the original player character on and then revert.

Elegant!

Re: Save Inventory State
« Reply #5 on: 07 Feb 2020, 16:38 »
That's a clever idea! Although I'm wondering about complications like having to make sure the code for the character is the right one, like if they say something it needs to be the right cEgo or cEgo2 or whatever.

But otherwise it does seem the easiest route to take.

Cassiebsg

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Save Inventory State
« Reply #6 on: 07 Feb 2020, 16:58 »
Maniac Matt... using player.Say solves that every time.  (laugh)
There are those who believe that life here began out there...

Re: Save Inventory State
« Reply #7 on: 07 Feb 2020, 17:23 »
Oh. Oh yeah.

... er.. hmm.

Ah! But what if they decide to have two different characters in the game and they both have unique dialogue, and you'd code in if statements for which character it is currently being controlled, then you couldn't do player say! So ha! Take that desperate clutch at saving myself! Ahem.
« Last Edit: 07 Feb 2020, 17:25 by ManicMatt »

Cassiebsg

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on one or more games that won an AGS Award!
    •  
    • Cassiebsg worked on one or more games that was nominated for an AGS Award!
Re: Save Inventory State
« Reply #8 on: 07 Feb 2020, 17:32 »
Code: Adventure Game Studio
  1. if (player==EGO1 || player==EGO2) player.Say("blah blah blah");
  2. else player.Say("blublubli");
  3.  

 (laugh) 
There are those who believe that life here began out there...

Re: Save Inventory State
« Reply #9 on: 07 Feb 2020, 17:37 »
Ah but what if they decide to have THREE characters?!

Lol I'd better stop before the mods zap me. No need to answer that, I know how that code would look!

Re: Save Inventory State
« Reply #10 on: 07 Feb 2020, 19:44 »
Depending on what you want to achieve may be easier to switch player character to one that looks just like him/her, and hide the original player character on and then revert.

Elegant!

From my point of view, this is an open hole into the hell. As soon as you begin adding more to the game and player character, this solution, seemingly trivial at first, may turn into source of bugs.

Simple examples: later in game you'd like to change character's property. Then you'd also have to make same change to the character copy. If you change properties in runtime, you also would have to do this for the duplicate character. You end up having to update second character each time you adjust your first one (or at least remember to copy those properties when you make the switch).

And all this trouble instead of moving array of items from one place to another, which is an explicit operation.
« Last Edit: 07 Feb 2020, 19:59 by Crimson Wizard »

eri0o

Re: Save Inventory State
« Reply #11 on: 07 Feb 2020, 19:58 »
I agree it may be a bad solution but it depends on what are the requirements for the game.

There's missing data on this operation relatively to the order the items were added, so while operating arrays work, the order of them in the inventory would change - going from ManicMatt comment.

Probably to avoid this, one would need to traverse the inventory window to figure out the order, and store this too. And later on, add them using this order.

Edit: thinking again, may be easier to change which character the inventory window belongs to. Again, this depends on the game constraints.

Disclaimer: in my game I had to do this and did exactly what CW said in his first comment of creating a dummy character and moving the inventory to it.
« Last Edit: 07 Feb 2020, 20:05 by eri0o »

Re: Save Inventory State
« Reply #12 on: 07 Feb 2020, 20:06 »
There's missing data on this operation relatively to the order the items were added, so while operating arrays work, the order of them in the inventory would change - going from ManicMatt comment.

Probably to avoid this, one would need to traverse the inventory window to figure out the order, and store this too. And later on, add them using this order.

Yes, this is where keeping original character as a backup has an advantage. But this also reminds that there's another hidden property that user does not have an access too directly...


Edit: thinking again, may be easier to change which character the inventory window belongs to.

If not displaying items is a primary goal, then indeed.
Actually, it's like the suggestion to switch player characters, except you keep controlling original character, but using items from another. It's even possible to script storing items in another character, probably, you just need not use "player" but some custom pointer "playerInventory" when dealing with items. (if necessary)

But if not displaying items is all you want, then maybe just draw an empty inventory window...
« Last Edit: 07 Feb 2020, 20:09 by Crimson Wizard »

Re: Save Inventory State
« Reply #13 on: 07 Feb 2020, 20:21 »
Or just disable the inventory GUI from appearing all together, unless the save and load etc icons are on it.

Yup, multiple solutions to one problem, but not all solutions are equal, heh.

Racoon

  • you may say I´m a dreamer..
    • I can help with animation
    • I can help with making music
    • I can help with play testing
    • I can help with story design
    • I can help with translating
Re: Save Inventory State
« Reply #14 on: 08 Feb 2020, 23:35 »
Thanks for your ideas!
The solution I did go with is making an invisible character cDummy and move all inventory to it when leaving the room and doing the same but backwards when reentering.
The code I scratched together while looking through the forums is:

Code: Adventure Game Studio
  1. function oGoBack_AnyClick()
  2. {
  3. int i = 1;
  4. while (i < Game.InventoryItemCount +1) {
  5.   cDummy.InventoryQuantity[i] += cEgo.InventoryQuantity[i];
  6.   cEgo.InventoryQuantity[i] = 0;
  7.   i++;
  8.   }
  9. UpdateInventory();
  10. player.ChangeRoom(1);
  11. }
  12.  
  13.  
  14. function room_Load()
  15. {
  16. int i = 1;
  17. while (i < Game.InventoryItemCount +1) {
  18.   cEgo.InventoryQuantity[i]+= cDummy.InventoryQuantity[i];
  19.   cDummy.InventoryQuantity[i] = 0;
  20.   i++;
  21.   }
  22. UpdateInventory();
  23. }

The only problem left is, that there are 4 items that the player should have at all time after adding them to the inventory. But I have an idea of how to do that with variables.