01 Basic
Last updated
Last updated
This is a basic example showing how to create a simple blackboard.
These files can be found in the /Examples/[example] and /Blackboards/Examples folders.
using CrashKonijn.Blackboard.Contracts;
namespace CrashKonijn.Blackboard.Blackboards.Examples
{
// This attribute will enable viewing the generated source for the blackboard in the inspector.
// Only enable this if you can't use Rider or Visual Studio to view the generated source.
[DebugBlackboard]
public partial class BasicBlackboard : BlackboardBehaviour
{
// This will generate a HealthSignal containing an int value on this same class.
// Even though you can't see it in this file, it has been added in another 'partial'.
// The source generators are automatically run by Unity's compilation process!
// To view the other partial/generated code you can do the following:
// In Rider: ctrl + click on the classname
// In Visual Studio: Place your cursor on the classname and press F12. The declarations window should show and you can select the generated partial. The file is <FileName>_<lite|pro>_generated.cs.
private int health = 100;
}
}
using CrashKonijn.Blackboard.Blackboards.Examples;
using CrashKonijn.Blackboard.Contracts;
using UnityEngine;
namespace CrashKonijn.BlackboardPro.Examples._01_basic
{
[RequireComponent(typeof(BasicBlackboard))]
public class BasicHealthBehaviour : MonoBehaviour
{
/* Simple hack to show a button in the editor */
[Header("Removes 10 health.")]
[Button(nameof(DoDamage))]
public string doDamageButton;
/* Simple hack to show a button in the editor */
[Header("Adds 10 health")]
[Button(nameof(Heal))]
public string healButton;
private BasicBlackboard blackboard;
public BasicBlackboard Blackboard
{
get
{
if (this.blackboard == null)
{
this.blackboard = this.GetComponent<BasicBlackboard>();
}
return this.blackboard;
}
}
public void DoDamage()
{
this.Blackboard.Health -= 10;
}
public void Heal()
{
this.Blackboard.Health += 10;
}
}
}