Hi there, not sure if this is the right place to ask this, but I’m having issues with a custom editor I’ve been working on. This is my first real custom editor and I can’t seem to get an array to change its members using EditorGUILayout.ObjectField. The values in the array keep resetting when I change them.
Here’s me editor code:
[CustomEditor(typeof(LootTable))]
public class LootTableEditor : Editor
{
int labelSize = 40;
int lootSize = 250;
int oddsSize = 100;
int tableSize = 1;
bool foldout = true;
LootTable lootTable;
ItemData[] itemDatas;
float[] odds;
private void OnEnable()
{
lootTable = (LootTable)target;
}
public override void OnInspectorGUI()
{
labelSize = EditorGUILayout.IntField("Label Size:", labelSize);
lootSize = EditorGUILayout.IntField("Loot Field Size:", lootSize);
oddsSize = EditorGUILayout.IntField("Odds Field Size:", oddsSize);
tableSize = EditorGUILayout.IntField("Table Size:", tableSize);
itemDatas = new ItemData[tableSize];
odds = new float[tableSize];
foldout = EditorGUILayout.Foldout(foldout, "Table");
if (foldout)
{
for (int i = 0; i < tableSize; i++)
{
GUILayout.BeginHorizontal();
EditorGUIUtility.labelWidth = labelSize;
// This is where the issue is
itemDatas[i] = (ItemData) EditorGUILayout.ObjectField("Loot: ", itemDatas[i], typeof(ItemData), false, GUILayout.Width(lootSize));
odds[i] = EditorGUILayout.FloatField("Odds: ", odds[i], GUILayout.Width(oddsSize));
GUILayout.EndHorizontal();
}
}
}
So I’m trying to add ItemData ScriptableObjects and floats to specify odds to this loot table but every time I change a value in the inspector it gets reset to null or 0 immediately.
I’m not having this issue with the int values like labelSize, only with the 2 values that are in the arrays.
My goal is to set these values in this inspector, then once they’re set, pass them onto the LootTable ScriptableObject (which this is the editor off) to set up the table.
I’ll give this a try as soon as I can, but that doesn’t explain why I’m having the same issue with odds _= EditorGUILayout.FloatField("Odds: ", odds*, GUILayout.Width(oddsSize));.*_ That’s just a float so I would think it should be working.
Sorry to be hard headed, but I’m a beginner. Do you mean no errors as in, it’s compiling? Because my code is compiling, it’s just that I can’t actually change the values of odds and ItemData.
But I can change the value of the other non-array elements. I’m using 2018.3.4f1 though, so I guess I could try to upgrade? I was scarred to do it mid-project haha
And that works fine. I can put in my ItemData objects and my floats just fine, so the issue is with the array elements:
int tableSize = 1;
ItemData[] itemDatas;
float[] odds;
tableSize = EditorGUILayout.IntField("Table Size:", tableSize);
itemDatas = new ItemData[tableSize];
odds = new float[tableSize];
for (int i = 0; i < tableSize; i++)
{
GUILayout.BeginHorizontal();
EditorGUIUtility.labelWidth = labelSize;
itemDatas[i] = (ItemData) EditorGUILayout.ObjectField("Loot: ", itemDatas[i], typeof(ItemData), false, GUILayout.Width(lootSize));
odds[i] = EditorGUILayout.FloatField("Odds: ", odds[i], GUILayout.Width(oddsSize));
GUILayout.EndHorizontal();
}
For whatever reason, it doesn’t work on the array elements, but it does work if I just put in the elements one by one. I really need to get the array working though.
I posted this question on Reddit and I figured out why the values were being reset based on the help there. The issue was this line:
itemDatas = new ItemData[tableSize];
odds = new float[tableSize];
Makes sense, it was making a new array each time.
The issue now is that I can’t seem to figure out a way to directly manipulate the size of these arrays. I tried switching to Lists and still got nowhere.
I’m basically trying to recreate the default way lists and arrays work on the inspector, where you can type in a size, the array adjusts to that size, and then you can enter in values for each element. The key difference being that I’m trying to line up two elements from 2 arrays / lists.
I feel like this should be doable, but I can’t even figure out how to create that default functionality on a single array.