Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamic Array/List

Discussion in 'Scripting' started by Arvid_2, Jan 16, 2015.

  1. Arvid_2

    Arvid_2

    Joined:
    Jul 3, 2013
    Posts:
    12
    Hi Guys.
    I'm trying to figure out the best way of creating a 3 dimensional array (x,y,z) of letters (a,b,c etc).

    I would like:
    01. All axis to be able to grow and shrink dynamically.
    02. Easy access to change the content
    03. Move whole columns and rows up and down and sideways.
    04. They would preferably be as light as possible (Saving and loading)

    So my question is What is my best tool for this?

    Thanks in advance.

    Arvid
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    I would make your own struct or class with a b c in it for public variables than use list<my class>()
     
  3. Arvid_2

    Arvid_2

    Joined:
    Jul 3, 2013
    Posts:
    12
    Thanks for the quick reply. just to make sure I've explained myself right here's a drawing:
    Untitled-1.jpg
    I basically want to be able to switch the red index to whatever string I want and do the other things i wrote in the first post.
    Is that how you understood my question too?

    Thanks guys.
    Much appreciated

    A
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Why are you using strings? Why can't you just use a List<Vector3> ?
     
    sluice likes this.
  5. Arvid_2

    Arvid_2

    Joined:
    Jul 3, 2013
    Posts:
    12
    hey guys.
    Maybe I'm thinking the wrong way about this but how would List<Vector3> work IF:
    I want to let's say add a "q" to index x1 y6 z3 OR read what value x4 y7 z0 holds at the moment in the list?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Ah. I see. Just roll your own then

    Code (csharp):
    1.  
    2. public class IndexedString
    3. {
    4.     public int X { get; private set; }
    5.     public int Y { get; private set; }
    6.     public int Z { get; private set; }
    7.     public string Value { get; set; }
    8.  
    9.     public IndexedString(int x, int y, int z, string value)
    10.     {
    11.         this.X = x;
    12.         this.Y = y;
    13.         this.Z = z;
    14.         this.Value = value;
    15.     }
    16. }
    17.  
    Code (csharp):
    1.  
    2. var indexedStrings = new List<IndexString>();
    3.  
    Lookups are a bit trickier. Nested Dictionaries are the most straightforward approach.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Dictionary <int3, string>
    where the int3 key is a custom struct
    Wrapped with custom indexer and methods to shift rows & colums
     
    KelsoMRK and Jessy like this.