Reputation: 97
Hej, I've been trying to click on StaticMeshComponent during Run-time, but not able to click on particular StaticMeshComponent. I've tried the below logic to click and set the material inside OnClick() Function, but not able succeed. Is AddDynamic approach correct ??
In .cpp
StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP"));
ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh_obj(TEXT("/Game/StarterContent/Meshs/Chairs_Chair1"));
StaticMeshComponent->SetStaticMesh(StaticMesh_obj.Object);
StaticMeshComponent->OnClicked.AddDynamic(this, &AMyActor::OnClick);
StaticMeshComponent->AttachTo(RootComponent);
// OnClick Function
void AMyActor::OnClick(UPrimitiveComponent* pComponent)
{
ConstructorHelpers::FObjectFinder<UMaterial> MeshMaterial(TEXT("/Game/GTFreeMaterials/Materials/Metal_BrushedSteel"));
// Set properties for Staic mesh component
StaticMeshComponentArray[i]->SetMaterial(0, MeshMaterial.Object);
FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("Clicked")));
}
In .h
void OnClick(UPrimitiveComponent* pComponent);
Could you guys please help me in this issues Or guide me any another efficient logic to solve my problem.
P.S: Game Mode : Mouse Click mode
Upvotes: 4
Views: 11209
Reputation: 552
I just put together a quick test and everything worked correctly. Perhaps the steps I used will help you:
1,Launch UE4.18; Create a new C++ project with Basic Code, Desktop/Console, Maximum Quality, and No Starter Content
2,Add a Player Controller C++ class and use the default constructor so show the mouse cursor, and enable click and mouse-over events
3,Open the C++ Game Mode class that was generated with the project and use the default constructor to set the default Player Controller class to my new class
4,Edit the Project Settings to use the C++ Game Mode class (Maps & Modes -> Default Modes -> Default GameMode)
5,Add a new Pawn C++ class with a click function that simply logs a message to the output log and registers that function in the default constructor
6,Add a blueprint class derived from my C++ Pawn class and add a Cube(Static Mesh Component) to its components, then check "Hidden in Game" in it's Detail Panel.
Or
If want to use Character’s default CollisionProfile Pawn
to receive Clicked Event
, Visibility
must to be set as Block
( Project Settings -> Engine -> Collision -> Preset -> Pawn -> Trace Type -> Visibility).
A strict way is to add new Trace Channel
for Click testing, see more details on this video:https://www.youtube.com/watch?v=yo9VrxFgUJY
7,Place one of my blueprint Pawns into the scene in front of the player
8,Play the level, click the cube of my blueprint Pawn class, view the Output Log and see that the message appeared
Major code:
in AMyCharacter Constructor
OnClicked.AddUniqueDynamic(this, &AMyCharacter::OnSelected);
in header AMyCharacter.h
UFUNCTION()
void OnSelected(AActor* Target, FKey ButtonPressed);
in AMyCharacter.cpp
void AMyCharacter ::OnSelected(AActor* Target, FKey ButtonPressed)
{
GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Cyan, FString("EEEEEEEEEEEEEEEEE"));
}
Upvotes: 3