UE4 C++与蓝图的继承问题
生活随笔
收集整理的这篇文章主要介绍了
UE4 C++与蓝图的继承问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
C++写了一个类MyChar,并派生了一个蓝图子类BP_MyCharacter。
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include "GameFramework/Character.h" #include "MyCharacter.generated.h"using namespace UP; using namespace UF;UCLASS() class CPPTEST_API AMyCharacter : public ACharacter {GENERATED_BODY()public:// Sets default values for this character's propertiesAMyCharacter();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;UPROPERTY(VisibleAnywhere)class UCameraComponent* fpsCamera;};// Fill out your copyright notice in the Description page of Project Settings.#include "MyCharacter.h" #include <Camera/CameraComponent.h>// Sets default values AMyCharacter::AMyCharacter() {// 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;fpsCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FpsCamera"));//fpsCamera->SetupAttachment(RootComponent);//fpsCamera->SetRelativeLocation(FVector(0, 110, 0));}// Called when the game starts or when spawned void AMyCharacter::BeginPlay() {Super::BeginPlay();auto info = FString::Printf(TEXT("-11mychar,inst:%d"), this);GEngine->AddOnScreenDebugMessage(-1, 21, FColor::Red, info);}// Called every frame void AMyCharacter::Tick(float DeltaTime) {Super::Tick(DeltaTime);}// Called to bind functionality to input void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {Super::SetupPlayerInputComponent(PlayerInputComponent);}实验一,做如下操作:
1,修改蓝图中的fpsCamra的位置,任意,如世界空间位置(0,0,200)
2,注释掉构造函数中的fpsCamera的创建代码,并编译。这时fpsCamera在蓝图中消失了。
AMyCharacter::AMyCharacter() {// 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;//fpsCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FpsCamera"));//fpsCamera->SetupAttachment(RootComponent);//fpsCamera->SetRelativeLocation(FVector(0, 110, 0));}3,恢复构造函数中的fpsCamera的创建代码,并编译,这时fpsCamera在蓝图中又出现了。但位置被重置为世界空间的(0,0,0)。原因是2,3两步相当于先删除了相机组件再新加了一个相机组件,所有数据都是默认的了。但如果步2不删除相机组件,而仅修改相机位置,编译后发现蓝图中相机位置是没有任何改变的,原因是构造函数执行时先执行父类构造再执行蓝图子类构造,这样C++构造函数中的位置修被蓝图中的位置修改覆盖掉了。
这时候资源浏览器中BP_MyCharacter并没有带星号,也就是说UE编译器没有监视到蓝图的数据发生了改变。
AMyCharacter::AMyCharacter() {// 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;fpsCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FpsCamera"));//fpsCamera->SetupAttachment(RootComponent);//fpsCamera->SetRelativeLocation(FVector(0, 110, 0));}4,关闭UE4,不主动保存蓝图。重新打开UE4,打开BP_MyCharacter,发现fpsCamera的位置是世界空间的(0,0,200)。而如果关闭UE4之前,主动保存一下蓝图,再打开UE4,打开蓝图,发现fpsCamera的位置是世界空间的(0,0,0)
总结,此问题的关键三要素是:父类内存,子类内存,磁盘数据。
总结
以上是生活随笔为你收集整理的UE4 C++与蓝图的继承问题的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 子线程适当Sleep的重要性
- 下一篇: 一个非常奇怪的C++拷贝构造函数问题