Adding components to ALevelScriptActor

TL;DR:

  1. Given a map, is there a way to add components to its ALevelScriptActor Blueprint (BP)?
  2. Looks like Epic is against adding components to ALevelScriptActors. Why don’t ALevelScriptActor BPs have a “Components” tab? Why is there a “My Blueprint” tab instead?

A more detailed explanation:

I have to add some management logic meant to be accessible to all components of class UMyComponent. Basically, during UMyComponent::BeginPlay(), instances need to notify the management logic, which will cache them for later on.

Now, the most simple solution to this would be to create a dedicated UWorldSubsystem: subsystems are easy to retrieve, always available, etc etc.
Thing is, UWorldSubsystems aren’t necessarily destroyed when the ULevel is, right? I mean, a UWorld can be made of multiple levels AFAIK, and I’d like my cache of UMyComponents to be cleaned up automatically every time the level is destroyed. Besides, I am trying to move away from singleton-like classes as much as I can, so if I could use something the engine already provides, like the ALevelScriptActor, and avoid adding another subsystem to my game, that would be great!

Therefore, I thought:

“This caching logic could be inside a new UCachingComponent on the ALevelScriptActor instead! The ULevel is easy to retrieve from UMyComponent, and once I have it it’s just a matter of calling GetLevelScriptActor() and FindComponentByClass<UCachingComponent>. This would spare me a new subsystem while making sure the cache is cleared when the level is destroyed!”.

I was really excited with the idea but, when I went to my map’s level BP, I found out it doesn’t have a “Components” tab on the top left like normal actors do. Instead, it has a “My Blueprint” tab, without a way to add components to it. Hence the two questions I left on top.

To be crystal clear, here is what the top left corner of my ALevelScriptActor looks like in Unreal 5.3:

Here is what it looks like for a normal AActor:
image

Hello @Luca_Pedrelli

Did you simply create a LevelScriptActor without changing anything in it?

I just created this object and I see a tab with components.

Hi @Neot111, thanks for replying! :grin:

I didn’t create the BP using the “standard” procedure (e.g. Right Click > Blueprint Class), as I thought the ALevelScriptActor BP would be something Unreal automatically provides with the map. For example, here is how I access the level BP for my “TestMap_Bugfixing” map:

, which opens:

Isn’t this the correct procedure to edit the map’s logic? Can I assign a level BP created in the “standard” way to my map?

Hey @Luca_Pedrelli :wink:

After experimenting a bit, I didn’t find any practical use for a LevelScriptActor created through the content browser.

I also don’t quite understand how you can use components created in a LevelScriptActor since they aren’t initialized anywhere and won’t appear on the level, right?

Maybe it would be better for you to create components inside other actors, where they should be, and create an array in LevelScriptActor to store references to these variables, so you can quickly access your components from LevelScriptActor.

Or did I misunderstand something?

After experimenting a bit, I didn’t find any practical use for a LevelScriptActor created through the content browser.

Yeah right? You’ll need the one from the dropdown menu I showed, I think it’s the only way.

I also don’t quite understand how you can use components created in a LevelScriptActor since they aren’t initialized anywhere and won’t appear on the level, right?

Not sure I follow… every AActro initializes its components and keeps them alive, right? So, if I could add components to my ALevelScriptActor as I do with any other actor, I could access them via FindComponentByClass(). My UCachingComponent needs to be a UActorComponent, and UActorComponents don’t have a visual representation… they don’t need to visually “appear” on the level nor have a world transform, so that’s not an issue.

Maybe it would be better for you to create components inside other actors, where they should be, and create an array in LevelScriptActor to store references to these variables, so you can quickly access your components from LevelScriptActor .

That would work, but I still don’t see why components can’t be added via the “Components” tab for the map’s ALevelScriptActor. Sure, this AActor can’t support USceneComponents, for the reason you mentioned, but from this to forbidding adding any kind of component… I don’t know, this looks excessive to me.

EDIT:

No, nevermind, that can’t work because UMyComponents need to tell the cache about themselves at BeginPlay(), but there are no guarantees their AActors are put into play after the AActor holding the UCachingComponent. Only if this component is on the level I can guarantee it’s accessible at BeginPlay() already.