x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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,

Product Version: UE 4.14
Tags:
more ▼

asked Jan 03 '17 at 12:56 AM in C++ Programming

avatar image

Kabe
41 1 3 10

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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:

https://answers.unrealengine.com/questions/389059/how-do-you-instantiate-a-new-uobject.html https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Objects/index.html

  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

more ▼

answered Jan 04 '17 at 08:56 PM

avatar image

Kabe
41 1 3 10

(comments are locked)
10|2000 characters needed characters left

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

more ▼

answered Jan 03 '17 at 12:53 PM

avatar image

MieszkoZ STAFF
7.3k 223 56 410

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question