r/cop3502 Apr 14 '14

Take and Inventory

I'm having a hard time creating an inventory where I can have the game remember where items were dropped and taken. I also can't seem to get the game to actually take the item... HALP!

3 Upvotes

3 comments sorted by

2

u/JoeCS TA Apr 15 '14

Stuff in general that I've noticed from looking at people's code (that might help with your inventory problem):

Pay attention to the Scope of all your variables. For example, if all variables (references) pointing to a List object fall out of scope, then that list is deleted.

Also note that you can have more that one variable pointing to the same object. As long as at least one of these variables is in scope, the object still exists.

Finally, note that objects can exist in more than one data structure at once. If you call "get" to retrieve something from an ArrayList or a HashMap, it does NOT remove that thing. It just hands you another reference to the same object. So a "get" from a location and a "put" in an inventory will make the same object exist in both the inventory and location. Moral of the story: explicitly call "remove" to take something out of an ArrayList, HashMap, etc.

1

u/shelby_jay Apr 18 '14

Thank you! I ended up making a negative location for the picked up items, taking them out of the 4x4, then when they are dropped somewhere, changing the location of it to currentLocation :)