private void LoadBlackboardState()
    {
        if (File.Exists(Application.persistentDataPath + "/SaveFile"))
        {
            BlackboardStateSave save;
            try
            {
                BinaryFormatter bf   = new BinaryFormatter();
                FileStream      file = File.Open(Application.persistentDataPath + "/SaveFile", FileMode.Open);
                save = (BlackboardStateSave)bf.Deserialize(file);
                file.Close();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                Debug.LogError("You most likely need to delete the SaveFile file at '" +
                               Application.persistentDataPath + "', start/stop the game and look at the exception " +
                               "thrown on playmode exit");
                throw;
            }

            foreach (KeyValuePair <string, BVarSave> entry in save.savedEntries)
            {
                if (!blackboard.KeyExists(entry.Key))
                {
                    Debug.LogWarning($"Key {entry.Key} does not exist for the current blackboard. Skipping.");
                    continue;
                }

                BlackboardVariable finalValue = blackboard.GetValue(entry.Key);
                if (entry.Value.GetType() != finalValue.GetSaveType())
                {
                    Debug.LogWarning($"Value {entry.Value.GetType()} does not match the value type of current " +
                                     $"blackboard's {entry.Key} key, which is {finalValue.GetType()}. Skipping.");
                    continue;
                }

                if (finalValue.persistenceType == PersistenceType.SavedToFile)
                {
                    finalValue.LoadFrom(entry.Value);
                }
            }

            foreach (KeyValuePair <string, BlackboardVariable> entry in blackboard.AsList())
            {
                if (entry.Value != null && entry.Value.persistenceType != PersistenceType.SavedToFile)
                {
                    entry.Value.SnapshotState();
                }
            }
        }
        else
        {
            Debug.Log("No saved blackboard data found.");
        }
    }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            EditorGUI.BeginChangeCheck();

            position.height = 18f;
            SerializedProperty keyProperty        = property.FindPropertyRelative("key");
            SerializedProperty blackboardProperty = property.FindPropertyRelative("blackboard");
            SerializedProperty useConstProperty   = property.FindPropertyRelative("useConstant");

            MonoBehaviour inspectedComponent = property.serializedObject.targetObject as MonoBehaviour;

            // search only in the same game object
            if (inspectedComponent != null)
            {
                // Blackboard blackboard = inspectedComponent.GetComponent<Blackboard>();
                Blackboard blackboard = GetBlackboardInParent(inspectedComponent);
                if (blackboard != null)
                {
                    // Draw mode toggle if not disabled
                    if (property.FindPropertyRelative("mode").enumValueIndex == 0)
                    {
                        Rect togglePosition = position;
                        togglePosition.width       = 8;
                        togglePosition.height      = 16;
                        useConstProperty.boolValue = EditorGUI.Toggle(togglePosition, useConstProperty.boolValue, constVarGUIStyle);
                        position.xMin += 10;
                    }

                    // Draw constant or dropdown
                    if (useConstProperty.boolValue)
                    {
                        // Use constant variable
                        EditorGUI.PropertyField(position, property.FindPropertyRelative("constantValue"), label);
                    }
                    else
                    {
                        System.Type          desiredVariableType = fieldInfo.FieldType.BaseType.GetGenericArguments()[0];
                        BlackboardVariable[] variables           = blackboard.GetAllVariables();
                        List <string>        keys = new List <string>();
                        keys.Add("None");
                        for (int i = 0; i < variables.Length; i++)
                        {
                            BlackboardVariable bv = variables[i];
                            if (bv.GetType() == desiredVariableType)
                            {
                                keys.Add(bv.key);
                            }
                        }
                        // Setup dropdown
                        // INFO: "None" can not be used as key
                        int selected = keys.IndexOf(keyProperty.stringValue);
                        if (selected < 0)
                        {
                            selected = 0;
                            // If key is not empty it means variable was deleted and missing
                            if (!System.String.IsNullOrEmpty(keyProperty.stringValue))
                            {
                                keys[0] = "Missing";
                            }
                        }
                        int result = EditorGUI.Popup(position, label.text, selected, keys.ToArray());
                        if (result > 0)
                        {
                            keyProperty.stringValue = keys[result];
                            blackboardProperty.objectReferenceValue = blackboard;
                        }
                        else
                        {
                            keyProperty.stringValue = "";
                            blackboardProperty.objectReferenceValue = null;
                        }
                    }
                }
                else
                {
                    EditorGUI.LabelField(position, property.displayName);
                    int indent = EditorGUI.indentLevel;
                    EditorGUI.indentLevel = 1;
                    position.y           += EditorGUI.GetPropertyHeight(keyProperty); // + EditorGUIUtility.standardVerticalSpacing;
                    EditorGUI.PropertyField(position, keyProperty);
                    position.y += EditorGUI.GetPropertyHeight(blackboardProperty);    // + EditorGUIUtility.standardVerticalSpacing;
                    EditorGUI.PropertyField(position, blackboardProperty);
                    EditorGUI.indentLevel = indent;
                }
            }


            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
            }

            EditorGUI.EndProperty();
        }