Hello Forum,
Having finished Battle Tank, I’ve now just begun some practice in the form of recreating Space Invaders in Unreal using C++. I’ve now come across my first obstacle.
I’m trying to recreate the effect where the aliens move every second or so and also change shape. To accomplish this, I’m trying to set the static mesh of the actor every second. However, while the following code compiles, it crashes the editor on play and the debugger points to the ConstructorHelper lines as the trouble-makers.
I’ve read online that you must use FObjectFinder in the constructor of your class but when I paste those lines there, SetStaticMesh() becomes unaware of “StaticBugOpen” and “StaticBugClosed”.
Does anyone out there know how I might fix this code or know of a better way to accomplish this effect?
void AAlien::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MoveAlien();
}
void AAlien::MoveAlien()
{
if(GetWorld()->GetTimeSeconds() >= TimeOfLastMove + MoveDelay)
{
FVector AlienLocation = GetActorLocation();
AlienLocation.X += 20.0f;
SetActorLocation(AlienLocation, false);static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticBugClosed(TEXT("StaticMesh'/Game/Aliens/SpaceInvaderSprites01_BugInvaderClosed.SpaceInvaderSprites01_BugInvaderClosed'")); static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticBugOpen(TEXT("StaticMesh'/Game/Aliens/SpaceInvaderSprites01_BugInvaderOpen.SpaceInvaderSprites01_BugInvaderOpen'")); if(bIsAlienOpen == true) { UStaticMeshComponent().SetStaticMesh(StaticBugClosed.Object); bIsAlienOpen = false; } if(bIsAlienOpen == false) { UStaticMeshComponent().SetStaticMesh(StaticBugOpen.Object); bIsAlienOpen = true; } TimeOfLastMove = GetWorld()->GetTimeSeconds(); }
}
Cheers.