Search Unity

Arrays in Structs in C#

Discussion in 'Scripting' started by Nic-Cusworth, May 7, 2009.

  1. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    My structs are really messy. I'm having to use things like:

    Code (csharp):
    1. public struct bobStructure
    2. {
    3. int bob1;
    4. int bob2;
    5. int bob3;
    6. }
    when what I really want is:

    Code (csharp):
    1. public struct bobStructure
    2. {
    3. int bob[3];
    4. }
    Arrays in Structs don't seem to be supported. Anyone got a good work around for this??
     
  2. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    int[] bob = new int[3];

    Try that. :)
     
  3. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    unfortunately not :( I get this error:

    Assets/Scripts/map.cs(51,22): error CS0573: `map.mapStructure.bob': Structs cannot have instance field initializers
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    how about
    Code (csharp):
    1. fixed int bob[3];
    :)
     
  5. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Nope :( need to be defined as unsafe and Unity doesn't support unsafe...

    Curses...

    Nic.
     
  6. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Erm... just realised. Should point out this is Unity iPhone - so it's .net 1.0?? I think.
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can have an array in a struct, but you can only declare the size of the array at runtime. You need to declare an int[] field and then set up the array in the constructor:-

    Code (csharp):
    1. struct ExampleStruct {
    2.     int[] ints;
    3.  
    4.     public ExampleStruct() {
    5.         ints = new int[3];
    6.     }
    7. }
     
  8. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    ^^ That's what I was getting at! I was close.
     
  9. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Damn - I feel horrible to keep saying nope, but here's the error I get with the above example:

    Code (csharp):
    1. error CS0568: Structs cannot contain explicit parameterless constructors
    This is the line causing the error:

    Code (csharp):
    1. public ExampleStruct()
     
  10. lehk

    lehk

    Joined:
    Feb 12, 2009
    Posts:
    91
    Try this.

    Code (csharp):
    1. struct ExampleStruct {
    2.     int[] ints;
    3.  
    4.     public ExampleStruct(int[] _ints) {
    5.         ints = _ints;
    6.     }
    7. }
    And to create an instance of the struct:

    Code (csharp):
    1.  
    2. int[] ints = new int[13];
    3. new ExampleStruct(ints);
     
  11. Dobson

    Dobson

    Joined:
    May 1, 2009
    Posts:
    12
    Hi, are you using C#? I'm pretty sure C# does not allow parameterless struct constructors, so alternatively you could perhaps add a constructor with a parameter specifying the length of the array(s), or add an Initialize() function to the struct that does all the array initializing. I'm not sure if these suggestions are very ideal/efficient ways of handling it though, but they seems to work fine.
     
  12. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Some good news! The struct doesn't throw up an error. However, the instance does :(

    Code (csharp):
    1. ExampleStruct _ExampleStruct(ints);
    throws up the error:

    Code (csharp):
    1. Identifier expected
    I'm close to giving up. It seems like a crazy .net 1.0 limitation. :roll:

    But I have to say thank you to all everyone for jumping in to help. It's a great example of how cool the Unity community is.
     
  13. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    Here ya go. This works fine.



    Code (csharp):
    1. public class NewBehaviourScript : MonoBehaviour {
    2.  
    3.     public struct bobStructure
    4.     {
    5.         public bobStructure(int[] temp )
    6.         {
    7.             bob = temp;
    8.         }
    9.         public int[] bob;  
    10.     }
    11.  
    12.     bobStructure t;
    13.    
    14.  
    15.     void Start () {
    16.         t = new bobStructure(new int[3]);
    17.     }
    18.    
    19.     void Update () {
    20.         Debug.Log(t.bob.Length);
    21.     }
    22. }
     
    unity_21msHKaKThaDIw likes this.
  14. Nic-Cusworth

    Nic-Cusworth

    Joined:
    Oct 12, 2008
    Posts:
    218
    Awesome. Thank you so much guys. This is does indeed compile and work!

    Now to tidy up my code!
     
  15. lehk

    lehk

    Joined:
    Feb 12, 2009
    Posts:
    91
    Just to clarify:

    Code (csharp):
    1. ExampleStruct _ExampleStruct(ints);
    This throws error because the syntax for instance an object it's:

    type instance_name = new type(parameters);

    This is how it should look like:
    Code (csharp):
    1. ExampleStruct _ExampleStruct = new ExampleStruct(ints);
     
  16. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Only for completeness: struct, array, constructor, prevent, prevent garbage collection

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestAnything : MonoBehaviour {
    5.  
    6.     public struct bobStructure
    7.     {
    8.         public bobStructure(int[] b1, int[] b2, float[] s1, string[] d1)
    9.         {
    10.             bob1 = b1;
    11.             bob2 = b2;
    12.             joe = 0;
    13.             sue = s1;
    14.             doe = d1;
    15.         }
    16.         public int[] bob1;
    17.         public int[] bob2;
    18.         public float joe;
    19.         public float[] sue;
    20.         public string[] doe;
    21.     }
    22.  
    23.     static int maxdoe = 99;
    24.     public bobStructure t = new bobStructure(new int[3], new int[3], new float[4], new string[maxdoe]);
    25.  
    26.     void Start () {
    27.  
    28.         t.joe = 0.7777777f;
    29.         t.sue[3] = 39;
    30.         t.doe[98] = "John";
    31.  
    32.         Debug.Log("t.bob1[] " + t.bob1.Length);
    33.         Debug.Log("t.bob2[] " + t.bob1.Length);
    34.         Debug.Log("joe " + t.joe);
    35.         Debug.Log("t.sue[3] " + t.sue[3]);
    36.         Debug.Log("t.doe[98] " + t.doe[98]);
    37.     }
    38.    
    39.     void Update () {
    40.     }
    41. }
    42.  
     
    Roman_Galashov likes this.