这一个章节蓝图占大多数,毕竟是材质相关的内容,用蓝图确实会比C++方便不少,这一块唯一一个C++的内容就是新写的一个cube的类。
STargetDummy.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "STargetDummy.generated.h"
UCLASS()
class ACTIONROGUELIKE_API ASTargetDummy : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere)
class UStaticMeshComponent* StaticMeshComp;
UPROPERTY()
class USAttributeComponent* AttributeComp;
public:
// Sets default values for this actor's properties
ASTargetDummy();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//绑定的受击函数
UFUNCTION()
void OnHealthChanged(AActor*InstigatorActor , USAttributeComponent* OwningComp, float NewHealth, float Delta);
};
STargetDummy.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "STargetDummy.h"
#include "Components/StaticMeshComponent.h"
#include "SAttributeComponent.h"
// Sets default values
ASTargetDummy::ASTargetDummy()
{
// 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;
StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh Comp"));
AttributeComp = CreateDefaultSubobject<USAttributeComponent>(TEXT("Attribute Comp"));
AttributeComp->OnHealthChanged.AddDynamic(this,&ASTargetDummy::OnHealthChanged);
}
// Called when the game starts or when spawned
void ASTargetDummy::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASTargetDummy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
//函数实现
void ASTargetDummy::OnHealthChanged(AActor* InstigatorActor, USAttributeComponent* OwningComp, float NewHealth, float Delta)
{
if (Delta<0.0f)
{
StaticMeshComp->SetScalarParameterValueOnMaterials("TimeToHit", GetWorld()->TimeSeconds);
}
}
不过我有个疑问,用老师的这个方法,在刚进入游戏的时候我们的cube就会闪一下,这明显是一个bug,我在蓝图内尝试靠更改TimeToHit的值来解决这个问题,也就是在我们的自定义材质函数内修改值来解决bug,问题也解决了。