How to change child actor component paramters with C++?

I’m new to Unreal c++ coding, I have got a quetion about a match 3 game I’m working on with UE5, that I’m not familiar with how to resolve.

I got a C++ Tile base class, and A Tile_BP object inherits it.
The Tile_BP contains another Ball_BP component that has a lot of parameters, like some specific Materials slots, and some color variable slots.

Now I’m coding in the C++ Tile base class, that need to change the Material and color variable in the Ball_BP.

What I am doing is like, in Tile.cpp, the actual question in comments,

void ATile::SetTileMaterial_Implementation(class UMaterialInterface* MaterialToUse)
{
        // I want to access the Ball_BP
	UChildActorComponent* BallBP = Cast<UChildActorComponent>(GetDefaultSubobjectByName(TEXT("BP_Ball_solid_5pieces")));
	
        // Tile_BP is derived from Tile.cpp base class
	ConstructorHelpers::FClassFinder<ATile>
		BPClassFinder(TEXT("/Game/BP/Tile_BP"));

	TSubclassOf<ATile> Tile_BP = BPClassFinder.Class;

        //try to cast the BallBP into Tile_BP component, so that maybe I can access variables there, but this wouldn't compile
	Tile_BP* selectedBall = Cast<Tile_BP>(BallBP);

       // and how to access and change the Material and color variable here?

}

After some research, I ended up with following, it compiles, but crash my editor.

UChildActorComponent* BallBP = Cast<UChildActorComponent>(GetDefaultSubobjectByName(TEXT("BP_Ball_solid_5pieces")));
	
	FStructProperty* ColorProp = FindFProperty<FStructProperty>(BallBP->GetClass(), "SurfaceColor");


	//https://forums.unrealengine.com/t/how-to-get-color-property-by-name/389184/3
	// 
	if (ColorProp) 
	{
		FVector* v = ColorProp->ContainerPtrToValuePtr<FVector>(BallBP);
		v->X = 0.9;
		v->Y = 0.0;
		v->Z = 0.0;

		UE_LOG(LogTemp, Warning, TEXT("SurfaceColor Property result: %s"), *(v->ToString()));
	}

You need Child Actor, not ChildActorComponent.
Tile_BP* selectedBall = Cast<Tile_BP>(BallBP->GetChildActor());

2 Likes

Thanks so much, Do you mean the BP inside other BP is a Child Actor? We cannot access parameters with ChildActorComponent?

I tried to use FProperty to get and set parameters in the question text just updated above.

As far as I know you can’t cast to a blueprint class in C++, you need to implement the variables you need in the parent C++ class and then you’ll be able to interact with them

2 Likes

Yes, UChildActorComponent derives from USceneComponent, and it can’t be cast to AActor or to anything derived from AActor, including your ball class. But GetChildActor() returns the actor that you set to ChildActorComponent as ChildActorClass.

P.S. Plus what @LInchIo said: if your ball exists only as a blueprint class, you won’t be able to access its custom functions and variables in C++. If it’s a C++ class with a blueprint created as its child class, you’re good to go.

2 Likes

Thanks, after learning Child Actor is not dynamic from this live stream tutorial, Child Actor Templates | Feature Highlight | Unreal Engine - YouTube, it looks like what I was doing is wrong. The Child Actor Template parameters can only be changed on spawn, impossible to change after, is it still like so in UE5?

So I should make my custom tile class flat, use the original BP_Ball_solid_5pieces as a derived class from Tile base class, rather than make it a Child Actor.