x

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"

Changing actor mesh material with UMG button click c++

I'm new to UE4 and find most of the help related to blueprints. I simply created a UI Widget Blueprint with one button and in Event Graph: Event BeginPlay->Create UserWidget->Add to Viewport. Then created an actor C++ class with a StaticMeshComponent. In constructor, finding mesh and material in contents folder and setting mesh component. Now material is found and saved, I have SetMaterial function which I want to be called using user widget button click. alt text How to call a c++ actor function from UMG button? Should it be called from user widget event graph OnClick or from elsewhere? I couldn't figure out the casting and references.

Please give some pointers or material to study for this simple task and also the same task with drag n drop i.e. drag button material on a mesh to change material. Thanks

Product Version: UE 4.19
Tags:
ccode.png (211.6 kB)
more ▼

asked May 03 '18 at 08:39 PM in C++ Programming

avatar image

unreal_eng
15 1 2 8

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

1 answer: sort voted first

In order for a C++ function to be called in Blueprint you need to put this macro just before declaring the function in your .h file:

 UPROPERTY(BlueprintCallable)
     void SetMaterial1();

If you do this, your function will be callable within blueprint.

Then, all you have to do is, in your OnClick event, use GetPlayerCharacter, then cast to your AMesh character (if you made a blueprint class of your AMesh character, use that instead), and simply call the SetMaterial1() function from there ;)

more ▼

answered May 03 '18 at 08:57 PM

avatar image

goncasmage
897 12 14 25

avatar image unreal_eng May 03 '18 at 10:37 PM

I was actually using BlueprintCallable but still there is missing reference to my actor class in userwidget blueprint event graph. I dont have any player controller or nothing else, do I need to create one? I got only one actor class with static mesh and one User Widget blueprint class. alt text

widgetblueprint.png (160.7 kB)
avatar image goncasmage May 04 '18 at 07:48 AM

In order to do that cast, you need to somehow get a reference to the actor. If the actor isn't a Player Character, just do a GetAllActorsOfClass and feed the AMesh class, keep the first result (assuming you have only one object of that class in your level) and then use that reference (if you do it this way you won't need the cast).

avatar image unreal_eng May 05 '18 at 08:03 AM

Thanks. It worked. Is it possible to do that all in code and only call setmaterial function with onclick? For example if I create custom user widget class with setmaterial function and set it parent to my UI widget blueprint, then I can call setmaterial directly, right? But how do I get reference of actor class in userwidget class to call its public functions?

avatar image goncasmage May 05 '18 at 08:13 AM

In this post i helped someone else create Widget elements in C++ that can be manipulated in Blueprint: https://answers.unrealengine.com/questions/786387/how-can-i-handle-blueprint-widgets-in-c.html?childToView=786400#answer-786400

In order to get the reference to the actor, assuming it's not a Character, i think the best way is to use this node:

alt text

Except, you have to feed your AMesh class to the node instead of the default Actor class. Also, if you have more than one AMesh object in your level, you have to keep the first few references (depending on how many you have) instead of just the first one ;)

Also, if you're satisfied with the answer, make sure to mark it as correct :) Thanks!

help.png (42.7 kB)
avatar image unreal_eng May 07 '18 at 02:22 PM

Thanks, I set to answered.

(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