SoulRyder
(SoulRyder)
November 19, 2015, 6:34pm
1
Hi,
Trying to create a custom event, which is similar to for example a OnActorOverlap event.
But I am stuck at the last part, it all seems to work except the function added to the delegate doesnt seem to fire…
Anyone have an idea what I am missing here, kinda new to the whole delegate thing.
I created a Delegate, function and all in my playercontroller.h
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorPickedUpObjectSignature, FName, ObjectName);
/**
*
*/
UCLASS()
class PROJECT_API ALHPlayerController : public APlayerController
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
UPROPERTY(BlueprintAssignable, Category = "Collision")
FActorPickedUpObjectSignature OnPickedUpObject;
virtual void NotifyPickedUpObject(FName ObjectName);
And made a function for it:
void ALHPlayerController::NotifyPickedUpObject(FName ObjectName)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("THIS IS WORKING %s"), *ObjectName.ToString()));
}
And then I try to run it from another actor:
void ALHInteractive_Pickup::ExecuteAction()
{
ALHPlayerController* PC;
PC = Cast<ALHPlayerController>(GetWorld()->GetFirstPlayerController());
PC->OnPickedUpObject.AddDynamic(PC,&ALHPlayerController::NotifyPickedUpObject);
PC->OnPickedUpObject.Broadcast(ActorOptions.ActorName);
}
But this is not working, any idea what I am missing here?
Mattlauk
(Mattlauk)
November 19, 2015, 7:57pm
2
PC->OnPickedUpObject.AddDynamic(PC,&ALHPlayerController::NotifyPickedUpObject);
should be in your ALHPlayerController
constructor as:
this->OnPickedUpObject.AddDynamic(this,&ALHPlayerController::NotifyPickedUpObject);
or else every time you call ExecuteAction()
, it’s going to add it again.
Also, be sure that ALHInteractive_Pickup::ExecuteAction()
is being called.
SoulRyder
(SoulRyder)
November 19, 2015, 9:07pm
3
Hi MJLaukala!
Thanks, I wasnt sure where to put the AddDynamic part, that would make sense to add it in the constructor.
I am going to try this tomorrow when I am at work and will post here if it worked. But I think it will
Thanks
SoulRyder
(SoulRyder)
November 20, 2015, 12:42pm
4
So it is still not working unfortunately
I changed some things because I have to use a multicast delegate and if I am right I should not use AddDynamic but Add for that.
Anyway the debug message “FUNCTION IS BOUND” is fired correctly
But the debug message “THIS IS WORKING TEST TEXT” is not working at all, see my code added below to see what I am talking about.
I put the code in beginplay now for test purposes
HEADER FILE
#pragma once
#include "GameFramework/PlayerController.h"
#include "LHPlayerController.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorPickedUpObjectSignature, FName, ObjectName);
UCLASS()
class LHPROJECT_API ALHPlayerController : public APlayerController
{
GENERATED_UCLASS_BODY()
public:
ALHPlayerController();
virtual void BeginPlay() override;
UPROPERTY(BlueprintAssignable, Category = "Pickup")
FActorPickedUpObjectSignature OnPickedUpObject;
virtual void NotifyPickedUpObject(FName ObjectName);
};
CPP FILE
#include "LighthouseProject.h"
#include "LHPlayerController.h"
ALHPlayerController::ALHPlayerController(const class FObjectInitializer& PCIP)
:Super(PCIP)
{
FScriptDelegate Del;
Del.BindUFunction(this, "NotifyPickedUpObject");
OnPickedUpObject.Add(Del);
}
void ALHPlayerController::BeginPlay()
{
FName testTxt = "TEST TEXT";
Super::BeginPlay();
if (OnPickedUpObject.IsBound())
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("FUNCTION IS BOUND")));
OnPickedUpObject.Broadcast(testTxt);
}
}
void ALHPlayerController::NotifyPickedUpObject(FName ObjectName)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("THIS IS WORKING %s"), *ObjectName.ToString()));
}
SoulRyder
(SoulRyder)
November 20, 2015, 2:02pm
5
Ok well, I found the problem. I guess I expected the wrong things to happen.
The Notify function I have in code, doesn’t fire anything inside it. I do not need that, but I thought it would, so that made searching for the problem a bit more complicated…
So final code here, and in blueprints I had to do something different too. For anyone stumbling upon how to handle this maybe this helps!
PlayerController HEADER:
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorPickedUpObjectSignature, FName, ObjectName);
UCLASS()
class LHPROJECT_API ALHPlayerController : public APlayerController
{
GENERATED_UCLASS_BODY()
public:
ALHPlayerController();
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
UPROPERTY(BlueprintAssignable, Category = "Pickup")
FActorPickedUpObjectSignature OnPickedUpObject;
virtual void NotifyPickedUpObject(FName ObjectName);
};
PlayerController CPP
ALHPlayerController::ALHPlayerController(const class FObjectInitializer& PCIP)
:Super(PCIP)
{
FScriptDelegate Del;
Del.BindUFunction(this, "NotifyPickedUpObject");
OnPickedUpObject.Add(Del);
}
void ALHPlayerController::NotifyPickedUpObject(FName ObjectName)
{
}
PickupObject CPP:
void ALHInteractive_Pickup::ExecuteAction()
{
FInventorySlots ActorOptions;
ActorOptions.ActorName = ObjectName;
PC->OnPickedUpObject.Broadcast(ActorOptions.ActorName);
Destroy();
}
Screenshot from WORKING blueprint below:
Screenshot from blueprint I thought should work but it didn’t, below (Did I do something wrong or is this normal?)
EDIT: OK correction again! (I corrected this saying AddDynamic is working just a s fine as Add…). BUT AddDynamic DOESNT work for MULTICAST. The game will still run but will break to visual studio on compiling. And Add, the way I used it above will not do this