Download

C++ and Blueprints UFUNCTION and AddDynamic

Hello there. I’m trying to figure out how to create a blueprint of a class. I’ve followed a few guides, youtube videos and other forum posts on how to do this and although only few of them work, none of them explain the properties we’re meant to include in the functions we create that we want to override. I’m sure I’ve explained this poorly and I hope that by showing you my problem you might understand it further and offer advice.

I have a very basic C++ class (ABaseClass).
My nOnClicked method below however doesn’t have any parameters and I believe this is why I’m receiving errors. I assumed that because when I create the On Clicked event node in blueprint viewer it has no parameters that there should be no parameters in this even either.


UCLASS()
class SIM_LITE_API ABaseClass : public AActor
{
	GENERATED_UCLASS_BODY()

	/* Simple collision primitive to use as the root component */
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Component)
	TSubobjectPtr<USphereComponent> CollisionComponent;

	/*	Mesh to store the model data	*/
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Component)
	TSubobjectPtr<UStaticMeshComponent> ModelComponent;
	
	/*	Event	*/
	UFUNCTION(BlueprintNativeEvent)
	void nOnClicked();
};


ABaseClass::ABaseClass(const class FPostConstructInitializeProperties& PCIP) 
	: Super(PCIP)
{
	CollisionComponent = PCIP.CreateDefaultSubobject<USphereComponent>(this, "CollisionComponent");

	RootComponent = CollisionComponent;

	ModelComponent = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, "ModelComponent");

	ModelComponent->AttachTo(RootComponent);

	ModelComponent->OnClicked.AddDynamic(this, &ABaseClass::nOnClicked);
}

void ABaseClass::nOnClicked_Implementation()
{

}

Thanks for your time.


ABaseClass::ABaseClass(const class FPostConstructInitializeProperties& PCIP) 
	: Super(PCIP)
{
	CollisionComponent = PCIP.CreateDefaultSubobject<USphereComponent>(this, "CollisionComponent");

	RootComponent = CollisionComponent;

	ModelComponent = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, "ModelComponent");

	ModelComponent->AttachTo(RootComponent);

	ModelComponent->OnClicked.AddDynamic(this, &ABaseClass::nOnClicked);
}

void ABaseClass::nOnClicked_Implementation()
{

}


 (...) error C2664: 'void TBaseDynamicMulticastDelegate_OneParam<void,UPrimitiveComponent *,FWeakObjectPtr>::__Internal_AddDynamic<ABaseClass>(UserClass *,void (__cdecl ABaseClass::* )(Param1Type),const FString &)' : cannot convert argument 2 from 'void (__cdecl ABaseClass::* )(void)' to 'void (__cdecl ABaseClass::* )(Param1Type)'
1>          with
1>          
1>              UserClass=ABaseClass
1>  ,            Param1Type=UPrimitiveComponent *
1>          ]
1>          and
1>          
1>              Param1Type=UPrimitiveComponent *
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Thanks for your time.

The On Clicked event you’re seeing in your blueprint is in fact based on the function you declared, so of course it won’t have any arguments. For dynamic delegates, you do want a UFUNCTION, but it doesn’t need to be a BlueprintNativeEvent. (Unless you want to subscribe to the delegate in blueprint but if so, just do that in blueprint instead of C++.)

The signature for OnClicked is as follows: FComponentOnClickedSignature( UPrimitiveComponent* TouchedComponent )

So you need to create your handler function according to that signature:


UFUNCTION()
void nOnClicked( UPrimitiveComponent* TouchedComponent );