How to create a clickable Actor?

Hi, I’m trying to learn how to do things in c++. I have a blueprint that holds a static mesh of a minable rock. For now I just want to create a simple message when the rock is clicked so I know I can get it working.

So I added a c++ actor component to this blueprint and I’m trying to use delegates to know when it has been clicked. This is what I have so far, I’m not sure where to go from here. If it makes a difference I’m using the top down player controller starting project

header:

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "ClickableMiningRockComponent.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FClickedDelegate);

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class GAME_API UClickableMiningRockComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	UClickableMiningRockComponent();

	UFUNCTION(BlueprintCallable, Category = "Clickable")
	void OnClicked();

	UPROPERTY(BlueprintAssignable, Category = "Clickable")
	FClickedDelegate OnClick;	
};

cpp file

#include "ClickableObject/MiningRocks/ClickableMiningRockComponent.h"

// Sets default values for this component's properties
UClickableMiningRockComponent::UClickableMiningRockComponent()
{
	OnClick.AddDynamic(this, &UClickableMiningRockComponent::OnClicked);
}

void UClickableMiningRockComponent::OnClicked()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Actor has been clicked"));
	OnClick.Broadcast();
}

If you want “clickable” as with the cursor, then there are already events for that.
Set the UI mode to “UI and game” and bind the “clicked” event on the object and you should be fine.

If you want “clickable” as in “when object is in the center of the screen, clicking will mine it,” then you have to cast a ray from the camera to detect collisions, or use a collision object (capsule, sphere, or slim box) on front of the camera, and when the mineable object enters this volume, turn on the ability to mine it with some flag in your controller and display the appropriate message in your HUD.

1 Like

Ya, I know about those, but I want a bunch of logic to happen when it is clicked, and I want that to happen in c++. How would I do that?

You use AddDynamic() to bind a particular function to the delegate in question, and the function that you bind then Does The Things ™

It looks like you’re already binding something to click, so what’s the actual problem?
What are you trying to do that doesn’t work?

1 Like

You’re right, I don’t know why I was complicating things. I Just used the onclick event and set it to my c++ function

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.