String to Class?

I’ve used the JSON plugin to save my level and it works very well, though when doing so i’m forced to store a key value integer which represents a class and is then re-translated back when the file is loaded, like this:

Save:
if class = actor1, save class:“0”

Load:
if class = “0”, create class:“actor1”

This works fine, but I’d like to make this more dynamic so that I can create actors without updating this index each time.

I’d like to instead store the class name in the json as plain text, however doing this has proved troublesome. Is there a way to load a class from a string which is the same as the class name? I feel like this should be doable, but I can’t seem to figure it out easily using BPs.

Thoughts?

Anyone know how to do this? It would be very helpful to have a way to do this without maintaining an index.

you could use a struct + data table
with
String Name
Class Class

on loading:
iterate through the data table, if name = class name, create <class>

Edit: after messing around with this for a while, I’ve come to realize it’s the same issue as using ‘GetClass’ from an asset loaded from the asset manager. It returns an object class but I want an actor class and I can’t figure out how to convert them.

After poking around for a while in the asset manager, I found a way to do this in 4.26. Not sure when all the required nodes were added to the engine.

The two critical nodes are ToSoftClassReference (SoftClassPath) (in the category Utilities) and AsyncLoadClassAsset.

Right click the blue SoftClassPath structure node and split it; it becomes a single string. The string to build for the soft path is formed this way:

/Game/[subfolderA]/[subfolderB]/[subfolderX]/[ASSET].[ASSET]_C

where [ASSET] is the display name of the class you want to reference dynamically. You can see a sample of this by hovering an asset in the content browser and looking at the path for its parent class.

The async load class returns an object class, so if you want to spawn an actor from that, you can (hopefully) use one of the workarounds mentioned here.

2 Likes

I managed it this way. The two “Name” variables are serialized into the save game. Note that the classes must be present in the asset manager (in Project Settings), I believe.

2 Likes

I found another way: class :purple_circle: → Resolve Soft Reference → To String (SoftClassReference) → :red_circle: string :red_circle: → Make Soft Class Path → To Soft Class Reference → Resolve Soft Reference → :purple_circle: class