Pawn MeshComponent C++

Hello everyone! Please tell me why in the child Mesh component of My Pawn I can enable or disable СastShadow via Details Tab, but I can’t do it via C++ code?

USkeletalMeshComponent* PlayerMesh = GetMesh();
PlayerMesh->GetChildComponent(i)->???

Maybe I’m calling the function incorrectly?

PlayerMesh->CastShadow - However, it works correctly… But the child components are not working correctly.

GetMesh()->CastShadow = false; is correct.

If you add this to the constructor (CDO), notice that once you go into your blueprint later on and go into the mesh component the cast shadow default behavior is set to false. If the bp has it true there will be a little revert to default arrow next to it that will set it to false.

Apparently, I explained it wrong. I have a player whose every body part is a separate mesh. I need to disable cast shadows not for the parent mesh, but only for one of child mesh. And it needs to be done not via a blueprint, but via C++.

Yes, it works correctly GetMesh()->CastShadow = false; is correct.
But it works for parent mesh only.

	TArray<UActorComponent*> meshes = GetComponentsByClass(UMeshComponent::StaticClass());
	for (int32 i = 0; i < meshes.Num(); i++) {
		if (UMeshComponent* m = Cast<UMeshComponent>(meshes[i])) {

			if (m != GetMesh()) {
				m->CastShadow = false;				
				m->MarkRenderStateDirty();
			}
		}
	}

This will turn off shadows for all meshes except the getMesh one (comment out the m!=GetMesh() if you want that to be taken into account too)

for a specific one you would need to see if it has for instance a Tag set

	if(m->ComponentHasTag("shoulder")) {
				m->CastShadow = false;
				m->MarkRenderStateDirty();
			}

Or grab just components with Tags

TArray<UActorComponent*> meshes = GetComponentsByTag(UMeshComponent::StaticClass(), "Shoulder");

Then litterate over them and turn off shadow casting with marking the render state as dirty.

1 Like

Thank you!! Its really good!

Then mark it as resolved, and have a nice day :slight_smile:


hm… where is Solved button? :=D

Did you start the thread as a question? I don’t see a question mark icon next to the thread so it might not be.

Its my mistake… Sorry

No problem. Just make a question next time.