Change the material color of a mesh of an actor via c++ code ?

hello, i want to change material properties like color of a mesh on an actor via c++ code . basically, if he gets hit or damaged or whatever, change color so its visible.

from what ive read i understand that in order to change any material properties i have to access the mesh of the actor. Now i tried it with blueprints, so i made the class and then added a static mesh as a shape in the editor. But i couldnt find a way to access the mesh at the actor in code …maybe i have overseen something ?.

anyway, i searched, and it seems that in order to do that i have to actually put a property of the mesh component in the class (if i read RAMAs post correct)

thus:

.h:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = StaticMeshComponents)
		TSubobjectPtr<UStaticMeshComponent> AwesomeSword;

.cpp:

.cpp:
AwesomeSword = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("PickupMesh"));

But how can i change the color now ? i am looking for something like GetMaterial()->setMaterialColor(), that changes the color of the material which is attached to the mesh.

Okay i tried around and i got it to work. First i declare it in the header file, and then i can use it in the constructor. it assigns a cube shape, and in the blueprinted class i can also edit it etc.

.H

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = StaticMeshComponents) TSubobjectPtr schwein;

.CPP

    schwein = PCIP.CreateDefaultSubobject<UStaticMeshComponent> (this, TEXT("schwein"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));

	static ConstructorHelpers::FObjectFinder <UMaterialInterface>Material_Blue(TEXT("Material'/Game/Materials/M_Basic_Wall_red.M_Basic_Wall_red'"));

	schwein->SetStaticMesh(StaticMesh.Object);
	schwein->AttachParent = RootComponent;
	schwein->SetMaterial(0, Material_Blue.Object);

This way i create “schwein”, and i can also change it later in the blueprint. Maybe this helps someone with the same question !

this script is from my post a few hours ago, lol

yes so what ? if you refer to me calling the same functions yes.

with the difference that your codesnippet was broken/incomplete,and since you didnt update your answer with a working version i posted the fixed code here so someone else has a working/complete piece of code.

It wasn’t broken or incomplete, the code you posted is “Exactly” the code I posted. I didn’t update my answer with a working version because that IS the working version. Anyway, I don’t want an argument, next time at least credit where you got an answer from.

you claim you dont want an argument, yet all you do is complain about not getting credit for posting some code snippet that you probably got half from other peoples tutorials . of course i got it from somewhere else. literally anything is, if i am using a framework and not exactly invent some new algorithm etc.

what you do is not really magic, as you are just calling functions that can be found in the reference, i looked up those functions and while i fixed your snippet i saved myself the typing. big deal. i dont even see whats the big deal, as i never claimed to have reinvented the whole engine or something, i simply posted working code that does exactly what i want, so other people can use it.

did you even try compiling your code ?

static ConstructorHelpers::FObjectFinder StaticMesh(TEXT(“StaticMesh’/Game/Shapes/Shape_Cube.Shape_Cube’”));

does not compile at least not for me.

by the same logic you are using, i could tell you that you didnt mention that the idea for setting up the

UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = “StaticMesh Components”)
TSubobjectPtr Block;

was posted by Rama as he showed it in another answer.

and by the way: you should post those comments as comments, i was told posting it as answers makes it unorganized.

the fact that you are trying to call a template function without specializing it ?
by the way: no need to be insulting.

Yes It was a copy paste error. I deleted the post yes.
You writing a novel there bro?
How am I being insulting? I’m done talking to you anyway.

then better stop posting here and fix the code you posted :wink:

What, I deleted the post because I saw that I forgot the template class, no need to lie. I did not call you stupid! Wow.

Haha, you editing all your posts and down voting me. haha. I’m done.

you said “being stupid” in your now deleted comment. if youre done :stop trolling with nonsense in this thread and deleting your old comments, and go back to posting broken codesnippets as “answers” XD

Hello, I am locking further comments on this for obvious reasons. Please refrain from discussions (and especially arguments) on AnswerHub posts.

Please also be aware that anything shared on the AnswerHub, Forums, Wiki, or GitHub become freely available for the use of the community and by Epic Games.

Have a good day.