- Home /
Foldout loop structure problems
Greetings community !
I wrote code that draws tons of foldouts, depending on Data file information it reads. Once user clicks and expands an foldout I draw even more controls that read even more data ! That works perfectly!
Problem comes in nature of my for loop and the way you check is an foldout expanded.
Let's take a look, shall we ? :)
for (var xy : int =0; xy <=2; xy++){
if (i >= l_Array.Length){
break;
}
t_ListFoldouts[xy] = EditorGUILayout.Foldout(t_ListFoldouts[xy], l_Array[i]);
if (t_ListFoldouts[xy]){
l_Array[i] = EditorGUILayout.TextField("Sub Entry1 :", l_Array[i]);
i++;
l_Array[i] = EditorGUILayout.TextField("Sub Entry 2", l_Array[i]);
i++;
l_Array[i] = EditorGUILayout.TextField("Sub Entry 3", l_Array[i]);
var tmpInt : int = parseInt(l_Array[i]);
i++;
for (var c = 0; c <= tmpInt-1; c++){
l_Array[i] = EditorGUILayout.TextField("Sub Entry 4", l_Array[i]);
i++;
l_Array[i] = EditorGUILayout.TextField("Sub Entry 5", l_Array[i]);
i++;
}
}
}
So where is the problem ?
Anything bellow this IF statement :
if (t_ListFoldouts[xy]){
// Code
}
... logically only gets executed if bool is true. In order for bool to be true, user has to expand Foldout.
That doesn't fit my needs as I write ID of entry in foldout and because of that code all foldouts are the same until user expands each one of them.
Solution to this is to expand all foldouts, but that's not what I'm after, I want to keep ability to expand them by wish.
Is there some way around this ?
Regards.
Uhm, this is pure chaos... Is it possible that your "`l_Array`" is a flattened array of some sort? This doesn't make much sense, especially that you use parts of the array to describe how much has to follow. Such a layout can very easily corrupt all your data. Is it possible to describe more what exactly you want to do and what kind of "data" this l_Array
actually contains? Is that data in any special format? I guess you read those strings from a file?
Have you realized that when you collapse the foldout you're no longer increasing your "i" value. To get the same i for the next foldout you would need to increase i manually by the same amount it would have been increased when the foldout was visible.
Also have you realized that when the user edits this "Sub Entry 3" you change how many following lines are displayed within the same foldout. That would shift all data the comes next. Again this doesn't make any sense, so please explain what's the point of all this.
Hi Bunny83, thanks for sparing some time.
l_Array is strictly formated array of Strings. I read data from storage to it, I format that data. Data is always in same format because I also write strictly in that format to file.
There is no fear of corruption there.
Yes, I'm aware that code wont run if foldout is not expanded. That is exactly what I'm trying to change.
Finally, - yes I'm aware that user can change that field, he wont be able to, this information will most likely be read-only, even though it's not the case in code. I'm aware of what this code does and how it does it.
What I'm trying to achieve is this :
I have formated storage file that has data in this order :
int - An ID value
String - Simple string value
int - int value that declares how many additional sub items there are
String - an string value of first sub item
int - an int value of first sub item
I'm reading this data into i_Array of Strings. I format this data additionally to make sure there wont be any problems with converting it later.
And finally, I'm trying to show this data in editor using foldout.
An example of how foldout should look :
^ Foldout - first ID int value
sub foldout 1 - ID int value again
sub foldout 2 - String
sub foldout 3 - int (the one that decides how many sub items are there)
sub foldout 4 - String
sub foldout 5 - int
Where values 4 and 5 are repeating as many times as entry 3 tells them to. Again code is working properly and is showing data correctly, it's fixed environment chance to get any kind of mistake is for storage file to get corrupted.
Problem is obviously in that if statement. If nothing expands that foldout, boolean wont ever trigger IF statement and piece of my code wont work.
So what I need here is only a way to show this hierarchy properly and I can't at the moment because we have to check if boolean is active in order to show sub-items... which requires IF statement which is causing problems.
I hope that I was a bit more clear this time, I'm not good with explaining problems.
On picture you can see window "in action", where repeat 1 and 2 entries are repeating as many times as sub entry 3 tells them to.
Edit : Fixed formating, it's really weird here on Q&A.
Answer by Srki94 · Oct 15, 2014 at 03:59 PM
Have you realized that when you collapse the foldout you're no longer increasing your "i" value. To get the same i for the next foldout you would need to increase i manually by the same amount it would have been increased when the foldout was visible.
This indeed solved the issue. In IF statement I added else and manually increased i in the same way I increase it when user expands foldout, thus resulting in correct data display.
I will wait for Bunny83 to convert his comment to answer so that I can tag it as answer.
Thanks :)
Your answer
Follow this Question
Related Questions
EditorGUI.Foldout consumes click so GUI.Button doesnt work when inside of foldout region. 1 Answer
EditorGUI.ObjectField placed right below EditorGUI.Foldout? 1 Answer
How can PropertyDrawer with fouldouts to draw at the appropriate height? 3 Answers
Initialising List array for use in a custom Editor 1 Answer
EditorGUI.Foldout -- Cannot interact with contents! 1 Answer