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.

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

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 :wink:

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.

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).

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?

In this post i helped someone else create Widget elements in C++ that can be manipulated in Blueprint: How can I handle Blueprint-Widgets in C++ - UI - Unreal Engine Forums

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:

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 :wink:

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

Thanks, I set to answered.