- Home /
Manually Saving ScriptableObject due to 2D Array...
Hey Guys,
before you drop the "just look it up" Comments. I show you what I tried
SetDirty()
AssetDataBase.Refresh() and AssetDatase.SaveAssets()
Undo.RecordObject()
Make the SO non-static
I freaking don't know anymore.
Everytime I press Play the array doesn't exist and has to be initilaized with the default values. Which means, these 2script are for the dust bin right now. I tried to save it by any means the internet told me to.
So now, you come into play. Save me please. Just look over it and help me figure it out.
FactionsEditor
using UnityEngine;
using UnityEditor;
using System;
[CustomEditor(typeof(Factions))]
public class FactionsEditor : Editor
{
private bool[] folds;
private int max;
private bool update;
private GUIStyle factionStyle, otherFactionStyle;
public void OnEnable()
{
Factions.CheckAndUpdateRelations();
max = Enum.GetValues(typeof(Faction)).GetLength(0);
folds = new bool[max];
factionStyle = new GUIStyle();
factionStyle.fontStyle = FontStyle.Bold;
factionStyle.alignment = TextAnchor.MiddleRight;
otherFactionStyle = new GUIStyle();
otherFactionStyle.alignment = TextAnchor.MiddleRight;
}
public override void OnInspectorGUI()
{
// Doesn't work either
Undo.RecordObject(target, "Factions");
for (int i = 1; i < max; i++)
{
EditorGUILayout.BeginHorizontal();
folds[i] = EditorGUILayout.Foldout(folds[i], "");
GUILayout.FlexibleSpace();
EditorGUILayout.LabelField(Enum.GetName(typeof(Faction), i), factionStyle);
EditorGUILayout.EndHorizontal();
if (folds[i])
{
for (int j = 1; j < max; j++)
{
if (i != j)
{
EditorGUILayout.BeginHorizontal("Label");
EditorGUILayout.LabelField(Enum.GetName(typeof(Faction), j), GUILayout.MaxWidth(120));
// Here I used to check for valueChanges, see below
int stinker = EditorGUILayout.IntSlider(Factions.GetRelations(i,j), 0, 100, GUILayout.MaxWidth(200));
EditorGUILayout.LabelField(GetStand(stinker), otherFactionStyle, GUILayout.MaxWidth(100));
Factions.SetRelations(i, j, stinker);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
EditorGUILayout.EndHorizontal();
}
}
EditorGUILayout.BeginHorizontal("Box");
EditorGUILayout.LabelField("Towards Neutral", GUILayout.MaxWidth(120));
int pimmele = EditorGUILayout.IntSlider(Factions.GetRelations(i, 0), 0, 100, GUILayout.MaxWidth(200));
EditorGUILayout.LabelField(GetStand(pimmele), otherFactionStyle, GUILayout.MaxWidth(100));
Factions.SetRelations(i, 0, pimmele);
EditorGUILayout.EndHorizontal();
}
}
// Actually created two int[,] called oldValues and values and checked if they changed. it actually was quite slow,
// but update didn't at least freeze while constantly saving, like before (even though it didn't save shit!)
if (update)
{
EditorUtility.SetDirty(target);
update = false;
}
}
private string GetStand(int pimmele)
{
if (pimmele <= 10)
{
return "War";
}
else if (pimmele <= 35)
{
return "Cold War";
}
else if (pimmele < 65)
{
return "Neutral";
}
else if (pimmele < 90)
{
return "Partnership";
}
else
{
return "Alliance";
}
}
}
(would really like to ut this stuff in spoilers, but this markdown really is crap)
I used this int[,] because I can't use Lists. The way I constructed this array, was to only use a one way relation, that's why I used Get and Set Relations. To guarantee the consistent one way relation.
Factions
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[CreateAssetMenu]
[Serializable]
public class Factions : ScriptableObject
{
public static int[,] relations;
public static int max;
public static int GetRelations(int first, int second)
{
max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
if (first == second)
{
throw new Exception("Factions are the same");
}
else if (first > max)
{
throw new Exception("First Faction is beyond reach: " + first);
}
else if (second > max)
{
throw new Exception("Second Faction is beyond reach: " + second);
}
else if (second < first)
{
return relations[second, first];
}
else
return relations[first, second];
}
public static void SetRelations(int first, int second, int value)
{
max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
if (first == second)
{
throw new Exception("Factions are the same");
}
else if (first > max)
{
throw new Exception("First Faction is beyond reach: " + first);
}
else if (second > max)
{
throw new Exception("Second Faction is beyond reach: " + second);
}
else if (second < first)
{
relations[second, first] = value;
}
else
relations[first, second] = value;
}
void OnEnable()
{
CheckAndUpdateRelations();
}
public static void CheckAndUpdateRelations()
{
max = Enum.GetValues(typeof(Faction)).GetLength(0) - 1;
int[,] oldRelations = new int[0, 0];
int length;
if (relations == null)
{
length = -1;
}
else if (relations.GetLength(0) < max)
{
oldRelations = relations;
length = oldRelations.GetLength(0);
}
else
return;
if (length < max)
{
relations = new int[max, max + 1];
}
Debug.Log(length);
for (int i = 0; i < max; i++)
{
for (int j = i + 1; j <= max; j++)
{
if (i < length)
{
relations[i, j] = oldRelations[i,j];
}
else
{
relations[i, j] = 50;
}
}
}
}
}
public enum Faction
{
None,
Outlaws,
Merchants,
Security
}