Instanced Object Reference Variable? (Easy in BP, can't do in C++ ? )

Very simple.

ie using GameCamera, CodeGameCamera, PController, and CodePController:

In Blueprints:
Open GameCamera
Add Variable named PControllerRef
Change type from dropdown
type “PController”
Hover over the list
Select “Reference”

You can now store a variable there at runtime (ie when spawned by the player controller) ; Now I can reference my playercontroller ezpz.

In C++:
Open CodeGameCamera.h

ASSUME ALL OF THESE HAVE THE FOLLOWING ABOVE THEM:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = ReferenceVariables

I’ve tried:

AActor* PControllerRef;

AActor* APController;

APController* PControllerRef;

APController *PControllerRef;

APController* _PControllerRef;

Edit: I’ve also tried this with the code versions of PController. I’ve created my blueprint classes by deriving them from a code class of their same type; just so that I can add functions and whatnot for the heavier stuff that’ll benefit from C++ performance improvements)

None of these work. All I’m trying to do is create a container for an instance. In this case it’s a reference to the player controller that spawns a camera. In other instances it’s going to be the character a piercing bullet has hit (or an array of them or w/e). Point is, I really need to know how to do this. It’s a huge limitation and has forced me to write a couple functions rather than using full c++ just because I can’t figure out how to get these references to work.

Is this a custom Playercontroller ? Is it created in Blueprint with APlayerController as a parent class?

If so, C++ does not know about the BP class you created.

You could always store every BP Playercontroller you derived from APlayerController in:
APlayerController * PCVariableName;

What this will not allow you to do is call function that were created down the inheritance chain.

What I get from your tests is:

  1. You do not seem to be familiar with the concept of pointers (your AActor* just have different names and your APController are the same (it doesn’t matter where the space is)).

  2. Maybe try reading up on the concept of inheritance and pointers/references. Also remember that your c++ classes do not know about the blueprint classes you inherited from the c++ classes.

#1 I knew that but I’ve been up and down Google all day today and one of the things that I saw was with the asterisk over there and, even though it made no sense, I tried it just in case.
#2 I kinda figured which is why I tried giving them the code based classes. Which were refused. That was the very first thing that I tried.

That said, while I understand that it has NO idea that my BP classes exist why can’t it know about them once I’ve compiled? Meaning, what prevents it from eventually seeing them at runtime? I have a vague, obvious understanding (THEY’RE NOT THE SAME and blueprints don’t seem to ever compile down to C++) but is there any way for me to do what I’m trying to do in C++?

To be clear, what I’m trying to do is:

Spawn a Camera actor (not component) via my Player Controller. I want the camera actor to have a reference to my player controller so that it can get the pawn. There are other ways to get it but I need the variable to be stored eventually.
Use the Controller reference to “GetControlledPawn” and store that reference as the camera’s “Target”.
Using the Target reference, get the player’s location and do camera stuff. With math. Math that really should be done in C++.

Worst case scenario I can just do what I’ve been doing. References and actors in BP. “Get Actor location” and pass it in a vector to a C++ function. Just figured there might be a way to do this without having to use what seemed like a “hacky” workaround.

Thanks anyway! :slight_smile:

This can be easily achieved in CPP . In your case, you don’t need the specific class of the PC BP that you have. You just want a APlayerController*

So in your camera actor APlayercontroller * thePC = gameplaystatistics::getplayercontroller(…)

APawn * thePawn= ThePC->getpawn()

This is from memory and written on my phone, so please look up the correct functions/spelling etc.

Thank you VERY much. Not only was this (very very close to) what I was looking for it also taught me (indirectly) more about the -> vs :: operators and I’m now ages ahead of where I was when I typed this up simply because I’m spending less time looking things up and more time guessing (correctly) what the names of the calls/functions I’m looking for are. Before this I was almost never using the -> as I didn’t quite “get it” before. Used it to access some stuff in normal C++ but in UE4 I thought that it was replaced by the :: stuff. So. Thanks. Clearly I still need to keep learning C++.