Details
New in Unreal Engine 5 is T Object Pointers `TObjectPtr`. TObjectPtr's are supposed to be a drop in replacement for `UObjects` to replace raw pointers such that the `TObjectPtr` will automatically cast itself to a raw pointer on functions that expect it.
One common issue when migrating projects from UE4 to UE5 is the blackboard asset. The `UseBlackboard()` function is expecting a "reference to a pointer" or "pointer by reference".
Mark Godwin's blog post (https://markgodwin.blogspot.com/2009/08/c-reference-to-pointer.html) does a good job explaining the differences between a reference to a pointer and a raw pointer. The key difference here between a raw pointer and a pointer by reference, is that a pointer is actually a copy within the function. Compared to a pointer by reference that is actually pointing to the pointer itself so you can modify the pointer rather than the copy.
To explain this a little further let's run this simple code here that's showing the differences between a raw pointer and a pointer by reference.
```
#include <iostream>
using namespace std;
// Pointer By Reference
void function_a(int *&a) {
*a += 5;
int *c = new int(7);
a = c;
}
// Regular Pointer
void function_b(int *a) {
*a += 5;
int *c = new int(7);
a = c;
}
int main() {
int *myInt = new int(5);
int *myInt2 = new int(5);
function_a(myInt);
// function_a returns 7
std::cout << "function_a = ";
std::cout << *myInt << std::endl;
function_b(myInt2);
// function_b returns 10
std::cout << "function_b = ";
std::cout << *myInt2 << std::endl;
return 0;
}
```
If we were to run this we we'll see that we get 7 and 10.
The difference is the "pointer by reference" we're incrementing by `5` so we expect that to be `10` however, because we're creating a new pointer and we are then assigning `a` to `c` we're actually modifying the a pointer outside of this function so the result is 7.
In our function_b, which is utilizing a regular pointer we increment that same pointer by `+= 5` and we create a new pointer `c` when we assign `a` to `c` we're only modifying the a pointer within this function_b so we do not modify the original a pointer and that's why we're getting `7` instead `10`.
To make this work what we can utilize the `Blackboard.Get()` function and put that into our new temporary pointer and pass that to our `UseBlackboard()` function.
Once we're done utilizing it we need to update our blackboard again from our temporary pointer `this->Blackboard = BlackBoardTemp;`
Rebuild the project and you'll see the errors are fixed.
This is a good way to utilize the T Object Pointers `TObjectPtr` when migrating from Unreal Engine 4 to Unreal Engine 5.
Since T Object Pointers `TObjectPtr` can only cast to a raw pointer it can't do the reference to a pointer or pointer by reference which is what's needed in this `UseBlackboard()`
function.