Search Unity

NetCode Code Generation Issue/Question

Discussion in 'NetCode for ECS' started by florianhanke, Jan 6, 2020.

  1. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Hi all

    I'm using NetCode 0.0.4-preview.0 on 2020.1.0a17 and I have an issue and a question.

    1. I've been following the Getting Started here: https://docs.unity3d.com/Packages/com.unity.netcode@0.0/manual/getting-started.html

    This results in illegal code in the `CubeGhostSpawnSystem`, generated via the cube's Ghost Authoring Component:

    Code (CSharp):
    1.  
    2. public partial class CubeGhostSpawnSystem : DefaultGhostSpawnSystem<CubeSnapshotData>
    3. {
    4. struct SetPredictedDefault : IJobParallelFor
    5. {
    6. [ReadOnly] public NativeArray<CubeSnapshotData> snapshots;
    7. public NativeArray<int> predictionMask;
    8. [ReadOnly][DeallocateOnJobCompletion] public NativeArray<NetworkIdComponent> localPlayerId;
    9. public void Execute(int index)
    10. {
    11. if (localPlayerId.Length == 1 && snapshots[index].GetTranslationValue() == localPlayerId[0].Value) // <-- here
    12. predictionMask[index] = 1;
    13. }
    14. }
    15.  
    (The error is: "In order to be applicable as a short circuit operator a user-defined logical operator ('bool3.operator &(bool, bool3)') must have the same return type and parameter types [Assembly-CSharp]")

    I'm under the impression that I cannot fix this on the user end…?

    2. I was surprised to see that the code generator uses a 1 to 1 correspondence with the Prefab name instead of a mapping to an Id, or storage in a hash using the name as a key, for example. This causes quite a few issues with my naming scheme which uses dashes and underscores. Will this be rewritten to be independent of user naming, or will you perhaps add a "variable name" akin to the Snapshot Data Path etc. on the Ghost Authoring Component?

    Cheers and thanks in advance!
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    1. Wrap your translation equality check in math.all.
     
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Thanks - it's code generated by NetCode.
     
  4. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    It looks like the problem here is that when you change the default client instantiation to be "Owner Predicted" you are not specifying the correct "Predicting player network id" right below it. This field must point to a field containing a network id, not the translation as it seems to do in your case. The guide is not calling this out and it looks like the screenshot has it set incorrectly, we'll clarify that in the guide.

    We don't have any short term plans to fundamentally change it, but it not handling any prefab name is a bug - and it seems reasonable to have an override field for specifying the name.
     
  5. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Thanks! Now that I've done it again, I remember thinking it weird that Rotation was selected, but at that point I wasn't thinking in the NetCode domain yet, just following the example, and did not remember when the issue occurred later. So updating the example is a good idea.

    Cheers!

    @timjohansson One thing I've been wondering about: assuming I just added the Networking in a random test scene of mine where I also try other things. How would I go about temporarily removing networking (go back to the previous PlayMode, so to speak), so I could try something, then go back to networking when I'm finished?
     
  6. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    You need to override the bootstrap to do this, we do it on a per scene level in the multiplayer samples since some of our scenes are not using netcode - see https://github.com/Unity-Technologi...pleproject/Assets/Samples/NetCodeBootstrap.cs .
    You could change the check to do something other than checking scene if you want.
     
    florianhanke likes this.
  7. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
  8. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    P.S: I'm using the enabled/disabled ConvertToClientServerEntity GO to switch on/off multiplayer/networking, since this is how I first expected it to work :)

    Code (CSharp):
    1. var convertToClientServerEntity = GameObject.FindObjectOfType(typeof(ConvertToClientServerEntity), false);
    2. if (convertToClientServerEntity != null)
    3.     return base.Initialize(defaultWorldName);
     
  9. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
    Thanks for clarifying! However, it looks like the tutorial's `MovableCubeComponent` can't be directly added onto the prefab since it doesn't inherit from `MonoBehaviour`. Per this thread https://forum.unity.com/threads/getting-started-with-pure-ecs.637300/, I tried adding a `ComponentDataProxy`

    ```
    using Unity.Entities;
    using Unity.NetCode;

    [GenerateAuthoringComponent]
    public struct MovableCube : IComponentData
    {
    [GhostDefaultField]
    public int PlayerId;
    }

    public class MovableCubeComponent : ComponentDataProxy<MovableCube> { }
    ```

    However, it looks like the component still can't be added to the gameobject and am as a result unable to set "Predicting player network id" correctly.

    Curious how did you resolve the above in your second pass?

    Edit 1: What's really weird is if I load up the demo https://github.com/Unity-Technologies/multiplayer/tree/master/sampleproject, I'm able to remove and add the IComponentData to the GameObject just fine. I was previously using 2020.1.0a18 but still having the same issue with 2019.3.0f3. I've ensured that my MovableCubeComponent is in a file named MovableCubeComponent.cs as well.

    Edit 2: Extra weird is even though the code in the two files is identical, Unity complains about my copy (left) as not containing a MonoBehaviour. The script in the official Unity sampleproject doesn't have such a warning (right).

     
    Last edited: Jan 12, 2020
  10. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    The `[GenerateAuthoringComponent]` generates an authoring component (a GO component that results in the MovableCubeComponent) for you. You should be able to find and use it via autocompletion via `Add Component`. So the `ComponentDataProxy` should not be necessary.

    But I've had issues with filenames and `GenerateAuthoringComponent` – can't remember why, though, right now.
     
    edalbeci likes this.
  11. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
    Ah thanks! That makes sense.

    Re filenames: Ahh that rings a bell. I ran into issues shortly after renaming both the class and the filename in 2020.1.0a18. Moving the project to 2019.3.0f3 didn't work. However, restarting the project in 2019.3.0f3 worked. Just now, I was unable to reproduce the above issue with class/filename renaming. Not sure how I got the project into such a weird state.
     
  12. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
    Just for posterity. If anyone runs into this issue, where your inspector looks like the left-hand panel below (with error "No MonoBehaviour...") and thus can't add your Component to a game object:



    One workaround is to restore the old names, and then create a new file for the new name. In more detail: Say you want to rename A.cs with class A to B.cs with class B.

    1. You've renamed A -> B and Unity now looks like the left-hand panel above. (with the "No Monobehaviour ..." error)
    2. Revert B.cs with class B back into A.cs with class A.
    3. Click into Unity, ensure that Unity recognizes A.cs with class A (matching the right-hand panel above, without the "No Monobehaviour..." error)
    4. Create a new file B.cs with class B.
    5. Click into Unity, and Unity will now recognize B.cs correctly.

    At least, this worked for me. Probably something to do with the meta files?
     
    florianhanke likes this.