I am working on a plugin for Unity for an AI system using behavior trees and blackboards. I assume all of you know what a behavior tree is, but not a blackboard (if you do just skip this explanation), so I will tell you what it is:
What’s a blackboard and a blackboard key:
A Blackboard is basically a place to store all your variables to be used inside of the behavior tree. So let’s take a simple AI for example that will only chase something. The blackboard will have a variable with the target, the behavior tree will chase whatever that variable is set to, and you will set the variable in a script.
Blackboards use a system with keys. They are just like variables, except they have their special type, and they are being accessed with a script (kinda like a parameter in Unity’s animation system).
What I am trying to do and the script:
So each blackboard will have a list of keys, and when pressing a button, it opens a small window where you can set some things. So I was kinda stuck with the UI for the default value… I will just paste the code here and then explain why it’s bad:
keyType = (BBKeyType)EditorGUILayout.EnumPopup("Type", keyType);
switch (keyType)
{
case BBKeyType.Object:
EditorGUILayout.LabelField("No current default value setter available for type Object");
break;
case BBKeyType.Integer:
try
{
if (defaultValue == null)
defaultValue = new int();
defaultValue = EditorGUILayout.IntField("Default Value", (int)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new int();
}
break;
case BBKeyType.Float:
try
{
if (defaultValue == null)
defaultValue = new float();
defaultValue = EditorGUILayout.FloatField("Default Value", (float)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new float();
}
break;
case BBKeyType.Boolean:
try
{
if (defaultValue == null)
defaultValue = new bool();
defaultValue = EditorGUILayout.Toggle("Default Value", (bool)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new bool();
}
break;
case BBKeyType.String:
try
{
defaultValue = EditorGUILayout.TextField("Default Value", (string)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = null;
}
break;
case BBKeyType.Vector4:
try
{
if (defaultValue == null)
defaultValue = new Vector4();
defaultValue = EditorGUILayout.Vector4Field("Default Value", (Vector4)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new Vector4();
}
break;
case BBKeyType.Vector2:
try
{
if (defaultValue == null)
defaultValue = new Vector2();
defaultValue = EditorGUILayout.Vector2Field("Default Value", (Vector2)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new Vector2();
}
break;
case BBKeyType.Vector3:
try
{
if (defaultValue == null)
defaultValue = new Vector3();
defaultValue = EditorGUILayout.Vector3Field("Default Value", (Vector3)defaultValue);
}
catch (InvalidCastException)
{
defaultValue = new Vector3();
}
break;
}
Explanation about the script:
So some things you need to know first: defaultValue is a variable of type System.Object. In my actual blackboard I set the value using a generic type, so when creating the key, I set the value, and then every time I want to set the value I can use the generic. Only the default one needs to be object, so it can be flexible between all types.
Also keep in mind that it works perfectly fine. This video will show you. I just want to find a better way.
What’s wrong with the script:
Even if you don’t know anything about programming you should know that this is just bad…it’s unreadable, inefficient, and WAYY too long.
Actual question and things I tried:
A thing know would work is to not make that null check, but to also catch a NullReferenceException, since I do the same thing for both that exception and the InvalidCastException. The problem is, that…I could do it using the “when” keyword when catching, but that only exists in C# 6, which Unity doesn’t use! I could upgrade to the 5.5 beta, but that will result in many bugs, since a beta isn’t anywhere close to perfect.
Does anyone here have a better idea for how I can do this?
Some BTW I like to put:
I just put it here to ask if the titles help reading it, since I always make long posts that are kinda hard to follow, so I decided to put it here, because it might be better.



