Skip to content
\n

My doubt this: are the values in the blackboard always stored as strings? If so, is there a way to guarantee type safety?

\n

eg: Image a key:value pair with std:int type and value 3. In another action node, later on in the tree, would I be able to save an std:float into the same key?

\n

Thank you for your time.

","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"

great question!

\n

BB storage

\n

Storage is done with a variant of std::any. if you are unfamiliar with it, I strongly recommend you learn how that works.
\nstd::any implements type erasure (store any type), but preserving the original object AND its type.

\n

NOW, in addition to any, we do:

\n\n

But wait, there is more.

\n

If you DO KNOW the type of a blackboard entry / port, we will automatically TRY to convert the string to that type.

\n

Answer

\n

I am assuming you will use the provided getInput and setOutput interfaces (as you should).

\n

You can not connect a OutputPort<float> to InputPort<int> because they haven't the same type.

\n

If everything works as intended, you should get an exception during the creation of the tree.

\n

You can do, anyway getInput<float>() of an integer port, if that is not causing numerical cancellation.

","upvoteCount":1,"url":"https://github.com/BehaviorTree/BehaviorTree.CPP/discussions/614#discussioncomment-6508170"}}}

Question: Type-safety of the blackboard values #614

Closed Answered by facontidavide
MithunKinarullathil asked this question in Q&A
Discussion options

You must be logged in to vote

great question!

BB storage

Storage is done with a variant of std::any. if you are unfamiliar with it, I strongly recommend you learn how that works.
std::any implements type erasure (store any type), but preserving the original object AND its type.

NOW, in addition to any, we do:

  • small object optimization to prevent allocating memory in the heap if the object is 16 bytes or less.
  • smart casting. For instance, a regular std::any would complain if you try to cast uint16_t to int32_t, but instead I check if there is any numberical error in the casting, and if there isn't, it is allowed.

But wait, there is more.

If you DO KNOW the type of a blackboard entry / port, we will automatically TRY …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by facontidavide
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #569 on July 21, 2023 10:11.