May I ask what you mean by static context?
I have a case where I in the BeginPlay function of my override of the GameMode class call NewObject on my UObjectSpawner class.
void ABlubberGameModeBase::BeginPlay()
{
Super::BeginPlay();
PrivInitObjectSpawner();
}
void ABlubberGameModeBase::PrivInitObjectSpawner()
{
if (!ObjectSpawnerBlueprint)
{
UE_LOG(LogTemp, Warning, TEXT(“No Object Spawner Blueprint”));
return;
}
myObjectSpawner = NewObject<UObjectSpawner>(ObjectSpawnerBlueprint);
myObjectSpawner->Init(GetWorld());
}
But this results in an instance of the ObjectSpawner that does not get its blueprint for the AFish class set, even though I have specified it in the editor.
The is the header for the game mode:
#pragma once
#include “CoreMinimal.h”
#include “GameFramework/GameModeBase.h”
#include “BlubberGameModeBase.generated.h”
class UObjectSpawner;
class AOcean;
class AOceanFloor;
/**
*
*/
UCLASS()
class BLUBBER_API ABlubberGameModeBase : public AGameModeBase
{
GENERATED_BODY()
public:
ABlubberGameModeBase();
UPROPERTY(EditDefaultsOnly)
TSubclassOf<UObjectSpawner> ObjectSpawnerBlueprint;
static const int ObjectsYValue = 1;
private:
void PrivInitObjectSpawner();
void BeginPlay() override;
UPROPERTY()
UObjectSpawner* myObjectSpawner;
};
and the Object Spawner class:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “UObject/NoExportTypes.h”
#include “ObjectSpawner.generated.h”
class AFish;
class ABlubberGameModeBase;
class UWorld;
/**
*
*/
UCLASS(Blueprintable)
class BLUBBER_API UObjectSpawner : public UObject, public FTickableGameObject
{
GENERATED_BODY()
public:
UObjectSpawner();
void UpdateSpawningFish();
void SpawnFish();
UPROPERTY(EditDefaultsOnly)
TSubclassOf<AFish> myFishBlueprint;
UPROPERTY(EditDefaultsOnly)
float mySpawnDistance;
UPROPERTY(EditDefaultsOnly)
float myFishSpawnCooldown;
bool IsTickable() const override { return bCanTick; }
bool IsTickableInEditor() const override { return false; }
bool IsTickableWhenPaused() const override { return false; }
TStatId GetStatId() const override { return TStatId(); }
void Tick(float DeltaTime) override;
void Init(UWorld* aWorld);
UWorld* GetWorld() const override;
private:
FTimerHandle mySpawnFishTimer;
UWorld* myWorld;
bool bCanTick;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include “ObjectSpawner.h”
#include “Kismet/GameplayStatics.h”
#include “Kismet/KismetMathLibrary.h”
#include “Fish.h”
#include “BlubberGameModeBase.h”
#include “Walrus.h”
#include “Engine/Engine.h”
UObjectSpawner::UObjectSpawner()
{
mySpawnDistance = 1000.f;
myFishSpawnCooldown = 1.f;
bCanTick = true;
}
void UObjectSpawner::Init(UWorld* aWorld)
{
myWorld = aWorld;
bCanTick = true;
}
void UObjectSpawner::Tick(float DeltaTime)
{
UpdateSpawningFish();
}
void UObjectSpawner::UpdateSpawningFish()
{
UWorld* world = GetWorld();
if (!world)
return;
ACharacter* player = UGameplayStatics::GetPlayerCharacter(world, 0);
if (!player)
return;
FTimerManager& timerManager = world->GetTimerManager();
if(!timerManager.IsTimerActive(mySpawnFishTimer))
timerManager.SetTimer(mySpawnFishTimer, this, &UObjectSpawner::SpawnFish, myFishSpawnCooldown);
}
void UObjectSpawner::SpawnFish()
{
if (!myFishBlueprint)
{
UE_LOG(LogTemp, Warning, TEXT(“No Object Spawner Blueprint”));
return;
}
UWorld* world = GetWorld();
if (!world)
return;
ACharacter* player = UGameplayStatics::GetPlayerCharacter(world, 0);
if (!player)
return;
AWalrus* walrus = Cast<AWalrus>(player);
if(!walrus)
return;
FVector location;
location.Y = 0;
location.Z = 0;
location.X = walrus->GetActorLocation().X + mySpawnDistance;
FActorSpawnParameters SpawnParams;
AFish* fishSpawned = world->SpawnActor<AFish>(myFishBlueprint, location, FRotator(0.f), SpawnParams);
}
UWorld* UObjectSpawner::GetWorld() const
{
return Super::GetWorld();//myWorld;
}
myFishBlueprint on the newly created instance is nullptr, despite it being set on the default instance.