x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Attaching camera to Pawn at runtime

I'm trying to call a function for attaching an already created camera to the calling pawn, but I can't even find the camera in the scene. The UCameraComponent is a static member of the MyPlayerController class. This is what I do in the constructor:

 PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));

And this is the function that is called by the pawn (after the pawn's BeginPlay() method is called):

 void AMyPlayerController::SetCurrentPawn(AMyPawn* Pawn)
 {
     
     if (PlayerCamera && Pawn && Pawn->CameraSpringArm)
     {
         CurrentPawn = Pawn;
         PlayerCamera->AttachTo(Pawn->CameraSpringArm);
         //UCameraComponent* C = CreateDefaultSubobject<UCameraComponent>(TEXT("asdf"));
         //C->AttachTo(Pawn->CameraSpringArm);
         UE_LOG(MyLog, Warning, TEXT("playercamera->attachment %s"), PlayerCamera->GetAttachParent() == NULL? TEXT("NULL") : TEXT("NOT NULL"));
         
     }
 
 }

I get the output "NOT NULL", so I'd assume that the camera was actually attached to the pawn. When I look in the editor, that's apparently not the case. The editor crashes if I try to run the outcommented code. What might be the problem?

edit: also the engine crashes when I try to access a UCameraComponent Pointer that was declared in the MyPawn class, instead of usind a local variable.

Product Version: UE 4.7
Tags:
more ▼

asked Aug 29 '15 at 07:29 PM in C++ Programming

avatar image

maxim6394
16 8 11 14

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I think you overthinking this :p I think you not aware of fact that PlayerController can do what you trying to do, which i assuming is switching pawn possession and switching camera

First you can switch pawn attached to controller using Possess function:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AController/Possess/index.html

Then from controller you can access possess Pawn from GetPawn() as well all PlayerController function will cooperate with that pawn (like input controls)

2nd, the camera. Camera system in UE4 is actor focus based, you set actor that will be View Target for PlayerController with this function:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/APlayerController/SetViewTarget/index.html

When camera menager has view target set, it will ask view target (which is actor) on every frame for camera position by calling CalcCamera in view target actor:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/CalcCamera/index.html

In default code in CalcCamera (which oyu can override) it picks first camera component in actor and uses it's coordinates for camera (yes CameraComponent is not really needed for camera to work, but it's helpful).

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/CalcCamera/index.html

Here you got default code from engine source code:

 void AActor::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
 {
     if (bFindCameraComponentWhenViewTarget)
     {
         // Look for the first active camera component and use that for the view
         TInlineComponentArray<UCameraComponent*> Cameras;
         GetComponents<UCameraComponent>(/*out*/ Cameras);
 
         for (UCameraComponent* CameraComponent : Cameras)
         {
             if (CameraComponent->bIsActive)
             {
                 CameraComponent->GetCameraView(DeltaTime, OutResult);
                 return;
             }
         }
     }
 
     GetActorEyesViewPoint(OutResult.Location, OutResult.Rotation);
 }

As you can see it all about setting OutResult which contains camera attributes.You can freely override this function, if you have 2 or more camera components in actor you need to use this function to select one of them accprding to some conditions

Also keep in mind that when you possess pawn, by default it's automatically becomes view target. You can disable this behavior by setting bAutoManageActiveCameraTarget to false in your PlayerController

more ▼

answered Aug 30 '15 at 04:00 AM

avatar image

Shadowriver
36.6k 932 169 1111

avatar image maxim6394 Aug 30 '15 at 10:39 AM

The engine also crashed when I try to use the possess function:

     AController* PlayerController = GetWorld()->GetFirstPlayerController();
     
     PlayerController->Possess((APawn*)this);
avatar image Shadowriver Aug 30 '15 at 08:23 PM

Why are you casting? where do you place that code? If it's a class that is not related with APawn you will have a crash, only Pawns can be possesed. If class is related to APawn, you don't need to cast at all

avatar image IHateGlamour Sep 15 '15 at 06:58 PM

I don't get it, so why it is different when adding camera inside character from calling it from controller ? Why we should override CalcCamera in controller, but it's fine if we just put it in a character ?

avatar image Shadowriver Sep 15 '15 at 08:08 PM

You can do either, any you can pick any actor as view target (including non-physical actors like AControllers and AGameModes) and each actor can be coded to deal with camera differently. However you do it's you to decide.

Also lets go to your quastion with this disscussion so we won't talk in 2 diffrent quastions

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question