UE4 c++类派生蓝图类

时间:2024-04-04 12:25:01

先列代码

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class TESTUNREALC_API AMyActor : public AActor
{
 GENERATED_BODY()
 
public: 
 // Sets default values for this actor's properties
 AMyActor();

 UPROPERTY(EditAnywhere, BlueprintReadWrite,Category = "Damage")
  int32 TotalDamage;

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
  float DamageTimeInSeconds;

 UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category = "Damage")
  float DamagePerSecond;

 UFUNCTION(BlueprintCallable, Category = "Damage")
  void CalculateValues();

 UFUNCTION(BlueprintImplementableEvent, Category = "DAMAGE")
  void CalledFromCpp();

 void CalledFromCpp_Implementation();
 // Called when the game starts or when spawned
 virtual void BeginPlay() override;
 
 // Called every frame
 virtual void Tick( float DeltaSeconds ) override;

 virtual void PostInitProperties() override;

 virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
 
 
};

// Fill out your copyright notice in the Description page of Project Settings.

#include "testunrealc.h"
#include "MyActor.h"


// Sets default values
AMyActor::AMyActor()
{
  // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
 PrimaryActorTick.bCanEverTick = true;
 TotalDamage = 200;
 DamageTimeInSeconds = 1.0f;
 DamagePerSecond = TotalDamage / DamageTimeInSeconds;

}

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

// Called every frame
void AMyActor::Tick( float DeltaTime )
{
 Super::Tick( DeltaTime );

}

void AMyActor::PostInitProperties()
{
 Super::PostInitProperties();
 this->CalculateValues();
}

void AMyActor::CalculateValues()
{
 DamagePerSecond = TotalDamage / DamageTimeInSeconds;
}

#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
 this->CalculateValues();
 Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif

void AMyActor::CalledFromCpp_Implementation()
{

}

 

从此类中派生出两个蓝图类,其中一个蓝图类在编辑框的值能够随着输入值的变化而变化,一个不能随时计算

UE4 c++类派生蓝图类

 

UE4 c++类派生蓝图类

 

原因在于


void AMyActor::PostInitProperties()
{
 Super::PostInitProperties();
 this->CalculateValues();
}

#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
 this->CalculateValues();
 Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif