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()));
}