Hey, I'm working in C# and I just figured out arrays. Code (csharp): arrayOfItems = new string[6]; arrayOfItems[0] = "jumps"; arrayOfItems[1] = "bombs"; arrayOfItems[2] = "money"; arrayOfItems[3] = "rockets"; arrayOfItems[4] = "startspeed"; arrayOfItems[5] = "flux"; 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): 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.
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.
You should make a class (which contains the item and states for that item), and then make an array of that class. --Eric
No an array can just hold the type specified, no exceptions, unless you made a class like @Eric5h5 said, example: Code (csharp): public class StringandBool { public string text; public bool b1, b2; public StringandBool(string Text, bool bool1, bool bool2) { text = Text; b1 = bool1; b2 = bool2; } } Or you could use a Dictionary like Morning said Code (csharp): Dictionary<string,bool,bool> dictionary = new Dictionary<string,bool,bool> { {"hi", true, false}, {"bye", false, true},//etc } See http://msdn.microsoft.com/en-us/library/xfhwa508.aspx for more information
you could also do 3 separate arrays using the same index values to get each objects properties, but the class is a better idea.
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): using UnityEngine; using System.Collections.Generic; public class dctionaryTest : MonoBehaviour { public static void Main () { Dictionary<string, bool, bool> myFirstDictionary = new Dictionary<string, bool, bool> (); myFirstDictionary.Add ("data0", true, false); myFirstDictionary.Add ("data1", false, false); myFirstDictionary.Add ("data2", false, false); myFirstDictionary.Add ("data3", false, false); } } 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.
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): public enum ShipTypes { PTBOAT, FRIGATE, DESTROYER } public struct ships_struct { public ShipTypes Type; public float XSize; public float YPos; public string ResourceTxt; public ships_struct(ShipTypes type, float xsize, float ypos, string resourcetxt) { Type = type; XSize = xsize; YPos = ypos; ResourceTxt = resourcetxt; } } public static ships_struct[] Ships = { new ships_struct(ShipTypes.PTBOAT, 15.0f,3.0f,"ship_ptboat"), new ships_struct(ShipTypes.FRIGATE, 28.0f,3.5f,"ship_frigate"), new ships_struct(ShipTypes.DESTROYER, 23.0f,0.0f,"ship_destroyer") }; 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.
As Eric5c5 already suggested, create a struct or class for the entries: Code (csharp): public class DataEntry { public string Name; public bool StateA; public bool StateB; public DataEntry(string name, bool stateA, bool stateB) { Name = name; StateA = stateA; StateB = stateB; } } And then use it: Code (csharp): arrayOfItems = new DataEntry[7]; arrayOfItems[0] = new DataEntry("jumps", true, false); arrayOfItems[1] = new DataEntry("bombs", true, false); arrayOfItems[2] = new DataEntry("money", true, false); arrayOfItems[3] = new DataEntry("rockets", true, false); arrayOfItems[4] = new DataEntry("startspeed", true, false); arrayOfItems[5] = new DataEntry("flux", true, false); arrayOfItems[6] = new DataEntry("shotguy", true, false); Dictionary is only for Key Value pairs, not Key Value, Value
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.