Hi I’m working on this Tetris like puzzle game where a user can select blocks to drop into a scene. It’s been going fairly well so far except I think am having trouble with some basic aspects of how actors work.
My assumption was that I could feed the FObjectFinder a randomly determined resource path on initialization and every time my actor class is spawned I would get a different static mesh… However it seems that is not the case. Using the following code I am able to spawn a random mesh, but it is only random on each launch of the editor and not each time I spawn the class using
ARocks12Block* NewRock = GetWorld()->SpawnActor(RockLocation, FRotator(0,0,0));
// Create dummy root scene component DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0")); RootComponent = DummyRoot; // Create static mesh component BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BaseMesh0")); int32 randomItem = FMath::FRandRange(0, 11); static const TCHAR* mesh = *meshes[randomItem]; static const TCHAR* material = *materials[randomItem]; ConstructorHelpers::FObjectFinderOptional<UStaticMesh> RockMesh = mesh; ConstructorHelpers::FObjectFinderOptional<UMaterial> RockMaterial = material; static int32 RockCounter = 0; FString RockName = FString::Printf(TEXT("NewMesh_%d"), RockCounter++); newMesh = NewObject<UStaticMesh>(this, UStaticMesh::StaticClass(), *RockName); static int32 RockMatCounter = 0; FString RockMatName = FString::Printf(TEXT("NewMat_%d"), RockMatCounter++); newMaterial = NewObject<UMaterial>(this, UMaterial::StaticClass(), *RockMatName); newMesh = RockMesh.Get(); newMaterial = RockMaterial.Get(); BaseMesh->SetStaticMesh(newMesh); BaseMesh->SetRelativeScale3D(FVector(1.f,1.f,0.25f)); BaseMesh->SetRelativeLocation(FVector(0.f,0.f,25.f)); BaseMesh->SetMaterial(0, newMaterial); BaseMesh->SetSimulatePhysics(true); BaseMesh->WakeRigidBody(); BaseMesh->AttachToComponent(DummyRoot, FAttachmentTransformRules::KeepWorldTransform);
My working assumption is that stuff in the class constructor only gets loaded once when the game is launched, but if this is the case how do I achieve my goal of selecting a different mesh for each actor spawn.
My other thought was to create a blueprint class with an exposed static mesh property, add as many as I need to the level while setting static mesh for each, but I wasn’t sure how I would then use these to spawn new instances.
Any suggestions would be MUCH appreciated as I am a little lost!