Announcement

Collapse
No announcement yet.

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

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

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

    I'm accessing my FollowCamera (https://imgur.com/a/vbFagxg) 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
    Last edited by madgusto; 03-26-2020, 02:28 AM.

    #2
    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.)

    Comment


      #3
      Originally posted by Antidamage View Post
      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.)

      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 :/

      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!!

      Comment


        #4
        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.

        Comment


          #5
          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.
          Click image for larger version

Name:	e1fcfca00c4d4768a4e057c9411299e4.jpg
Views:	34
Size:	34.3 KB
ID:	1737817

          Or you just follow the documentation and check which functions are available.
          Last edited by Matty37_; 03-27-2020, 11:06 AM.

          Comment


            #6
            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.
            Last edited by ViperG; 03-27-2020, 01:42 PM.

            Comment

            Working...
            X