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