This is sort of a general question actually, I want to know how to edit an Actor’s components in C++. I tried
FindComponentByClass() and I was able to get the mesh but not the material.
I have been looking for examples for this for a couple of days and I still couldn’t manage. I imagine this is something very simple but I just can’t find what methods I need to use and how.
Can someone give me an example of how they’d change the base color of an Actor? I managed to find the owner of the code (is my terminology correct?) and I found its mesh but then I got lost.
You need to use the CreateAndSetMaterialInstanceDynamic -function.
If you download the ShooterGame example, it has a fully functioning system for the colour change in characters. Check out the ShooterCharacter class and in there UpdateTeamColors -function.
Also check this thread, it has a simplified example of the use of the function.
That is specific to the example, are there not more general ways to do this? Basically if I want to edit the Transform or Physics I need to find specific CreateAndSet… functions for all of them?
Sorry, I was a bit hasty reading your question.
Each type of component generally has its own set of functions that you can use to modify it in C++. In this specific instance we are modifying a skeletal mesh’s subcomponent’s material, so the application is indeed very specific. For everything you do in UE4 with C++, the best place to check how things are done is the API - it lists all the relevant functions for your specific type of component.
Say you want to manipulate the transform of a component that’s part of your actor. In your actor’s C++ class you need to declare all the components that are part of it if you want to easily manipulate them. Once you have declared them in your .h, you know what the variable is called and most importantly what class it is. Then you can start applying transform changes by using functions for that class. All actor components that have a transform are either USceneComponents or one of its subclasses. There are a lot of classes derived from that, but you would basically be using this class’s functions to manipulate your actor’s component. If you check the API here there is a function called AddLocalTransform. This would add a transform to the component relative to your actor - ie. it would still move with your character but would be either rotated, scaled or positioned a bit differently.
USceneComponent API is where I usually start if I need to do something to a component in C++. Remember, that if you want to manipulate the component, it is wiser to declare it in C++ and not add it in the editor as a subcomponent. While this is entirely possible to do, it’s needlessly complicated and like you said, you need to use FindComponentByClass() functions and whatnot. Even if you don’t apply an actual mesh as the subcomponent in the C++, you want to declare it.
Thanks, this was useful, I’ll get on with my trials and errors now.