0
\$\begingroup\$

I'm trying to use ACharacter for my characters, but the game uses ONLY static cameras (ie. they shouldn't even logically be attached to the mesh of the characters). But it seems Unreal's ACharacter will always have a camera spawned, there doesn't seem to be a way to disable it.

I've butchered code from this tutorial. It uses SetViewTargetWithBlend to change between the character's camera and the static one.

When I used that code, it crashed Unreal, and the reason I found was because he hard-coded '0' element of the found array. I added a check to see if array is empty and this stops the crash. Of course this means it is empty and hence the line inside the if block doesn't fire.

But it seems to me I have done exactly what they did in the tutorial. I have ensured that in the character BP I set the variable fixed_camera_classtype:

.h:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Camera/CameraComponent.h"

#include "Player_PnC.generated.h"

UCLASS()
class VOLCANOTOWN_API APlayer_PnC : public ACharacter
{
    GENERATED_BODY()



public:
    // Sets default values for this character's properties
    APlayer_PnC();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    TSubclassOf<ACameraActor> fixed_camera_classtype;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // Called to bind functionality to input
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;


    


    int movement_state = 0;
};

.cpp

#include "Player_PnC.h"
#include "Components/SkeletalMeshComponent.h"
#include "Camera/CameraActor.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"

// Sets default values
APlayer_PnC::APlayer_PnC()
{
    // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void APlayer_PnC::BeginPlay()
{
    Super::BeginPlay();



    //Cast to Player Character's PlayerController

    if (APlayerController* PlayerCharacterController = Cast<APlayerController>(GetController()))
    {
        //Array to contain found Camera Actors
        TArray<AActor*> FoundActors;

        //Utility function to populate array with all Camera Actors in the level

        UGameplayStatics::GetAllActorsOfClass(GetWorld(), fixed_camera_classtype, FoundActors);

        if (FoundActors.Num() != 0)
        {
            //Sets Player Controller view to the first CameraActor found
            PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], 0.0f, EViewTargetBlendFunction::VTBlend_Linear);
        }


    }

    
}

enter image description here

enter image description here

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Ok well I did find a related question that has led me to almost fix my issue. https://stackoverflow.com/questions/39406118/why-getallactorsofclass-returns-empty

It appears I needed the fixed_camera_classtype to be ::StaticClass() somehow.

So far I have done this to get around it , but now I can no longer use my BP version of the camera.

if (APlayerController* PlayerCharacterController = Cast<APlayerController>(GetController()))
    {
        //Array to contain found Camera Actors
        TArray<AActor*> FoundActors;
        TSubclassOf<AFixedCameraActor> classToFind;
        classToFind = AFixedCameraActor::StaticClass();
        UGameplayStatics::GetAllActorsOfClass(GetWorld(), classToFind, FoundActors);

        if (FoundActors.Num() != 0)
        {
            //Sets Player Controller view to the first CameraActor found
            PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], 0.0f, EViewTargetBlendFunction::VTBlend_Linear);
        }
        else
        {
            UE_LOG(LogTemp, Warning, TEXT("foundactors.num is zero"))
        }


    }
\$\endgroup\$
1
  • 1
    \$\begingroup\$ If this solved your problem, remember to click the green checkmark in the top-left of the answer to mark it as "Accepted". \$\endgroup\$
    – DMGregory
    Nov 24, 2022 at 14:06
0
\$\begingroup\$

As long as you've set your Subclass in your APlayer_PnC Blueprint then you already have access to it in code. You don't need to get all actors of class.

Try this instead:

    if (APlayerController* PlayerCharacterController = Cast<APlayerController>(GetController()))
    {
        // Check a Subclass has been set in the Blueprint
        if (fixed_camera_classtype)
        {
            PlayerCharacterController->SetViewTargetWithBlend(fixed_camera_classtype, 0.0f, EViewTargetBlendFunction::VTBlend_Linear);
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .