如何在 Actor 构造函数中使用 EditAnywhere 变量

问题描述 投票:0回答:1

在一类宇宙飞船中,我有一个变量

float CameraHeight
定义了在网格上方应该创建多高的相机。

我想在蓝图(基于 C++ 类)中设置此变量,以便我可以 选择最佳值。但是,当我从蓝图中更改此变量时,相机不会 改变其位置。我相信这是因为构造函数仅在创建对象时调用,当我尝试更改蓝图中的变量时,它已经被创建了,尽管我不知道如何解决。

是否有可能在 C++ 构造函数中将相机高度设置为等于一个变量,但稍后才在蓝图中初始化该变量的值?

ASpaceship.h


UCLASS() class SPACESHIPSHOOTER_API ASpaceship : public ACharacter { GENERATED_BODY() public: ASpaceship(); protected: virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; // Called to bind functionality to input virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; UPROPERTY(EditAnywhere) float CameraHeight; // This variable UPROPERTY(EditDefaultsOnly) USkeletalMeshComponent* MeshComponent; UPROPERTY() UCameraComponent* CameraComponent; UFUNCTION() void MoveForward(float value); UFUNCTION() void MoveRight(float value); };

ASpaceship.cpp constructor


ASpaceship::ASpaceship() { // 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; MeshComponent = GetMesh(); CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent")); CameraComponent->SetupAttachment(MeshComponent); CameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, CameraHeight)); // Here I try to set the camera position with a variable CameraComponent->bUsePawnControlRotation = true; }
    
unreal-engine4 unreal-engine5
1个回答
0
投票
在 C++ 中,如果添加以下覆盖,您可以拥有一个“构造脚本”功能,该功能将响应您在蓝图中设置的任何值:

//.h virtual void OnConstruction(const FTransform& Transform) override; //.cpp AYourActorClass::OnConstruction(const FTransform& Transform) { Super::OnConstruction(Transform); // Do whatever you want with the camera }
唯一重要的是,在你的Blueprint的Construction Script中,你需要调用Parent Construciton Script。如果您没有看到它的节点,请右键单击构造脚本节点,然后选择“添加对父函数的调用”。

© www.soinside.com 2019 - 2024. All rights reserved.