Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to set specific component data inside other system loop?

Discussion in 'Project Tiny' started by Moopra, Oct 10, 2020.

  1. Moopra

    Moopra

    Joined:
    Jun 4, 2015
    Posts:
    30
    Let's see. I have a cursor system which calculate player move direction in to float 2.

    Code (CSharp):
    1.   float time = (float)Time.DeltaTime;
    2.             Entities.WithAll<JoyStickCursorTag>().ForEach((ref JoyStickCursorTag cursor, ref Translation translation, ref CursorInput cursorInput) =>
    3.              {
    4.                  var oldPosition = translation.Value;
    5.                  var newPosition = translation.Value +
    6.                      new float3(-cursorInput.InputAxis.x * 0.01f,
    7.                      cursorInput.InputAxis.y * 0.01f, 0);
    8.                  translation.Value = new float3(math.clamp(newPosition.x, 0.320f, 0.434f),
    9.                      math.clamp(newPosition.y, -0.171f, -0.057f),
    10.                      newPosition.z);
    11.  
    12.                  float2 direction = new float2(0, 0);
    13.                  float disX = cursor.translation.Value.x - translation.Value.x;
    14.                  float disY = cursor.translation.Value.y - translation.Value.y;
    15.                  direction.x = disX;
    16.                  direction.y = disY;
    17.  
    18.                  Movement movement = EntityManager.GetComponentData<Movement>(cursorInput.movement);
    19.                  movement.direction = direction;
    20.  
    21.                  UnityEngine.Debug.LogError(movement.direction.x);
    22.  
    23.              }).WithoutBurst().Run();
    However , If I want to set this direction value to the Movement component that attach to my character object and then use MovementSystem to move player. I can't find a way to do that.

    What I have tried.
    1. nested loop , no good.
    2. EntityManager.GetComponentData << this give me a difference Movement (not the one attach to my player)
    3. Two seperate loop , no good.

    Thanks for the help guy!
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Hello @Moopra,
    So I don't fully understand your issue, but I believe you have two systems:
    - One which you calculate movement direction based on (I assume) an on-screen joystick
    - One which you take the movement direction and translate the player
    To pass data from the first system to the other, you have a component called
    Movement
    which has a
    float2
    parameter for direction. The entity with the
    Movement
    component is stored in the
    CursorInput
    component.

    If this is the case, the example you have linked should work, apart from a missing SetComponentData call:
    EntityManager.SetComponentData(cursorInput.movement, movement);

    It is important to remember that we are only dealing with value type components, so any changes to the components has to be set using the
    SetComponentData
    call.

    Let me know if this was indeed the issue you were trying to solve.
     
    Moopra likes this.
  3. Moopra

    Moopra

    Joined:
    Jun 4, 2015
    Posts:
    30
    @Ted_Wikman
    Yes ,You understand my issue correctly.
    That's what I'm missing. Thank you very much!
    Also, Tiny is very promising. Keep your good works!
     
    Ted_Wikman likes this.
  4. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Great that it now works, and thank you for your kind words