How to initialize a C++ Blackboard

I am building an C++ based AIController, and I would like to use the Blackboard but not linked with a BehaviorTree.

So, on the constructor of the controller I create the blackboard:

BlackboardComponent = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComponent"));

and normally on the override of the Possess(APawn* pawn) method

BlackboardComponent->InitializeBlackboard(*(character->BehaviorTree->BlackboardAsset));
BlackboardComponent->SetValueAsObject(TargetKey, character->FirstNode);
BehaviorTreeComponent->StartTree(*(character->BehaviorTree));

But, since I don’t want to use the behavior tree, I can not figure out how to initialize or setup the blackboard from C++.

BlackboardComponent->InitializeBlackboard( **???** );
BlackboardComponent->SetValueAsObject("Target", character->FirstNode);

Thanks in advance,

You can construct a Blackboard asset in the content manager without using BT. It’s a separate asset type and is associated with BTs only for convenience. You need to build such an asset and use it to initialize your BB.

Cheers,

–mieszko

Thank you Mieszko. My problem was more complex than just NewObject<>.
I had to read some of the UE4 source code to figure out what was going on, in the end I took me a bit of time to understand how the implementation of the Blackboards work. I leave here the steps with some clarification, it may help someone else to figure things out quickly in the future (maybe me in a year time or so).

  1. you were right you can just instantiate a by using NewObject (an you need to follow the UE4 object instantiation)
    BlackboardAsset = NewObject();

Interesting reads:

  1. Prep the UBlackboardData.
    Blackboard does not work as a dynamic Dictionary data structure, that means that simply SetValue does not work it.
    This was may main problem, took me a while to get that, since the documentation is non existent, there was no log, warnings or error messages (I suggest to add a warning in the Log system at least). The SetValue(…) in the UBlackBoardComponent return a bool, but the SetValueAs…(…) are void

So before you initialize the UBlackboardComponent you need to define the key (names) and associated types you want to store. So an obvious function would be to have a AddKey function either on the BlackboardComponent or BlackboardData, but, such thing does not exist. Again documentation was not helpful at all, do I had to dig back into the UE4 code to try to figure out how to add keys.

In the middle of reading 4 or 5 Update methods with very intuitive names I found and Update method which finally allows me to add Keys and finally solve what could have been a very strait forward process.

API reference:

https://docs.unrealengine.com/latest/INT/API/Runtime/AIModule/BehaviorTree/UBlackboardData/index.html
https://docs.unrealengine.com/latest/INT/API/Runtime/AIModule/BehaviorTree/UBlackboardComponent/index.html

In short here is my final solution:

AStateController::AStateController() {
	// Create Blackboard Key Manager
	BlackboardAsset = NewObject<UBlackboardData>();
	BlackboardAsset->UpdatePersistentKey<UBlackboardKeyType_Object>(FName("Target"));

	BlackboardComponent = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComponent"));
}

void AStateController::Possess(APawn * pawn) {
	Super::Possess(pawn);
	AStateNPC* character = Cast<AStateNPC>(pawn);

	if (character) {
		BlackboardComponent->InitializeBlackboard(*BlackboardAsset);
		BlackboardComponent->SetValueAsObject("Target", character->FirstNode);
	}
	else {
		UE_LOG(LogAIStealth, Error, TEXT("AStateController is not linked the a AStateNPC."));
	}
}

UE4 Engine suggestions for improvement:

  1. Log a warning message when unsuccessful key is not found on SetValue
  2. Add a clear API document at least to the
  3. Clear instruction on how to set the UBlackboardComponent