Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Defining an array

Discussion in 'Scripting' started by TomBrien, Sep 1, 2012.

  1. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Hey, I'm working in C# and I just figured out arrays.

    Code (csharp):
    1. arrayOfItems = new string[6];
    2. arrayOfItems[0] = "jumps";
    3. arrayOfItems[1] = "bombs";
    4. arrayOfItems[2] = "money";
    5. arrayOfItems[3] = "rockets";
    6. arrayOfItems[4] = "startspeed";
    7. arrayOfItems[5] = "flux";
    8. arrayOfItems[6] = "shotguy";
    These are the items in my shop.
    In the original prototype, which is a flash game, I saved whether or not each item was NEW, and whether or not it had been bought.
    So I wanna have something like:
    Code (csharp):
    1. arrayOfItems[0] = ["jumps", true, false];
    but how do I do that here?
    ...I COULD make 3 arrays, but I feel like there's a way to do this that I just don't know about yet.
     
  2. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    I am not sure what you're doing here. You can't add bool to string array. Can you explain more what you're trying to do?
    Since you're making array of length 6, your arrayOfItems[6] will fail, as that is item #7 in array. 0-5 = 6 items.
    Depending on what you want to do, List or Dictionary would be better as they can dynamically resize.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should make a class (which contains the item and states for that item), and then make an array of that class.

    --Eric
     
  4. Outlaw-Lemur

    Outlaw-Lemur

    Joined:
    Aug 27, 2012
    Posts:
    45
    No an array can just hold the type specified, no exceptions, unless you made a class like @Eric5h5 said, example:

    Code (csharp):
    1.  
    2. public class StringandBool
    3. {
    4. public string text;
    5. public bool b1, b2;
    6.  
    7. public StringandBool(string Text, bool bool1, bool bool2)
    8. {
    9. text = Text;
    10. b1 = bool1;
    11. b2 = bool2;
    12. }
    13. }
    14.  
    Or you could use a Dictionary like Morning said

    Code (csharp):
    1.  
    2. Dictionary<string,bool,bool> dictionary = new Dictionary<string,bool,bool>
    3. {
    4. {"hi", true, false},
    5. {"bye", false, true},//etc
    6. }
    7.  
    See http://msdn.microsoft.com/en-us/library/xfhwa508.aspx for more information
     
    Last edited: Sep 1, 2012
  5. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    you could also do 3 separate arrays using the same index values to get each objects properties, but the class is a better idea.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You should really avoid the "3 separate arrays" thing, that's just asking for trouble.

    --Eric
     
  7. Outlaw-Lemur

    Outlaw-Lemur

    Joined:
    Aug 27, 2012
    Posts:
    45
    Dictionary is defintaly the way to go now that I think about it
     
  8. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Thanks guys!

    I'm getting an error that 'Dictionary '3' (or Dictionary '2 or however many values I use) isn't a Type. I don't know what to do about that.
    I read the help page, and I read up on what Hashtables are because that seemed relevant, but I still don't know how to do this. Also Dictionary isn't in the Unity Scripting Reference, so I'm not 100% sure what to do with it.
    Oh- here's me trying it:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class dctionaryTest : MonoBehaviour
    5. {  
    6.     public static void Main ()
    7.     {
    8.         Dictionary<string, bool, bool> myFirstDictionary = new Dictionary<string, bool, bool> ();
    9.         myFirstDictionary.Add ("data0", true, false);
    10.         myFirstDictionary.Add ("data1", false, false);
    11.         myFirstDictionary.Add ("data2", false, false);
    12.         myFirstDictionary.Add ("data3", false, false);
    13.     }
    14. }
    Like I said, "Could not find type or namespace Dictionary".
    I was about to give up, but my other game also has a shop/upgrade system, so I guess I better figure this out now.
     
    Last edited: Sep 4, 2012
  9. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    I thought Dictionary had to be a pair of keys, so I don't think you can have 3. In your case string,bool,bool is one to many keys. I solved this exact problem using initialised structs, let me find the code......

    Code (csharp):
    1.  public enum ShipTypes
    2.   {
    3.     PTBOAT,
    4.     FRIGATE,
    5.     DESTROYER
    6.   }
    7.   public struct ships_struct
    8.   {
    9.     public ShipTypes Type;
    10.     public float XSize;
    11.     public float YPos;
    12.     public string ResourceTxt;
    13.  
    14.     public ships_struct(ShipTypes type, float xsize, float ypos, string resourcetxt)
    15.     {
    16.       Type = type;
    17.       XSize = xsize;
    18.       YPos = ypos;
    19.       ResourceTxt = resourcetxt;
    20.     }
    21.   }
    22.  
    23.   public static ships_struct[] Ships = {
    24.     new ships_struct(ShipTypes.PTBOAT,    15.0f,3.0f,"ship_ptboat"),
    25.     new ships_struct(ShipTypes.FRIGATE,   28.0f,3.5f,"ship_frigate"),
    26.     new ships_struct(ShipTypes.DESTROYER, 23.0f,0.0f,"ship_destroyer")
    27.  };
    try something like that. I could have used a class but this seemed to work better, by making the variables public in the struct you don't
    need to add functions to get stuff out of it.
     
  10. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    As Eric5c5 already suggested, create a struct or class for the entries:

    Code (csharp):
    1.  
    2. public class DataEntry
    3. {
    4.   public string Name;
    5.   public bool StateA;
    6.   public bool StateB;
    7.  
    8.   public DataEntry(string name, bool stateA, bool stateB)
    9.   {
    10.     Name = name;
    11.     StateA = stateA;
    12.     StateB = stateB;
    13.   }
    14. }
    15.  
    And then use it:

    Code (csharp):
    1.  
    2. arrayOfItems = new DataEntry[7];
    3. arrayOfItems[0] = new DataEntry("jumps", true, false);
    4. arrayOfItems[1] = new DataEntry("bombs", true, false);
    5. arrayOfItems[2] = new DataEntry("money", true, false);
    6. arrayOfItems[3] = new DataEntry("rockets", true, false);
    7. arrayOfItems[4] = new DataEntry("startspeed", true, false);
    8. arrayOfItems[5] = new DataEntry("flux", true, false);
    9. arrayOfItems[6] = new DataEntry("shotguy", true, false);
    10.  
    Dictionary is only for Key Value pairs, not Key Value, Value
     
  11. TomBrien

    TomBrien

    Joined:
    Apr 28, 2012
    Posts:
    111
    Thanks man, that's perfect. I really got the feeling, looking up Dictionary, that I was using it for the wrong thing.

    And yeah I realised that I was numbering the arrays wrong right after I ade this threads, but thanks anyway everyone who said that.