Search Unity

Question Using Entities inside IInputCommandData

Discussion in 'NetCode for ECS' started by Richay, Jan 13, 2023.

  1. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    On my client, I need to specify entities within IInputCommandData structs (e.g. which entity the client has clicked on) for the server to process. However, using an entity field creates an error inside the auto-generated code.

    For example, this struct:
    Code (CSharp):
    1. public struct InputStruct : IInputComponentData
    2. {
    3.     [GhostField] public Entity Target;
    4. }
    Creates this error:
    Code (CSharp):
    1. NetCodeSourceGenerator\Unity.NetCode.Generators.NetCodeSourceGenerator\Assembly-CSharp\Assembly_CSharp.Generated_InputStructInputBufferDataCommandSerializer.cs(599,117): error CS1061: 'InputStruct' does not contain a definition for 'TargetSpawnTick' and no accessible extension method 'TargetSpawnTick' accepting a first argument of type 'InputStruct' could be found (are you missing a using directive or an assembly reference?)
    Considering that the entity is always pointing to a ghost (or default), is this something achieveable? Otherwise I need to maintain an alternate entity/id map, which feels redundant.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    316
    Hey Richay,

    I'll send you a patch of the fix shortly, as we also encountered this recently.
     
  3. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    316
    You need to modify the template, changing the `CalculateChangeMask` method to be this:

    Code (CSharp):
    1.   public void CalculateChangeMask(ref Snapshot snapshot, ref Snapshot baseline, uint changeMask)
    2.         {
    3.             #region __GHOST_CALCULATE_INPUT_CHANGE_MASK__
    4.             changeMask |= snapshot.__GHOST_FIELD_NAME__ != baseline.__GHOST_FIELD_NAME__ ? 1u : 0;
    5.             #endregion
    6.             #region __GHOST_CALCULATE_CHANGE_MASK_ZERO__
    7.             changeMask = (snapshot.__GHOST_FIELD_NAME__ != baseline.__GHOST_FIELD_NAME__ || snapshot.__GHOST_FIELD_NAME__SpawnTick != baseline.__GHOST_FIELD_NAME__SpawnTick) ? 1u : 0;
    8.             #endregion
    9.     #region __GHOST_CALCULATE_CHANGE_MASK__
    10.     changeMask |= (snapshot.__GHOST_FIELD_NAME__ != baseline.__GHOST_FIELD_NAME__ || snapshot.__GHOST_FIELD_NAME__SpawnTick != baseline.__GHOST_FIELD_NAME__SpawnTick) ? (1u<<__GHOST_MASK_INDEX__) : 0;
    11.     #endregion
    12.     }
    13.  
    You then need to re-compile the source generators using the readme instructions:
    ========= HOW TO BUILD SOURCE GENERATORS ==============
    Source generator DLLs need to be compiled manually outside of the Unity compilation pipeline using the .NET SDK 6.0 or higher: https://dotnet.microsoft.com/en-us/download/dotnet/6.0 That can be done with dotnet from within the Packages\com.unity.netcode\Runtime\SourceGenerators\Source~ directory via command prompt:

    dotnet publish -c Release

    Additionally, they can be built/debugged with the SourceGenerator Solution (.sln) in the same folder. In order to debug source generators you can replace Release with Debug when running the publish command.
     
  4. Richay

    Richay

    Joined:
    Aug 5, 2013
    Posts:
    122
    Ah excellent it's just a bug, I was scared it was a design decision.

    Thanks for the rebuild steps. Could you give an ETA for the next update drop? If it's soon (next week or two) I'd prefer to wait.
     
  5. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    316
    I wish I could, but I cannot. Apologies. Unless stated otherwise, you also can't assume it'll be in the next pre-release.
     
    Richay likes this.