GUI interactive array error

Hello,

I have another problem that I can’t seem to figure out. I simplified the code and added comments for easier reading.

Desired result:
I’d like to have the user type in a number. The number will be the size of the array and create a list below. In the list will be a textfield section for user input followed by a label.

Current situation:
When the program is run, a window pops up with a label asking the user to input a number for an array size. I didn’t put the conditional test to see if it’s just numbers so ignore the errors if you put a letter instead of a number there. When the user puts a number, the array goes to work and creates sections below the input section.

Problem:

  • Result 1 (assuming untouched code from below) - when the code below is ran, the array looks like it’s working. Rows equal to the user input will be created and properly named with TextFields to the left. The labels confirm that the increment is working properly. When the user selects and types ANYTHING in those windows, it is placed in all windows. So I tested with putting that editable section in an array (see result 2)
  • Result 2 (assuming untouched code from below) - This section is commented out. If you comment out lines 8 and 51 and uncomment lines 8 and 54 (basically swapping out the string for a string array) and run the code, a single label is posted but no textfield. An error pops up saying the array index is out of range. I have tried messing with the array index ([i+1] and [i-1]) to get back into range, but they didn’t worked.
  • Result 3 (assuming untouched code from below) - When commenting out line 51 and uncomment line 55, the same error as result 2 occurs.

Code:

// rectangle variable block
var rect_Window : Rect = Rect(30, 30, 300, 200);
var rect_Label : Rect = Rect (20, 20, 100, 20);
var rect_Text_ArrayCount : Rect = Rect(120, 20, 20, 20);

// edit text block
var edit_ArrayCount : String = "0";
var edit_NumberInArray : String = "";
//var edit_NumberInArray : String[];

// array variable block
var tempArray_Count : int[];
var tempArray_Edit : String[];

 
function OnGUI()
{
	// creates the window
	rect_Window = GUI.Window (0, rect_Window, GetArray, "Interactive Array");
}

// creates the stuffs in the window
function GetArray()
{
	// general label
	GUI.Label(rect_Label, "Enter array size:");
	// creates an editable text field for the user to modify
	edit_ArrayCount = GUI.TextField(rect_Text_ArrayCount, edit_ArrayCount, 5);

	// if the text field (which currently is a string) has something, anything, in the field...
	if (edit_ArrayCount != "")
	{	
		// creates a temporary variable and converts the string to an int
		var arrayCount : int = parseInt (edit_ArrayCount);
		tempArray_Count = new Array (arrayCount);
//		tempArray_Edit.length = tempArray_Count;
		// padding variables for loop
		var cumulativePadding : int = 20;
		var currentPadding : int = 30;
		// because tempCount ALWAYS has a 0 value... not sure why...
		var i : int = 0;
		for (tempCount in tempArray_Count)
		{
			// accumulate padding
			currentPadding += cumulativePadding;
			// creates a label from the loop
			GUI.Label(Rect(60, currentPadding, 100, 20), "temp label" + i);
			
			// problem section
			  // situation 1
			edit_NumberInArray = GUI.TextField(Rect(30, currentPadding, 20, 20), edit_NumberInArray, 5);

			  // situation 2
//			edit_NumberInArray[i] = GUI.TextField(Rect(30, currentPadding, 20, 20), edit_NumberInArray[i], 5);
//			tempArray_Edit[i] = GUI.TextField(Rect(30, currentPadding, 20, 20), tempArray_Edit[i], 5); 

			// end problem section
			i++;
		}
	}
}

Any thoughts on the matter is appreciated.

I also noticed that Array.length isn’t letting me set the array length in the script. I get an error saying that it’s “read only.”

Thanks,

-S

  1. The reason you can’t change the length property of an array directly is because it’s read-only (like the compiler said). The length is set when the array is created (which you never did). Try this instead
tempArray_Edit = new Array(tempArray_Count);
  1. Not sure where tempCount came from…

  2. I’m not sure which arrays are supposed to hold what, so I’m just making tempArray_Edit store the contents of the text fields.

for (var i = 0; i < tempArray_Edit.length; i++)
{
	// accumulate padding
	currentPadding += cumulativePadding;
	// creates a label from the loop
	GUI.Label(Rect(60, currentPadding, 100, 20), "temp label" + i);

	tempArray_Edit[i] = GUI.TextField(Rect(30, currentPadding, 20, 20), tempArray_Edit[i], 5); 
}

If these isn’t what you need exactly, give me some more information on what variables are supposed to store what, and for what reasons, and I’ll help you out.

@sccrstud92
Thank you for the reply.
tempCount was a test I think I forgot to take out when trying to solve the problem. Not sure anymore as it’s all kind of a blur and mixed together. I’ll try your solution in a bit.
As for what I’m doing, the code is a draft of the first part of a longer script. When I have a problem, I try to isolate and start over to get rid of the clutter. tempArray_Edit is supposed to store integers for use later.

-S

I have replaced some sections of the code with what was posted but now no new rows are being created.

Here is the code as it stands.

// rectangle variable block
var rect_Window : Rect = Rect(30, 30, 300, 200);
var rect_Label : Rect = Rect (20, 20, 100, 20);
var rect_Text_ArrayCount : Rect = Rect(120, 20, 20, 20);

// edit text block
var edit_ArrayCount : String = "0";
var edit_NumberInArray : String = "";

// array variable block
var tempArray_Count : int[];
var tempArray_Edit = new Array ();

 
function OnGUI()
{
	// creates the window
	rect_Window = GUI.Window (0, rect_Window, GetArray, "Interactive Array");
}

// creates the stuffs in the window
function GetArray()
{
	tempArray_Edit = new Array (tempArray_Count);
	
	// general label
	GUI.Label(rect_Label, "Enter array size:");
	// creates an editable text field for the user to modify
	edit_ArrayCount = GUI.TextField(rect_Text_ArrayCount, edit_ArrayCount, 5);
	
	// if the text field (which currently is a string) has something, anything, in the field...
	if (edit_ArrayCount != ""  edit_ArrayCount != 0  edit_ArrayCount != "0")
	{	
		// creates a temporary variable and converts the string to an int
		var arrayCount : int = parseInt (edit_ArrayCount);

		var cumulativePadding : int = 20;
		var currentPadding : int = 30;

		for (var i = 0; i < tempArray_Edit.length; i++)
		{
			// accumulate padding
			currentPadding += cumulativePadding;
			// creates a label from the loop
			GUI.Label(Rect(60, currentPadding, 100, 20), "temp label" + i);

			tempArray_Edit[i] = GUI.TextField(Rect(30, currentPadding, 20, 20), tempArray_Edit[i], 5); 
		}
	}
}

here is a draft the desired result.

  • The top most window is what happens when the code is run. The gray box is for user input.
  • The middle window is what happens if the user puts “2” in the first gray box. Two rows would be created. Each row would have a user input section to the left of a label.
  • The third box shows that each of the two boxes created are independent of each other and can store their own values for further use.

-S

Still having problems. Can anyone help with my situation?
-S

Phew, after hours and hours of aggravation, I think I found a solution to my problem.

For one, arrays don’t like GUI.Label as GUI.Labels are voids and arrays want Objects.
Secondly, var newArray : Array = new Array(); doesn’t like GUI.TextField. It errors, “NullReferenceException: Object reference not set to an instance of an object.” This has been HUGE headache as I’ve been trying to find a work around from that situation. I think it has to do with the fact that Array isn’t considered a string, per say.

My solution goes with making a Builtin string array since I can declare the type at variable deceleration.

edit_Array : String[ ];
edit_Array[0] = GUI.TextField (Rect(60, 250, 100, 20), edit_Array[0]);

That works… and I’m going with it.

I hope this thread may save people headaches in the future.

-S