PaulB
PaulB

Reputation: 131

Add OnClicked listener to AActor in UE 4.18

I am trying to add an event listener to an actor so that I can preform certain tasks when the actor is clicked on in the game. I have tried multiple things including OnClicked.Add, OnClicked.AddDynamic and multiple versions of each that I saw in other answers but they all end up not compiling and giving a similar error.

Here is my current class

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

#include "InterestItem1.h"


// Sets default values
AInterestItem1::AInterestItem1()
{
    // 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;

}

// Called when the game starts or when spawned
void AInterestItem1::BeginPlay()
    {
    Super::BeginPlay();
    OnClicked.AddDynamic(this, AInterestItem1::moveToItem);

}

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

}

void AInterestItem1::moveToItem(UPrimitiveComponent* ClickedComp, FKey 
ButtonPressed){
    UE_LOG(LogTemp, Log, TEXT("meh, they clicked me"));
}

with this I am getting the following error in VS no instance of function template "FActorOnClickedSignature::_Internal_AddDynamic" matches the argument list

and this compile error in the output log

CompilerResultsLog: Error: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Source\Axis_Test_Purged\InterestItem1.cpp(18) : error C3867: 'AInterestItem1::moveToItem': non-standard syntax; use '&' to create a pointer to member
CompilerResultsLog: Error: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Source\Axis_Test_Purged\InterestItem1.cpp(18) : error C2672: 'TBaseDynamicMulticastDelegate<FWeakObjectPtr,void,AActor *,FKey>::__Internal_AddDynamic': no matching overloaded function found
CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: C:\Users\AtLink\Documents\Unreal Projects\Axis_Test_Purged\Binaries\Win64\UE4Editor-Axis_Test_Purged-4805.dll

I'm new to unreal so I could use some simple explanations of what I'm doing wrong. Thanks.

Upvotes: 2

Views: 3135

Answers (1)

Hani
Hani

Reputation: 31

You have an error in your BeginPlay method in line

OnClicked.AddDynamic(this, AInterestItem1::moveToItem);

error C3867: 'AInterestItem1::moveToItem': non-standard syntax; use '&' to create a pointer to member

You should create a pointer to member and pass it to AddDynamic like so:

OnClicked.AddDynamic(this, &AInterestItem1::moveToItem);

error C2672: 'TBaseDynamicMulticastDelegate::__Internal_AddDynamic': no matching overloaded function found

This error also shows, that there is no AddDynamic function that takes the arguments you passed.

There are plenty of examples on the web, for example:

AddDynamic example

I have used this in my own project also:

    // Sets default values
AArea::AArea()
{
    // 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;

    USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
    RootComponent = SphereComponent;

    this->StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
    this->StaticMeshComponent->SetupAttachment(RootComponent);

    OnClicked.AddDynamic(this, &AArea::SelectArea);
}

void AArea::SelectArea(AActor* TouchedActor, FKey ButtonPressed) {
    PlayerController->ChangeSelectedArea(this);
}

Be sure to read a documentation about delegates here:

Unreal Engine Delegates

Hope it helps :)

Upvotes: 3

Related Questions