- Home /
Inventory "Remove" Function Help
I'm attempting to creating a prototype inventory system for a MMO and got stuck trying to figure out how to delete a specific playerprefs key when the item is removed (Which should happen in the "RemoveItem" function I've created. Also feel free to let me know how to make any part of the script more efficient and you are free to use it if you want.
The Inventory Manager Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryManager : MonoBehaviour {
public List<GameObject> WeaponsInInventory;
public List<GameObject> ArmorInInventory;
public List<GameObject> ItemsInInventory;
void Start() {
LoadItems();
}
public void AddItem(GameObject item, string itemClass) {
if(itemClass == "Weapon") {
WeaponsInInventory.Add(item);
}
else if(itemClass == "Armor") {
ArmorInInventory.Add(item);
}
else if(itemClass == "Item") {
ItemsInInventory.Add(item);
}
SaveItems();
}
public void RemoveItem(GameObject item, string itemClass) {
if(itemClass == "Weapon") {
WeaponsInInventory.Remove(item);
}
else if(itemClass == "Armor") {
ArmorInInventory.Remove(item);
}
else if(itemClass == "Item") {
ItemsInInventory.Remove(item);
}
}
public void SaveItems() {
for(int i = 0; i < WeaponsInInventory.Count; i++) {
if(WeaponsInInventory[i] != null)
PlayerPrefs.SetString("WeaponsInventory" + i, WeaponsInInventory[i].name);
}
for(int i = 0; i < ArmorInInventory.Count; i++) {
if(ArmorInInventory[i] != null)
PlayerPrefs.SetString("ArmorInventory" + i, ArmorInInventory[i].name);
}
for(int i = 0; i < ItemsInInventory.Count; i++) {
if(ItemsInInventory[i] != null)
PlayerPrefs.SetString("ItemsInventory" + i, ItemsInInventory[i].name);
}
}
public void LoadItems() {
for(int i = 0; i < WeaponsInInventory.Count; i++) {
string it;
it = PlayerPrefs.GetString("WeaponsInventory" + i);
GameObject temp = new GameObject();
temp.name = it;
if(it != "")
WeaponsInInventory.Add(temp);
}
for(int i = 0; i < ArmorInInventory.Count; i++) {
string it;
it = PlayerPrefs.GetString("ArmorInventory" + i);
GameObject temp = new GameObject();
temp.name = it;
if(it != "")
ArmorInInventory.Add(temp);
}
for(int i = 0; i < ItemsInInventory.Count; i++) {
string it;
it = PlayerPrefs.GetString("ItemsInventory" + i);
GameObject temp = new GameObject();
temp.name = it;
if(it != "")
ItemsInInventory.Add(temp);
}
}
}
Answer by Tomer-Barkan · Dec 19, 2013 at 08:51 AM
Just Add PlayerPrefs.DeleteAll();
at the beginning of your SaveItems()
method, so that it will save the current state of the inventory deleting the previous contents.
Other than that, removing the item from the inventory by simply removing it from the list like you do in RemoveItem is fine.
public void SaveItems() {
PlayerPrefs.DeleteAll();
for(int i = 0; i < WeaponsInInventory.Count; i++) {
if(WeaponsInInventory[i] != null)
PlayerPrefs.SetString("WeaponsInventory" + i, WeaponsInInventory[i].name);
}
for(int i = 0; i < ArmorInInventory.Count; i++) {
if(ArmorInInventory[i] != null)
PlayerPrefs.SetString("ArmorInventory" + i, ArmorInInventory[i].name);
}
for(int i = 0; i < ItemsInInventory.Count; i++) {
if(ItemsInInventory[i] != null)
PlayerPrefs.SetString("ItemsInventory" + i, ItemsInInventory[i].name);
}
}
Edit: In case you are using PlayerPrefs to store other things except inventory, then you might not want to delete all keys, but only delete the inventory keys. It's a bit more complicated, you'll need to check which keys exist and delete them. If this is the case, replace PlayerPrefs.DeleteAll();
with the following code:
for (int i = 0; PlayerPrefs.HasKey("WeaponsInventory" + i); i++) {
PlayerPrefs.DeleyeKey("WeaponsInventory" + i);
}
// repeat with "ArmorInventory" and "ItemsInventory"
I thought about doing it both of those ways but they each have their drawbacks. By using PlayerPrefs.DeleteAll();, everything will be deleted (obviously :p) which includes all the players info such as name, location, cash, friends list..ect. I figured by leaving it as it is, if you delete something from the inventory then restart the game the item will reappear from the inventory because it was never deleted from playerprefs which it repopulates the inventory from.
Great answer! Got the internals working perfectly, thanks mate :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Adding Item to an Inventory. 1 Answer
C# syntax seems fine, why wont yeild work? (Unsolved) 1 Answer
unity3d simple inventory 1 Answer