Forums › 💬 NodeCanvas › 🗨️ General Discussion › Share Blackboard across Agents with same Behaviour Tree › Reply To: Share Blackboard across Agents with same Behaviour Tree
Hello,
Hmm. There are two possible solutions that I can think of for this.
1) You can use the Blackboard API, to set the variables by code directly. For example:
1 2 3 4 5 6 7 |
public void SetVariables(){ var bb = GetComponent<Blackboard>(); bb.SetValue("myFloat", 13f); bb.SetValue("myColor", Color.yellow); } |
2) You could save a Blackboard variables to a serialized json string and store it somewhere, then deserialize the target blackboard you want, using that json. Some example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Examples : MonoBehaviour { public string blackboardPresetJson; public void SaveToJson(){ var bb = GetComponent<Blackboard>(); blackboardPresetJson = bb.Serialize(); } public void LoadFromJson(){ var bb = GetComponent<Blackboard>(); bb.Deserialize(blackboardPresetJson); } } |
You could of course also create some editor utility to save the json string to a file in editor only, like for example:
1 2 3 4 5 6 7 8 9 10 11 |
[ContextMenu("Save Attached Blackboard To Preset File")] public void SaveToFile(){ var bb = GetComponent<Blackboard>(); var path = EditorUtility.SaveFilePanelInProject("Save Preset", string.Empty, "bb", string.Empty); if (!string.IsNullOrEmpty(path)){ System.IO.File.WriteAllText( path, bb.Serialize() ); AssetDatabase.Refresh(); } } |
Then in runtime you would need to of course have a reference to that saved TextAsset file and deserialize the blackboard from it’s contents. Once again, for example:
1 2 3 4 5 6 7 8 |
public TextAsset presetFile; public void LoadPreset(){ var bb = GetComponent<Blackboard>(); bb.Deserialize( presetFile.text ); } |
Let me know if any of the above solutions works for you.
Thanks!