Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Methods in my IComponentData struct

Discussion in 'Data Oriented Technology Stack' started by supermoof, Apr 29, 2018.

  1. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    45
    Hello, I'm having trouble trying to get something to work, probably something I don't know about C# so it would be nice to learn from this.

    I have a method in my IComponentData struct, which turns a bool1 to true.

    Example:
    Code (CSharp):
    1.  
    2. public struct Test: IComponentData
    3. {
    4.     public bool1 testBool;
    5.  
    6.     public void MakeTrue()
    7.     {
    8.         testBool = true;
    9.     }
    10. }
    11.  
    So in my system, I have it calling:
    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         for (var i = 0; i < data.Length; i++)
    4.         {
    5.             data.Test[i].MakeTrue();
    6.             Debug.Log(data.Test[i].testBool);
    7.         }
    8.     }
    but it doesn't seem to work, still returns false. If anyone can lecture me on why this doesn't work, it would be awesome :)
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,856
    It's late, I'm tired and lying in bed but I think the problem is you're misunderstanding how structs work.

    Structs are copied so if you break it up this is what you're code is doing is

    Code (CSharp):
    1.             var test = data.Test[i];
    2.             test.MakeTrue();
    3.             Debug.Log(data.Test[i].testBool); // false
    4.             Debug.Log(test.testBool); // true
    To get the behaviour you want you have to apply it back.

    Code (CSharp):
    1.             var test = data.Test[i];
    2.             test.MakeTrue();
    3.             data.Test[i]  = test;
    4.             Debug.Log(data.Test[i].testBool); // true
    This is why mutable structs are considered evil and something you should avoid in general
     
    supermoof likes this.
  3. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    45
    Thank you! Yeah, it was just my misunderstanding on how structs work. If mutable structs should be avoided, as an alternative solution, if I want my values to be changed through a method, should I use a monobehaviour instead?
     
    Last edited: Apr 29, 2018
  4. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    165
    You shouldn't have any methods in your components. Use systems only
     
  5. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    45
    So instead of having a method in my struct, for example, changing 3 booleans to true, I would have the method in my system change the variables instead.

    Like this?
    Code (CSharp):
    1.     void Test(int index)
    2.     {
    3.         Test t = data.Test[index];
    4.         t.testBool1 = true;
    5.         t.testBool2 = true;
    6.         t.testBool3 = true;
    7.         data.Test[index] = t;
    8.     }
     
  6. Kender

    Kender

    Joined:
    Nov 16, 2012
    Posts:
    165
    Yep. Or you may even not use a separate method - simply embed this code in your struct's loop