Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Select an actor with mouse click and call actor's class function with UMG button c++

I'm trying to select an actor with mouse click and use UMG buttons to for example change material on selected actor. Can someone please guide the right way to implement this in C++ like how to get/save the reference of selected/last clicked actor? Buttons are different materials and I have SetMaterial implemented in cube and sphere classes which takes a material parameter sent by pressed UMG button. Or should it be done some other way? alt text

Product Version: UE 4.19
mouseclick.png (165.8 kB)
more ▼

asked May 08 '18 at 12:15 AM in C++ Programming

avatar image

15 1 2 8

avatar image unreal_eng May 09 '18 at 10:14 AM

Ok I created empty MyUserWidget, MyGameMode, MyPlayerController c++ classes. Then actors classes MyCube, MySphere with UStaticMeshComponent and a SetMaterial1, SetMaterial2 functions(BlueprintCallable for now).

Then created BP_MyWidget blueprint class based on MyUserWidget where I created buttons. And in event graph -> OnClicked(Button1) -> GetAllActorsOfClass (MyCube) -> SetMaterial1. Here I'm confused how I should use c++ to get selected actor and call SetMaterial on that Also added the widget to view port in the MainMap level blueprint.* Can I do that in c++?

This works fine. Each button changes the material but if there are several MyCube actors, material changes for all of them. So I want to be able to click on an actor get/save reference -> and then pressing buttons should call SetMaterial only on referenced actor.

For testing, I created blueprint BP_MyCube based on MyCube class and in event graph I can simply OnClicked(StaticMesh) -> SetMaterial1. And it works with clicking on specific actor to call SetMaterial on that actor.

But I need to get/save reference in c++ so I tried to add OnClicked.AddDynamic to my actor class for mouse click events but seems like I cant use AddDynamic in actors?? Please tell me what should I do in what classes. I'm not using any player controller yet.

avatar image MrMcKvarz May 09 '18 at 03:30 PM

Then get a reference to object under the mouse cursor. Do this in a player controller: Trace. In buttons click event get cast the first pc to your own and call BlueprintCallable function(or just bp function) that will actually change material.

pctrace.png (108.5 kB)
avatar image unreal_eng May 15 '18 at 04:44 PM

I'm unable to understand... I can't even get OnClicked working. It works in BP but when I add it to Player controller class or some actor class, it doesn't recognize OnClicked. And that is where I need to pick and save the actor reference. Can you please add some c++ code snippet instead of BP?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Hi, the first thing you need is to get a reference to your actor. In player controller on mouse click call function Get Hit Result Under Cursor By Channel and store found actor. When clicking button get a reference to a player controller, cast it to your pc, get a reference to that actor, get static or skeletal mesh component of that actor and set desirable material.

more ▼

answered May 08 '18 at 03:41 PM

avatar image

241 2 6 15

avatar image unreal_eng May 09 '18 at 10:29 AM

Hi, I updated what I have done. Can you please add some pointers what I should do next? My player controller is currently empty.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1.mouse click ,you can get screen position. 2.use "PlayerController->DeprojectScreenPositionToWorld" can get startposition ,direction,and compute endposition . 3.you can use UKismetSystemLibrary::LineTraceSingle() get actor. 4.UMG Botton call (step.3 get actor) function ,and change material.

more ▼

answered May 09 '18 at 02:06 AM

avatar image

11 1 2

avatar image unreal_eng May 09 '18 at 10:30 AM

Hi, I updated what I have done. Can you please add some pointers what I should do next?

avatar image 正义使者 May 10 '18 at 01:36 AM

You shouldn't use "GetAllActorsOfClass ",because it get all actors,but you only get a specific actor,so you can use "ray detection",such as "LineTraceSingle of c++" or "LineTraceForObjects,LineTraceByChannel of blueprint",return HitResult struct,this struct can get a actor,this actor is pickup actor,then save this actor,this actor can add a function that switch materials....I said a lot of chinese english,sorry...

avatar image unreal_eng May 15 '18 at 04:47 PM

But if I just have a room with objects and no player controller. Isn't it possible to just read mouse click events on actors and get reference?

avatar image 正义使者 Jun 20 '18 at 07:38 AM

If you don't have playercontroller,engine will use a default playercontroller...so you can write "GetPlayerController" node in pawn or actor,then return node can get DeprojectScreenPositionToWorld function

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question