Where to place Global Actors and Components?

Hi,

I already have a custom Game Instance. I’d like to have a custom UWorld, but UE4 doesn’t allow that.

I have components such a Post Process Component that I want them to be always created and assigned to the world so other actors can use them.

UMyWorld* MyWorld = Cast<UMyWorld>(GetWorld());
 MyWorld->ApplyPostProcess();

I think You should stick with ULevel (persistent one):

The level object. Contains the level’s actor list, BSP information, and brush list. Every Level has a World as its Outer and can be used as the PersistentLevel, however, when a Level has been streamed in the OwningWorld represents the World that it is a part of. A Level is a collection of Actors (lights, volumes, mesh instances etc.). Multiple Levels can be loaded and unloaded into the World to create a streaming experience.

The UWorld:

The World is the top level object representing a map or a sandbox in which Actors and Components will exist and be rendered.

A World can be a single Persistent Level with an optional list of streaming levels that are loaded and unloaded via volumes and blueprint functions or it can be a collection of levels organized with a World Composition.

In a standalone game, generally only a single World exists except during seamless area transitions when both a destination and current world exists. In the editor many Worlds exist: The level being edited, each PIE instance, each editor tool which has an interactive rendered viewport, and many more.

I have a feeling you forgot about pointers. I don’t remember how cast works (since I would dynamic_cast in this case), but the fact you are trying basically to copy whole UWorld object instead just to cast it’s pointer is astounding. Long story short, you should have:

UMyWorld *MyWorld = dynamic_cast<UMyWorld*>(GetWorld());

Yep, edited

Your solution is perfect, but how can I implement it? as far as I know, there’s no way to configure a custom ULevel or UWorld. It’s only possible with ALevelScript.

I’ currently using a custom ALevelScriptActor similar to Rama’s implementation of The Solus Level (here). But if I add this to LevelScript it creates N components, one per ALevelScript.

No, the way he is using Cast is correct. This is how you are supposed to use cast from one pointer type to another with UObjects (it’s basically a dynamic_cast but with some additional support for the UObject type I believe).

Why not? ULevel is just a level. But afaik it’s not easy to make new UWorld. But You could configure one already exist.

Also, there’s a default ULevel in each UWorld.

ALevelScriptActor is an actor that represents level - the only way to store things in Levels w/o making new actors.

Roam the API, there are many useful functions. Some may be vague on description.

I didn’t know that I can have a ULevel alongside ALevelScript. Thanks I’ll give it a try.

I just tried what you suggested. I get a compilation error every time I try to create a class that extends from ULevel.

Have you ever done this?

What did you mean by “ULevel is just a Level”? it looks like UWorld, which is something you can’t extend (unless you create your own Engine too).

The only solution that I can come up with is having two separete ULevelScriptActor, one for the persistent Level and another one for the Streaming ones and I don’t think that is an elegant solution.

I only want a class that I can extend and actors can easily reference and it is loaded once per World.

This is the error that I get when I extend from ULevel:
CompilerResultsLog:Error: Error MyLevel.cpp.obj : error LNK2019: unresolved external symbol “public: __cdecl ULevel::ULevel(class FObjectInitializer const &)” (??0ULevel@@QEAA@AEBVFObjectInitializer@@@Z) referenced in function “public: __cdecl UMyLevel::UMyLevel(class FObjectInitializer const &)” (??0UMyLevel@@QEAA@AEBVFObjectInitializer@@@Z)
CompilerResultsLog:Error: Error LevelTest.generated.cpp.obj : error LNK2001: unresolved external symbol “public: __cdecl ULevel::ULevel(class FObjectInitializer const &)” (??0ULevel@@QEAA@AEBVFObjectInitializer@@@Z)
CompilerResultsLog:Error: Error MyLevel.cpp.obj : error LNK2019: unresolved external symbol “public: virtual __cdecl ULevel::~ULevel(void)” (??1ULevel@@UEAA@XZ) referenced in function “public: virtual __cdecl UMyLevel::~UMyLevel(void)” (??1UMyLevel@@UEAA@XZ)