Я пиши небольшую консольую игру на c#. У меня есть структура инвентарь
namespace FightGame.Structs
{
public struct Inventory
{
private int _totalProtection = 0;
private int _totalProtectionQuality = 0;
private List<Item> _items;
public int TotalProtection => _totalProtection;
public int TotalProtectionQuality => _totalProtectionQuality;
public List<Item> Items => _items;
#region Costructors
public Inventory()
{
_items = new();
}
public Inventory(List<Item> items)
{
_items = items;
foreach (var item in items)
{
_totalProtection += item.Protection;
_totalProtectionQuality += item.ProtectionQuality;
}
}
#endregion
public static Inventory GenerateInventory(int countItem)
{
Inventory inventory = new();
for (int i = 0; i < countItem; i++)
{
inventory.AddItemInInventory(Item.GenerateItem($"Item {i + 1}", 100, 100, 100, 3));
}
return inventory;
}
public void AddItemInInventory(Item item)
{
_items.Add(item);
_totalProtection += item.Protection;
_totalProtectionQuality += item.ProtectionQuality;
}
public void AddItemsInInvntory(List<Item> items)
{
foreach (var item in items)
{
AddItemInInventory(item);
}
}
public void PrintInventory()
{
if (_items.Count == 0)
Console.WriteLine("Инвентарь пуст");
else
foreach (Item item in _items)
item.PrintItemInfo();
}
public static void PrintInventory(Inventory inventory)
{
if (inventory.Items.Count == 0)
Console.WriteLine("Инвентарь пуст");
else
foreach (Item item in inventory.Items)
item.PrintItemInfo();
}
}
}
При добавлении в список предметов общая защита и общее качество защиты должны увеличиться. Самое интерестное что в списке появляется предмет, но общая защита и общее качество защиты не изменяются. Я отслеживаю это с помощью точек останова.
Вот скриншоты

using FightGame.Scripts;
using FightGame.Structs;
namespace FightGame
{
public static class Game
{
private static Player _player;
public static Player Player => _player;
static Game()
{
_player = new Player("Stepa");
// Shop.OpenShop();
Item item = new("Новый предмет", 10000, 456, 987, ItemType.Weapon, ItemSize.Large);
_player.Inventory.PrintInventory();
Console.WriteLine();
_player.Inventory.AddItemInInventory(item);
_player.Inventory.PrintInventory();
Console.WriteLine();
Console.WriteLine($"Protection { _player.Inventory.TotalProtection}");
Console.WriteLine($"ProtectionQuality { _player.Inventory.TotalProtectionQuality}");
Init();
}
private static void Init()
{
Console.WriteLine("Game Init");
Console.WriteLine();
//Shop.OpenShop();
}
public static void GameProcess()
{
Batlle:
Battle.BattlePVE(World.CurrentLvl.TakeFighter());
if (_player.Params.Health <= 0 && _player.Lifes <= 0)
goto GameOver;
else
{
if (_player.Params.Health <= 0)
_player.Respawn();
Console.WriteLine("Нажмите ENTER для продолжения игры");
Console.ReadLine();
Console.Clear();
goto EveryStep;
}
EveryStep:
_player.Gold += 5;
_player.Regeneration();
goto Batlle;
GameOver:
Console.WriteLine("Game over");
Console.ReadLine();
}
}
}
Это скрип в котором я вызываю метод, который добавляет предмет в инвентарь. Это структура Item.
namespace FightGame.Structs
{
public struct Item
{
private string _name;
private int _cost;
private int _protection;
private int _protectionQuality;
private bool _onEquiped = false;
private ItemType _type;
private ItemSize _size;
public string Name => _name;
public int Cost => _cost;
public int Protection
{
get => _protection;
set => _protection = value;
}
public int ProtectionQuality
{
get => _protectionQuality;
set => _protectionQuality = value;
}
public bool OnEquiped => _onEquiped;
public ItemType Type => _type;
public ItemSize Size => _size;
public Item(string name, int cost, int protection, int protectionQuality, ItemType type, ItemSize size)
{
_name = name;
_cost = cost;
_protection = protection;
_protectionQuality = protectionQuality;
_type = type;
_size = size;
}
public static Item GenerateItem(string name, int cost, int protection, int protectionQuality, int spread)
{
Random rand = new();
//* Генерация числа
int GenerateNumber(int number)
{
if (spread == 0 && spread < 0)
return number;
int minNum = cost - (int)(cost * 0.2);
int maxNum = cost + (int)(cost * 0.2);
return rand.Next(minNum, maxNum);
}
//* Генерация случайного типа
ItemType GenerateItemType()
{
switch (rand.Next(Enum.GetNames(typeof(ItemType)).Length))
{
case 0:
return ItemType.Weapon;
case 1:
return ItemType.Shield;
case 2:
return ItemType.Accessories;
default:
return ItemType.Weapon;
}
}
//* Генерация случайного размера
ItemSize GenerateItemSize()
{
switch (rand.Next(Enum.GetNames(typeof(ItemSize)).Length))
{
case 0:
return ItemSize.Small;
case 1:
return ItemSize.Medium;
case 2:
return ItemSize.Large;
default:
return ItemSize.Small;
}
}
return new Item(name, GenerateNumber(cost), GenerateNumber(protection),
GenerateNumber(protectionQuality), GenerateItemType(), GenerateItemSize());
}
public void PrintItemInfo()
{
Console.WriteLine("Name " + _name);
Console.WriteLine("Cost " + _cost);
Console.WriteLine("Protection " + _protection);
Console.WriteLine("Protection Quality " + _protectionQuality);
Console.WriteLine("Size " + _size);
Console.WriteLine("Type " + _type);
Console.WriteLine();
}
}
public enum ItemType
{
Weapon,
Shield,
Accessories
}
public enum ItemSize
{
Small,
Medium,
Large
}
}
Точка входа программы
static void Main()
{
//Настройка UI
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
World.SetLevels(GenerateLevels());
Game.GameProcess();
// World.PrintLevels();
}

Inventory inventory = new();где-то и как-то (например вызываете повторноGenerateInventory(), ну а новый инвентарь == новые значения. Показывайте весь код, если не найдете причину сами. – EvgeniyZ Dec 31 '23 at 15:53struct Inventory- вот причина проблем. Замените наclass. – Alexander Petrov Jan 02 '24 at 15:40