Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Something About Inventory's Items in Multiplayer.

Discussion in 'NetCode for ECS' started by OrientedPain, Oct 18, 2023.

  1. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    50
    Hello Everyone,

    I want to make an Inventory system for my MMORPG game, so my design was to create an inventory as a dynamic buffer of Entities (Items) in the Server assembly side. I have many different types of items, so each item type is based on a unique collection of components.

    My question is I want to create the item entity as normal in server side (without Ghost Component), but maybe the Netcode prevent it and the created Entity seems disposed after creation and don't need the item to be replicated to all client and I will sync the item stats throw RPCs to the owner client specially item stats do not change frequently it's only change when do some crafting on it.

    Is there any way to do that or even make it a ghost item but only replicate to its owner client?

    Code (CSharp):
    1. public struct Inventory : IBufferElementData
    2. {
    3.     public Entity Item;
    4. }
    5.  
    6. public struct ItemStats : IComponentData
    7. {
    8.     public int Id;
    9.     public FixedString64Bytes Name;
    10.     public ItemType Type;
    11.     public FixedString64Bytes Sort;
    12.  
    13. public enum ItemType
    14. {
    15.     PhyWeapon,
    16.     MagWeapon,
    17.     Shield,
    18.     .....
    19. }
    20. }
    That is what happen when add a normal entity into buffer:

    Code (CSharp):
    1. internal BufferLookup<Inventory> inventory;
    2. var item = ECB.CreateEntity();
    3. ECB.AddComponent<ItemStats>(item);
    4.  
    5. inventory[playerEntity].Add(new Inventory { Item = item });
    It gives me Invalid entity inside the buffer.


    "Player's Inventory Component"


    "The Item Entity in Entities Hierarchy"

    Is my design wrong with ECS? Need to hear from you.

    Thanks in advance.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    313
    OrientedPain likes this.
  3. OrientedPain

    OrientedPain

    Joined:
    Mar 26, 2018
    Posts:
    50
    Thank you for your response, the post you mentioned is very useful.

    I have solved the issue; I was trying to access the new entity in the same frame. The issue I'm experiencing is related to the order of execution of systems and the EntityCommandBuffer. When I use an ECB to create an entity and add components, the changes are not applied immediately. Instead, they are applied after the system that created the ECB finishes its update and the ECB is played back.

    I do not know how I forgot something like that. :):)