public void SaveInventory(string owner_id, Inventory inventory) { // Save the inventory to file SavedInventory sInventory = new SavedInventory(); sInventory.maxSpace = inventory.maxSpaces; if (inventory.IsEmpty() == false) { List <ItemReference> references = new List <ItemReference>(); foreach (InventoryItem item in inventory.inventory_items) { if (item.item == null) { continue; } ItemReference reference; reference.itemName = item.item.name; reference.itemType = item.item.itemType; reference.count = item.count; references.Add(reference); } sInventory.items = references.ToArray(); } JsonWriter.WriteInventoryToJson(sInventory, owner_id); }
public static void WriteInventoryToJson(SavedInventory inventory, string uniqueID) { string content = JsonUtility.ToJson(inventory); string filePath = Application.streamingAssetsPath + ("/Saved/Inventory/Inv_" + uniqueID + ".json"); File.WriteAllText(filePath, content); Debug.Log("Inventory saved to " + filePath); }
public override void SaveData(object obj) { Player player = obj as Player; level = player.GetCharac().level; xp = player.Xp; strength = player.GetCharac().strength; constitution = player.GetCharac().constitution; intelligence = player.GetCharac().intelligence; dexterity = player.GetCharac().dexterity; positionX = player.transform.position.x; positionY = player.transform.position.y; positionZ = player.transform.position.z; rotationY = player.transform.rotation.eulerAngles.y; inventory = new SavedInventory(); inventory.SaveData(player.Inventory); }
//Doesn't actually have to be a single chunk //NOTE: Written for the happy path, throws exceptions on failure public static void LoadChunk(string ToLoad) { SavedChunk LoadedChunk = Newtonsoft.Json.JsonConvert.DeserializeObject <SavedChunk>(ToLoad); foreach (SavedTile Saved in LoadedChunk.Tiles) { string GuidName = System.Guid.NewGuid().ToString(); Self.PlaceWithName(Saved.Id, Saved.Pos, Saved.Rot, Saved.Owner, GuidName); if (Saved.InventoryIndex >= 0) { Node Branch = EntitiesRoot.GetNode(GuidName); SavedInventory Inventory = LoadedChunk.Inventories[Saved.InventoryIndex]; if (Branch is IHasInventory HasInventory && HasInventory.Inventory.Contents.Length == Inventory.Contents.Length) { HasInventory.Inventory.Contents = Inventory.Contents; } } } }
public Inventory LoadInventory(string owner_id) { Item_Manager item_Manager = Item_Manager.instance; // Try to load and return SavedInventory savedInventory = JsonLoader.instance.LoadSavedInventory(owner_id); if (savedInventory.maxSpace > 0 && savedInventory.items != null && savedInventory.items.Length > 0) { Inventory loadedInventory = new Inventory(savedInventory.maxSpace); foreach (ItemReference itemRef in savedInventory.items) { // TODO : Load the updated stats by updating the item prototype // To do this we might have to store Item stats in saved inventory with each item reference Item newItem = item_Manager.CreateInstance(item_Manager.GetPrototype(itemRef.itemName)); loadedInventory.AddItem(newItem, itemRef.count); } return(loadedInventory); } // if none found... return(null); }
public int AddInventory(SavedInventory Inventory) { Inventories.Add(Inventory); return(Inventories.Count - 1); }