Hi, I was trying to save the inventory of player to make the possibility to have inventory pages. I managed to save the raw items without problems but, when it comes to save all the components it doesn't works.
Let me explain... When I want to save an ItemObject into a simple variable, no problem, it works, and I can resume the item from this variable. But when I want to save the ItemObject into a json file, it throws an error... So I tried to rebuild the entire item by myself, but I don't know how to get all the attached component from a weapon for example or even the effect added to an item like melee power or other effects. I managed to save the item name, guid, amount and slot so far but stuck with the other components I need.
Here is my actual code...
IDictionary<string, PlayerSaveClass> PlayerSavesList = new Dictionary<string, PlayerSaveClass>();
[ChatCommand("test")]
private void TestCommand(PlayerSession sender, string cmd, string[] args)
{
var name = sender.Identity.Name;
var location = GetPlayerPosition(sender);
var inventory = sender.WorldPlayerEntity.GetComponent<Inventory>();
var inventoryCapacity = inventory.Capacity;
List <ItemClass> rInv = new List<ItemClass>();
for (int i = 0; i < inventory.Capacity; i++)
{
var invItem = inventory.GetSlot(i);
if (invItem?.Generator == null) continue;
if (invItem != null && i != 14)
{
ItemClass itemToAdd = new ItemClass
{
itemID = RuntimeHurtDB.Instance.GetGuid(invItem.Generator).ToString(),
itemName = invItem.GetNameKey(),
amount = invItem.StackSize,
slot = i
};
rInv.Add(itemToAdd);
}
}
var bag = new ItemClass
{
itemID = RuntimeHurtDB.Instance.GetGuid(inventory.GetSlot(14).Generator).ToString(),
itemName = inventory.GetSlot(14).GetNameKey(),
amount = 1,
slot = 14
};
PlayerSaveClass playerSaving = new PlayerSaveClass
{
Name = name,
Location = location,
Inventory = rInv,
Bag = bag,
InventoryCapacity = inventoryCapacity
};
PlayerSavesList[name] = playerSaving;
dataFile.WriteObject("Test", PlayerSavesList);
ClearInventory(sender, inventory);
}
IDictionary<string, PlayerSaveClass> loadPlayers = new Dictionary<string, PlayerSaveClass>();
[ChatCommand("restoretry")]
private void RestoreTry(PlayerSession sender, string cmd, string[] args)
{
loadPlayers = dataFile.ReadObject<Dictionary<string, PlayerSaveClass>>("Test");
var instanceInventory = sender.WorldPlayerEntity.Storage;
var itemManager = Singleton<GlobalItemManager>.Instance;
var playerLoad = loadPlayers[sender.Identity.Name];
List<ItemClass> playerInv = playerLoad.Inventory;
ItemClass bag = playerLoad.Bag;
var bagGuid = RuntimeHurtDB.Instance.GetObjectByGuid<ItemGeneratorAsset>(bag.itemID);
var bagObject = itemManager.CreateItem(bagGuid, 1);
instanceInventory.SetSlot(14, bagObject);
foreach (ItemClass itemInstance in playerInv)
{
var itemGuid = RuntimeHurtDB.Instance.GetObjectByGuid<ItemGeneratorAsset>(itemInstance.itemID);
var itemToRestore = itemManager.CreateItem(itemGuid, itemInstance.amount);
instanceInventory.SetSlot(itemInstance.slot, itemToRestore);
instanceInventory.Invalidate(false);
}
}
public class PlayerSaveClass
{
public string Name { get; set; }
public Vector3 Location { get; set; }
public List<ItemClass> Inventory { get; set; }
public ItemClass Bag { get; set; }
public int InventoryCapacity { get; set; }
}
public class ItemClass
{
public string itemID { get; set; }
public string itemName { get; set; }
public int amount { get; set; }
public int slot { get; set; }
}