Trouble Setting Lists within Custom Struct

Forums 💬 FlowCanvas ⚙️ Support Trouble Setting Lists within Custom Struct

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #12215
    fisj
    Participant

      I’m trying to use a list within a list using a custom struct. But the list values all seem to be identical in each struct list entry. In the object editor here I’d expect:

      Element 0 – [Number – 0, NumList (3)]
      Element 1 – [Number – 1, NumList (1)]
      Element 2 – [Number – 2, NumList (4)]

      But all entries of NumList show as having an int of 4. What stupid thing am I doing wrong here? This one is driving me a bit bonkers, so any help would be greatly appreciated!

      #12218
      Gavalakis
      Keymaster

        Hello there,

        Hmm.. Can you please pose for me here the struct used as a variable and the rest of the code used so that I can replicate this behaviour?

        Thank you!

        #12217
        fisj
        Participant

          Thanks for the response! Project attached as a zip. Minus the actual installation of flowcanvas. I’m simply using the latest. Unity version 2018.4.36f1.

          The struct is as simple as it gets:

          public struct Struct4
          {
          public float number;
          public List<float> numList;
          }

          #12216
          Gavalakis
          Keymaster

            Hello again and sorry for the late reply.

            It was a bit tricky figuring out what is going on, but. Because Lists are passed by reference, within the whole script the “buffer_List” is passed by reference. This is why any changes made anywhere that list is referenced (“buffer_struct4”, “buffer_List” and “List_Struct4”) also take place everywhere else that list is referenced as well.

            Thus what is happening in your script is that you are basically “Adding” the same list reference to the “List_Struct4” on each iteration and by the end “List_Struct4” ends up having 3 references of the same list.

            The important thing is that Lists are passed by reference and structs are passed by value. A List in a Struct (as in your case) is still passed by reference.

            I hope this helps.  Let me know.

            Thanks!

          Viewing 4 posts - 1 through 4 (of 4 total)
          • You must be logged in to reply to this topic.