Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Inventory copying

Asdfghjkl I should be able to do this by now, but I can't(I've been editting scripts all day, to absolutely no success :|).

Basically, I want to be able to store a copy of the player's inventory at a point in time, then change the player's inventory to the stored version at a later point in time. So far, my messing around has got me to these 2 snippets;

in one common event
Code:
item = $game_party.items

$inventory = item
and in another common event
Code:
item = $inventory

$game_party.items = item

However, when I run this, the next time anything happens involving $game_party.items, the game crashes with the error "Script Game_Party line 52: NoMethod Error / undefined method "keys" for array blahblahblah"
I can't work out how to do this. Help please?
 
I don't have a chance to try it out now, but it sounds right anyway. Thanks!

(also I haven't heard of the .dup method. What does it do? It looks like it makes a copy(which would make sense), but how is it different to just going x=y ?
 
O-oh dear.

Okay, I tried it out. When I use it, it doesn't throw any errors until "$game_party.items = $inventory", when it throws out
"undefined method: "Keys" for #<arrayblahblahblah>"

Now, I got it to print the value of both $inventory and $game_party.items and they were identical. What's going on?

I also tried it in a game with default scripts, and got a different error, on the same line;
"Undefined method : "items=" for #<Game_Partyblahblahblah>"

Help?
 

Ares

Member

Before you can modify $game_party.items, you need permission to alter it. You can get that permission by adding the following in Game Party after line 14:
[rgss]attr_accessor :items
[/rgss]

Then you can do:
[rgss] 
$inventory = $game_party.items.dup # creates $inventory which is a copy of $game_party.items
$game_party.items.clear # clears the inventory
$game_party.items = $inventory # restores the inventory; $inventory is actually some kind of back up
[/rgss]

Hope that solves your problem :)

edit:
'dup' is a method of the Array and Hash classes, and does almost the same as 'clone'. This is the definition from the help file:
For Hashes":2jubvmnt said:
Returns a new hash with the same contents as the receiver. Using clone on a frozen hash will return a similarly frozen hash, but dup returns an unfrozen hash with identical contents.
For Arrays":2jubvmnt said:
Returns a new array with the same contents as the receiver. clone returns a complete clone of the original array, including freeze status and singleton methods, while dup duplicates the object contents only. Neither method copies the methods or elements themselves.
You can also use "$inventory=$game_party.items", but I personally don't like that syntax very much.
 
Actually, now I look at my default-scripts project, that line of code isn't there by default. Unfortunately, adding it to the default project just causes me to get the same error as in the project with scripts.

Also, there's one difference; I still get the same crash error, but now I don't get it until I try to start a battle. It's just that both versions have a battle right after the script call, so I didin't notice at first.
 

Ares

Member

Hmmm, I can't recreate that error :huh:
Can you post the events you are using? Also, are you using any other scripts?
Pokémaniac":32tod54m said:
"Undefined method : "Keys" for #<arrayblahblahblah>"
This error seems to indicate that you are trying to obtain an array of the keys (@blah.keys) of an array, which throws an error because keys is not a method in the 'Array' class. So most likely $game_party.items or $inventory is an array instead of a hash. In just a blank project it works fine for me, even when starting a battle.

I hope I can help you solve this soon because I am going to Austria for a week this evening (in about 4 hours).
 
asdfghjkl I think I know what's going on(kinda)

I just tried your script, and got it to work. The problem is, I'm an idiot, and I forgot to mention I'm working in VX. Sorry for messing you guys around like that.

Problem is, I still don't know what to change to make it compatible. The code I'm using is just an event that adds an item to the inventory, then calls the copy/erase/replace script that Ares made(who unfortunately must've fled the country by now). Still, hopefully this will help.
 
Sure.
error.png

with attr_accessor :items added as line 16 of Game Party.


This gives the the "keys" error I talked about before every time something happens involving items.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top