How to get Camera Location and Rotation in C++ if my Character Class is in Blueprint

I’m accessing my FollowCamera (Imgur: The magic of the Internet) from my BluePrintCharacter class in my C++ code, with this following line :

FollowCamera = GetOwner()->FindComponentByClass<UCameraComponent>();
FVector WorldLocationV = FollowCamera->GetWorldLocation();

But when I run my game, I get this error : GetWorldLocation is not a member of UCameraComponent

Why is this, and how can I get GetWorldLocation from FollowCamera in C++?

In BluePrint this seems to work fine : http://i.imgur.com/8m9TWUW.png

Since you added the component in the blueprint version of a class, you can’t use any references in C++ that were automatically created by that blueprint such as MyClass->FollowCamera, because as far as C++ is concerned that property isn’t declared and so doesn’t exist (and technically it doesn’t.) What you can do is try finding the component by class in the actor and then store that as a new reference in C++.

The straight up easiest thing to do though is to define your references in C++ as a parent class and derive the blueprint part of your class from that. That was UCameraComponent * FollowCamera exists in a C++ class and so C++ can reference it directly (and so can your derived blueprint.)

1 Like

The straight up easiest thing to do though is to define your references in C++ as a parent class and derive the blueprint part of your class from that. How can I do this exactly? I’m sorry, I’m confused :confused:

And what am I doing isn’t finding the component by class? Like you say it your first solution? Why doesn’t it work then if so, thank you btw!!

My bad, I was only looking at your screenshots.

This is how you solve this problem for any future C++ issue where you don’t know exactly what you should be calling:

  1. Google UCameraComponent
  2. Search the page for “location”
  3. If it’s not found, click the parent class in the inheritance list up top
  4. Repeat

In this case you’ll need to go to USceneComponent, where you’ll find GetComponentLocation() is the right function. Likewise for AActor derived classes the call is GetActorLocation(). Don’t assume that blueprints are a perfect reflection of C++. Read the API documentation as the first step when you’re stuck.

1 Like

Usually you do the opposite, you create stuff in c++ and you access it from blueprints.
I would recommend you some courses on Udemy if you are new to c++ in unreal engine 4.
And if you search tutorials online, most of the time they will be in blueprint, what you can do is to see how to do certain stuff and make them yourself in code, it’s really good as training.
Because usually all the functions that are in blueprints there are also in c++, sometimes with different names, for example in blueprint if you do “GetWorldLocation” of a component, in c++ it’s called “GetComponentLocation”, but it’s the same thing. So everything you do in blueprint can be done in c++

This is the link to the documentation on UCameraComponent, so you can find all the variables and functions (Or you just access it through code, unreal it’s open source)
https://docs.unrealengine.com/en-US/…ent/index.html

What you would do is to create a UCameraComponet in c++ and attach it to the player through code, so you don’t have to access it from blueprint, and to get the world location from the camera you don’t use “GetWorldLocation” but “GetComponentLocation”. You can see there are the same by debugging and print the value, of “GetWorldLocation” in blueprint and “GetComponentLocation” in c++

Create a new ThirdPersonProject for example in c++, the character it’s already created by unreal so you can go inside the code and see how they do it

Edit:
I also suggest using JetBrain Rider or Visual Assist (unfortunately they are all pretty expensive, but you know). Because you can see what functions you can call.
e1fcfca00c4d4768a4e057c9411299e4.jpg

Or you just follow the documentation and check which functions are available.

1 Like

you can also use the camera manager to get the camera reference.

Also if you look at your blueprint, the camera is casted to a uscenecomponent, which has the getcomponentlocation exposed.

1 Like