How to create custom events in UE5?

388 views Asked by At

I created a c++ class actor, created a function, and called an event inside that function, but that event does not work.

MyTestActor.h:

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyTestActor.generated.h"

UCLASS()
class TUTORIAL_CPP_API AMyTestActor : public AActor
{
    GENERATED_BODY()

public:
    // Sets default values for this actor's properties
    AMyTestActor();

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TestValues") float test_value_A;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TestValues") float test_value_B;

    UFUNCTION(BlueprintCallable, Category = "TestFunctions") float TestingSummValue(float a, float b);

    UFUNCTION(BlueprintImplementableEvent, Category = "TestFunctions") void OnValuesSummed();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

MyTestActor.cpp:

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


#include "MyTestActor.h"

// Sets default values
AMyTestActor::AMyTestActor()
    : test_value_A(3)
    , test_value_B(.1415926)
{
    // 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;

}

float AMyTestActor::TestingSummValue(float a, float b)
{
    OnValuesSummed(); // !!!! <<<<< i call event here!
    return a + b;
}

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

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

}


The blueprint

I expected that I will get 2 messages on output: some number and message "hello", but i didn't get "hello".

Event BeginPlay works, but my custom event doesn't. I want to make it work.

Output

Lower branch isn't activated

I expected that to activate lower branch of blueprint

0

There are 0 answers