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,注釋掉構(gòu)造函數(shù)中的fpsCamera的創(chuàng)建代碼,并編譯。這時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,恢復構(gòu)造函數(shù)中的fpsCamera的創(chuàng)建代碼,并編譯,這時fpsCamera在藍圖中又出現(xiàn)了。但位置被重置為世界空間的(0,0,0)。原因是2,3兩步相當于先刪除了相機組件再新加了一個相機組件,所有數(shù)據(jù)都是默認的了。但如果步2不刪除相機組件,而僅修改相機位置,編譯后發(fā)現(xiàn)藍圖中相機位置是沒有任何改變的,原因是構(gòu)造函數(shù)執(zhí)行時先執(zhí)行父類構(gòu)造再執(zhí)行藍圖子類構(gòu)造,這樣C++構(gòu)造函數(shù)中的位置修被藍圖中的位置修改覆蓋掉了。
這時候資源瀏覽器中BP_MyCharacter并沒有帶星號,也就是說UE編譯器沒有監(jiān)視到藍圖的數(shù)據(jù)發(fā)生了改變。
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,關(guān)閉UE4,不主動保存藍圖。重新打開UE4,打開BP_MyCharacter,發(fā)現(xiàn)fpsCamera的位置是世界空間的(0,0,200)。而如果關(guān)閉UE4之前,主動保存一下藍圖,再打開UE4,打開藍圖,發(fā)現(xiàn)fpsCamera的位置是世界空間的(0,0,0)
總結(jié),此問題的關(guān)鍵三要素是:父類內(nèi)存,子類內(nèi)存,磁盤數(shù)據(jù)。
總結(jié)
以上是生活随笔為你收集整理的UE4 C++与蓝图的继承问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 子线程适当Sleep的重要性
- 下一篇: 一个非常奇怪的C++拷贝构造函数问题