Using Subsystems for a "GridManager" class?

Hey there, I’m new to Unreal and C++ so sorry if my question is unusual or just stupid.
I’m working on a Game which has several Grids (c++). The grid class is a C++ class based on the Actor class.

I just started that project so I thought just to create a Singleton GridManager class which can return the Grid I’m looking for by ID but then I stumbled uppon the Subsystems, I read that It has some advantages compared to Singletons so i wanted to give them a try.
I created a Subsystem based on UGameInstanceSubsystem.

Now I’d like to create instances of my Grid class in my Subsystem::Initialize() but since I cannot create a Blueprint class from my Subsystem I’m unable to assign a Grid class to it to spawn that grid right? My Grid class has several TSubclassOf<> references which need to be assigned.

Is there a way to solve this? Or is a subsystem not meant to do such things and should not / can not such World Object related things?

Thanks a lot in advance!
Cxyda

If you’re interested spawning actors from your subsystem, you probably want a UWorldSubsystem instead. There’s also a ULocalPlayerSubsystem if you need a “singleton” per player.

Another alternative is a component attached to your game mode or game state. It depends on how you need to access it, if at all.

Hey MegForceSeven,
Thanks for the fast answer! Yes indeed a UWorldSubsystem would make sense. I’m not planning to have a multiplayer game so it should not make much of a difference between ULocalPlayerSubsystem and UWorldSubsystem right?

But the main question stays, how would I get the reference to the Actor class (Blueprint) I’d like to spawn via GetWorld()->SpawnActor<T> since I can’t assign it straight from the Unreal Editor right? :thinking:

Yeah, that’s true. It would really only affect where you have to look to get the instance of your subsystem, either getting a world or the local player.

There are a few options that would work for this:

  1. Config - mark your subsystem for Config (in the UCLASS macro) and whatever properties you want with Config and you can set those blueprint references in an ini by path. This is a good option if it’s sort of a one-done-setup and not something that you’ll be modifying a lot.
  2. DeveloperSettings - if you create a derived class, you can do all the configuration for your subsystem through the Project Settings UI in the Editor. You’ve also got to do the config stuff but you apply it to the DeveloperSettings class not your subsystem. That’s how the Settings get saved and persist. But you get the benefit of not having to edit the ini by hand which can be especially hard when referencing assets.
  3. Create a custom data asset type to act as your subsystem configuration. Then you just need a way to find it. This could be using the Config option above or by accessing a value to set in your game mode/game state. Even if you set it with the Config option, you only have to get the config path down once and then you just edit the asset.
  4. Use reflection. This is a bit advanced and is very dependent on your design, but you could do something similar to how the subsystems themselves are created. Instead of manually configuring the classes you spawn, you could use the asset registry to find all the blueprints that derive from a specific actor class, load them and then instantiate them.

There are probably others, but those are the ones I usually reach for depending on requirements of the specific subsystem I’m writing.

1 Like

Awesome ! Thanks a lot ! Great answer!

I think I’ll look into option 2 first :slight_smile: Thanks again for your time and help !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.