Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to update server data in all the clients [Netcode]

Discussion in 'NetCode for ECS' started by efpoman, Jul 6, 2020.

  1. efpoman

    efpoman

    Joined:
    Mar 24, 2020
    Posts:
    6
    Hi, im new in Netcode and im trying to change a health value between clients. I have created a system in the server to change the values and this works. The problem is to update the clients current health to all the clients. I think i need to use IRpcCommand but im not sure how to do it.

    Can anyone provide any simple example of how i can do it?.

    Thank you.
     
  2. efpoman

    efpoman

    Joined:
    Mar 24, 2020
    Posts:
    6
    Finally i solve my problem, and im not using IRpcCommand. First i get the ids and the players entities.

    Code (CSharp):
    1.         EntityQuery m_EntitiesToDamageQuery = GetEntityQuery(typeof(PlayerData));
    2.         var entities = m_EntitiesToDamageQuery.ToEntityArray(Allocator.TempJob);
    3.         var ides = new NativeArray<int>(entities.Length, Allocator.TempJob);
    Then i did a for each loop over all entities with PlayerData and the input buffer. Inside the loop:
    Code (CSharp):
    1.  
    2.                 if (input.shoot > 0)
    3.                 {
    4.  
    5.                     for (int i = 0; i < ides.Length; ++i)
    6.                     {
    7.                         if (ides[i] == input.shootID)
    8.                         {
    9.  
    10.                             var component = EntityManager.GetComponentData<PlayerData>(entities[i]);
    11.                             component.currentHealth -= 10;
    12.                             if (component.currentHealth < 0)
    13.                             {
    14.                                 component.currentHealth = 0;
    15.                                 component.death = true;
    16.                             }
    17.                             component.killedBy = ent;
    18.                             component.killedByID = player.playerId;
    19.                             EntityManager.SetComponentData<PlayerData>(entities[i], component);
    20.  
    21.                         }
    22.  
    23.                     }
    24.  
    25.                 }
     
    bb8_1 likes this.
  3. bb8_1

    bb8_1

    Joined:
    Jan 20, 2019
    Posts:
    98
    For health i use in my game authoring component with float value health that is calculated on server - its a synced value so all clients get that value after server sends it to them(it's a part of snapshot actually which is generated code by NetCode - so basically NetCode takes care of informing clients about the current value)