先列代码
// 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()
{
}
从此类中派生出两个蓝图类,其中一个蓝图类在编辑框的值能够随着输入值的变化而变化,一个不能随时计算


原因在于
void AMyActor::PostInitProperties()
{
Super::PostInitProperties();
this->CalculateValues();
}
#if WITH_EDITOR
void AMyActor::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
this->CalculateValues();
Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif
本文详细解析了在Unreal引擎中如何通过C++实现蓝图类的自定义属性计算,展示了如何使蓝图在编辑时响应属性改变并即时更新计算结果。通过具体代码示例,介绍了AMyActor类的实现,包括属性设置、函数调用以及在编辑器中响应属性变化的方法。
1527

被折叠的 条评论
为什么被折叠?



