Skip to content

Commit

Permalink
Set Actor Spawn action to use OnPostWorldInitialization delegate to s…
Browse files Browse the repository at this point in the history
…tart feature behavior instead game instance
  • Loading branch information
lucoiso committed Aug 7, 2022
1 parent c41b191 commit 91637b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 7 additions & 6 deletions Source/Private/GameFeatureAction_SpawnActors.cpp
Expand Up @@ -12,13 +12,14 @@ void UGameFeatureAction_SpawnActors::OnGameFeatureActivating(FGameFeatureActivat
ResetExtension();
}

Super::OnGameFeatureActivating(Context);
WorldInitializedHandle =
FWorldDelegates::OnPostWorldInitialization.
AddUObject(this, &UGameFeatureAction_SpawnActors::OnWorldInitialized);
}

void UGameFeatureAction_SpawnActors::OnGameFeatureDeactivating(FGameFeatureDeactivatingContext& Context)
{
Super::OnGameFeatureDeactivating(Context);

FWorldDelegates::OnStartGameInstance.Remove(WorldInitializedHandle);
ResetExtension();
}

Expand All @@ -27,12 +28,12 @@ void UGameFeatureAction_SpawnActors::ResetExtension()
DestroyActors();
}

void UGameFeatureAction_SpawnActors::AddToWorld(const FWorldContext& WorldContext)
void UGameFeatureAction_SpawnActors::OnWorldInitialized(UWorld* World,
[[maybe_unused]] const UWorld::InitializationValues)
{
if (!TargetLevel.IsNull())
{
if (UWorld* World = WorldContext.World();
World->IsGameWorld()
if (World->IsGameWorld()
&& World->GetNetMode() != NM_Client
&& World->GetName() == TargetLevel.LoadSynchronous()->GetName())
{
Expand Down
6 changes: 4 additions & 2 deletions Source/Public/GameFeatureAction_SpawnActors.h
Expand Up @@ -30,7 +30,7 @@ struct FActorSpawnSettings
*
*/
UCLASS(MinimalAPI, meta = (DisplayName = "MF Extra Actions: Spawn Actors"))
class UGameFeatureAction_SpawnActors final : public UGameFeatureAction_WorldActionBase
class UGameFeatureAction_SpawnActors final : public UGameFeatureAction
{
GENERATED_BODY()

Expand All @@ -46,7 +46,8 @@ class UGameFeatureAction_SpawnActors final : public UGameFeatureAction_WorldActi
protected:
virtual void OnGameFeatureActivating(FGameFeatureActivatingContext& Context) override;
virtual void OnGameFeatureDeactivating(FGameFeatureDeactivatingContext& Context) override;
virtual void AddToWorld(const FWorldContext& WorldContext) override;

void OnWorldInitialized(UWorld* World, const UWorld::InitializationValues InitializationValues);

private:
void SpawnActors(UWorld* WorldReference);
Expand All @@ -55,4 +56,5 @@ class UGameFeatureAction_SpawnActors final : public UGameFeatureAction_WorldActi
void ResetExtension();

TArray<TWeakObjectPtr<AActor>> SpawnedActors;
FDelegateHandle WorldInitializedHandle;
};

0 comments on commit 91637b4

Please sign in to comment.