How to write an Array?

Hello All,

How would I write an Array or where can I learn more about how to write them, as you can see from my code below this just doesn’t seem like the right way to write code because its repetitive and this is just for level one, the next levels have even more. Im really learning from you guys and appreciate everything! Im not looking for someone to do it for me just a few examples of how its done with a short explanation of whats going on, it doesn’t even have to pertain to my script. This place is AWESOME!

//sent from TransportScript
static var roomEntered : int = 1;

var DisplayMapBackground = false;
var MapSkin : GUISkin;
var MapBackground : Texture2D;

var youAreHere : Texture2D;
var missionEnd : Texture2D;
var beenHereBefore : Texture2D;

var room01Here = false;
var room02Here = false;
var room03Here = false;
var room04Here = false;
var room05Here = false;
var room06Here = false;
var room07Here = false;
var room08Here = false;
var room09Here = false;
var room10Here = false;
var room11Here = false;
var room12Here = false;
var room13Here = false;
var room14Here = false;
var room15Here = false;
var room16Here = false;


function OnGUI()
{
	GUI.skin = MapSkin;
	
	if(Input.GetKeyDown(KeyCode.M))
		{
			DisplayMapBackground = true;
			
			if(roomEntered == 1)
			{
				room01Here = true;
			}			
			if(roomEntered == 2)
			{
				room02Here = true;
			}
			if(roomEntered == 3)
			{
				room03Here = true;
			}			
			if(roomEntered == 4)
			{
				room04Here = true;
			}			
			if(roomEntered == 5)
			{
				room05Here = true;
			}			
			if(roomEntered == 6)
			{
				room06Here = true;
			}
			if(roomEntered == 7)
			{
				room07Here = true;
			}			
			if(roomEntered == 8)
			{
				room08Here = true;
			}
			if(roomEntered == 9)
			{
				room09Here = true;
			}			
			if(roomEntered == 10)
			{
				room10Here = true;
			}
			if(roomEntered == 11)
			{
				room11Here = true;
			}			
			if(roomEntered == 12)
			{
				room12Here = true;
			}			
			if(roomEntered == 13)
			{
				room13Here = true;
			}			
			if(roomEntered == 14)
			{
				room14Here = true;
			}
			if(roomEntered == 15)
			{
				room15Here = true;
			}			
			if(roomEntered == 16)
			{
				room16Here = true;
			}									
			
			//make other Gui Health bars to show false because of the size of this map.
		}
		
	if( DisplayMapBackground )
    {
    	GUI.Label (Rect (0,0, 600,450), MapBackground, GUIStyle.none);       
       
       //pause game somehow later later
       
       //Ok Button Change the size 152 x 51  
   	 	if (GUI.Button (Rect (65, 390, 152,51), "","Ok"))
   	 	 {
   	 		DisplayMapBackground = false;
   	 		room01Here = false;   	 		
   	 		room02Here = false;
   	 		room03Here = false;   	 		
   	 		room04Here = false;
   	 		room05Here = false;   	 		
   	 		room06Here = false;
   	 		room07Here = false;   	 		
   	 		room08Here = false;
   	 		room09Here = false;   	 		
   	 		room10Here = false;
   	 		room11Here = false;   	 		
   	 		room12Here = false;
   	 		room13Here = false;   	 		
   	 		room14Here = false;
   	 		room15Here = false;   	 		
   	 		room16Here = false;     	 		    	 		
   	 		   	 		   	 		   	 		
   	 		//unpausegame somehow
   	 		//show other gui health bars to true
   		 }	
   	 
   		if (GUI.Button (Rect (400, 390, 152,51), "", "Invite")) 
   	 	 {
   	 		//Intvite button code		
   		 }
   	}
   	
   	if(room01Here)
   	{
   		GUI.Label (Rect (290,348, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room02Here)
   	{
   		GUI.Label (Rect (290,318, 29,29), youAreHere, GUIStyle.none);	
   	}
	if(room03Here)
   	{
   		GUI.Label (Rect (290,558, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room04Here)
   	{
   		GUI.Label (Rect (290,258, 29,29), youAreHere, GUIStyle.none);	
   	}
	if(room05Here)
   	{
   		GUI.Label (Rect (290,228, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room06Here)
   	{
   		GUI.Label (Rect (290,198, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room07Here)
   	{
   		GUI.Label (Rect (230,258, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room08Here)
   	{
   		GUI.Label (Rect (260,258, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room09Here)
   	{
   		GUI.Label (Rect (320,258, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room10Here)
   	{
   		GUI.Label (Rect (250,258, 29,29), youAreHere, GUIStyle.none);	
   	}
	if(room11Here)
   	{
   		GUI.Label (Rect (350,228, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room12Here)
   	{
   		GUI.Label (Rect (320,198, 29,29), youAreHere, GUIStyle.none);	
   	}
	if(room13Here)
   	{
   		GUI.Label (Rect (260,198, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room14Here)
   	{
   		GUI.Label (Rect (230,198, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room15Here)
   	{
   		GUI.Label (Rect (230,168, 29,29), youAreHere, GUIStyle.none);	
   	}
   	if(room16Here)
   	{
   		GUI.Label (Rect (230,137, 29,29), youAreHere, GUIStyle.none);	
   	}
}

Thanks Again if anyone wants to share some knowledge :smile:

http://unity3d.com/support/documentation/ScriptReference/Array.html

Have you read this?

Yes but Im kind of confused as to how it works? I noticed that everything is in the start function so Im guessing Arrays need to be in the start function. But what Im not getting is if I use them examples it dosent seem like Im really saving any repetitiveness.
Example

function Start()
{
	var arr = new Array();
	
	arr.Push room01Here = false;
	arr.Push room02Here = false;
	arr.Push room03Here = false;
	arr.Push room04Here = false;
}

function OnGUI()
{
	GUI.skin = MapSkin;
	
	if(Input.GetKeyDown(KeyCode.M))
		{
			DisplayMapBackground = true;
			
			if(roomEntered == 1)
			{
				arr[0] = true;
			}			
			if(roomEntered == 2)
			{
				arr[1] = true;
			}
			if(roomEntered == 3)
			{
				arr[2] = true;
			}			
			if(roomEntered == 4)
			{
				arr[3] = true;
			}

So Im thinking I gotta be missing something? Or is it because of the way Ive written the code Im not gonna really save or be able to shorten it up anyway? Any examples would help.

It’s not a requirement to put them in the Start() function. You can create an array anywhere.
To skip the repetitive process, you would use a for loop:

var array : Array = new Array();
var arraySize : int = 20;
var i : int;

for(i = 0; i < arraySize; i++)
{
	var roomHere : bool = false;
	
	array.Push(roomHere);
}

(Untested Code)

When updating your “roomEntered” bit, you can condense it to:

if(Input.GetKeyDown(KeyCode.M))
{
	DisplayMapBackground = true;

	arr[roomEntered - 1] = true;
}

Ok I think that makes sense, so the array.Push(roomHere); will pick up the 01, 02, 03 etc… and I would still call it like array[0], array[1], array[2] which would be the same as roomHere01, roomHere02, roomHere03?

If so thats pretty awesome. Im guessing I could do the same thing for the if statements as well because they are all the same too :slight_smile: Ok Im trying it now. The only ones I probably couldn’t do would be the GUI Labels because they all have different numbers.

But thank you elveates that really explains a lot!

Well, there’s not much point in naming the variables that go into an Array since you call them by array[0], array[1], etc. If it makes it clearer to name the Array rooms, you can call room[0], room[1], room[2], etc. FizixMan has a good solution for the if statement. For GUI Labels, you could label them "Room " + i (i being an int variable).

Hey FizixMan :slight_smile:

so if I shorten up the roomEntered bit How does it know that they are all if statements? lol that one just through me for a loop

if(Input.GetKeyDown(KeyCode.M))
{
	DisplayMapBackground = true;

	arr[roomEntered - 1] = true;
	arr[roomEntered - 2] = true;
	arr[roomEntered - 3] = true;
	arr[roomEntered - 4] = true;	
	
}

I was thinking I could put them in a for loop as well like this:

var array2 : Array = new Array();
var arraySize : int = 16;
var i : int;

for(i = 0; i < arraySize; i++)
{
	var roomEntered : bool = true;
	
	array2.Push(roomEntered);
}

if(Input.GetKeyDown(KeyCode.M))
		{
			DisplayMapBackground = true;			
			
			if(array2 == [0])
			{
				array[0] = true;
			}

But I like your way better if thats how you meant but Im just not sure, how it knows its an if statement?

EDIT: Ahhh… Ok because they are all already in the if statement now that makes sense and I’ll use the room in place of the arr :smile: Now Im getting it :slight_smile: Thank you both! greatly appreciated

roomEntered is the value of the room number (starting from 1 and going to 4)
Arrays start from the number 0 (in this case, 0 through 3)

So rather than saying “if roomNumber is 1, set array entry 0 to true”, we can say, “set array entry roomNumber to true”. We don’t need an “if” statement if every time we are setting a roomNumber entry to true.

(we put the -1 so we can convert from your rooms going from 1 to 4 to the array going from 0 to 3)

Thank You FizixMan, it really helps :smile: Im already thinking of other places in my code where I can use it :slight_smile: and I’ll post the finished script a little later after playing with it for other who may need to know. Again Thank You!