I am trying to spawn in an actor from the gamemode at the very start. When the object spawns in there is no mesh attached even though I set up the mesh in the blueprint class. Thanks in advance.
Here is the header for the gamemode:
#pragma once
#include "CoreMinimal.h"
#include "EngineGlobals.h"
#include "Engine/Engine.h"
#include "GameFramework/GameMode.h"
#include "MyCharacter.h"
#include "Target.h"
#include "MyGameMode.generated.h"
/**
*
*/
UCLASS()
class LEARNING_API AMyGameMode : public AGameMode
{
GENERATED_BODY()
AMyGameMode(const FObjectInitializer& ObjectInitializer);
~AMyGameMode();
virtual void Tick(float DeltaSeconds) override;
AMyCharacter* user;
void StartPlay() override;
//virtual void GameEnding() override;
void CalculateAccuracy();
void FireResult(AMyCharacter* Character);
/*Cannot set ScaleOfTarget to less than 1. Don't change the Z value of the Scale.
*Set TimeAlive to 0 to keep the target*/
ATarget* CreateTarget(FName Name, float TimeAlive, FVector ScaleOfTarget = FVector(1.0f, 1.0f, 1.0f));
FTimerHandle TimerHWD;
float TimeOfRound;
float Accuracy;
float TotalShotsMissed;
float TotalShotsHit;
};
Here is the cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyGameMode.h"
#include "GameFramework/PlayerStart.h"
#include "Kismet/GameplayStatics.h"
#include "TimerManager.h"
AMyGameMode::AMyGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
DefaultPawnClass = AMyCharacter::StaticClass();
TimeOfRound = 10.0f;
TotalShotsMissed = 0.0f;
TotalShotsHit = 0.0f;
Accuracy = 0.0f;
}
AMyGameMode::~AMyGameMode()
{
}
void AMyGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
if (user != nullptr)
{
if (!user->bFireHandled)
{
FireResult(user);
user->HandleFire();
}
}
}
void AMyGameMode::StartPlay()
{
Super::StartPlay();
user = Cast<AMyCharacter>(GetWorld()->GetFirstPlayerController()->GetPawn());
StartMatch();
if (GEngine)
{
ATarget* Target1 = CreateTarget(TEXT("Target"), 0.0f);
GetWorld()->GetTimerManager().SetTimer(TimerHWD,this,&AMyGameMode::CalculateAccuracy,TimeOfRound,false);
}
}
void AMyGameMode::CalculateAccuracy()
{
if (user->ShotsFired == 0)
{
Accuracy = 0.0f;
FString Bop = FString::SanitizeFloat(Accuracy) + '%';
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Blue, Bop);
}
else
{
Accuracy = TotalShotsHit / user->ShotsFired;
Accuracy *= 100;
FString Bop = FString::SanitizeFloat(Accuracy) + '%';
GEngine->AddOnScreenDebugMessage(-1, 20.0f, FColor::Blue,Bop);
}
}
void AMyGameMode::FireResult(AMyCharacter* Character)
{
if (Character->HitResult.Actor != nullptr)
{
FString Act = Character->HitResult.GetActor()->GetName();
if (Act.Contains("Target")) TotalShotsHit += 1.0f;
else TotalShotsMissed++;
}
}
ATarget* AMyGameMode::CreateTarget(FName Name, float TimeAlive, FVector ScaleOfTarget)
{
if (ScaleOfTarget.Z != 1.0f)
ScaleOfTarget.Z = 1.0f;
ATarget* Whoop = nullptr;
FActorSpawnParameters SpawnParam;
SpawnParam.Name = Name;
SpawnParam.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
SpawnParam.Owner = this;
const FRotator Rotation = FRotator(180.0f, 90.0f, 180.0f);
const float XCor = 264.0f;
float YCor = FMath::RandRange(-839.0f, 864.0f);
float ZCor = FMath::RandRange(127.0f, 625.0f);
//FVector Location = FVector(XCor, YCor, ZCor);
FVector Location = FVector(XCor, YCor, ZCor);
Whoop = GetWorld()->SpawnActor<ATarget>(ATarget::StaticClass(),Location, Rotation, SpawnParam);
if (Whoop != nullptr)
{
Whoop->SetActorScale3D(ScaleOfTarget);
Whoop->SetLifeSpan(TimeAlive);
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Whoop Didn't Spawn"));
}
return Whoop;
}
Target cpp:
#include "Target.h"
#include "Engine/Engine.h"
#include "ConstructorHelpers.h"
// Sets default values
ATarget::ATarget()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
Root = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Collision"));
RootComponent = Root;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(RootComponent);
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ATarget::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATarget::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Here is the header:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Target.generated.h"
UCLASS()
class LEARNING_API ATarget : public AActor
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
UStaticMeshComponent* Mesh;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
USphereComponent* Root;
public:
// Sets default values for this actor's properties
ATarget();
protected:
// Called when the game starts or when spawned
void BeginPlay() override;
public:
// Called every frame
void Tick(float DeltaTime) override;
};