What is the best way to reference an asset inside content in c++? I need to reference a blueprint widget to instantiate it inside my c++ code. Actual I’m using a “FSoftClassPath” but I would like to remove the path from the c++ code.
Well, you can have a C++ class and then derive it with a blueprint. This can be an actor, widget or plain UObject or whatever. Usually, this will be the class that creates the widget in question.
In the C++ header, you would have a member like this:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<UUserWidget> MyWidgetClass;
Then when you open the blueprint that derives this C++ class, you would click on “class defaults” at the top. This will open up the details panel and there you can set the Widget class to your widget. This class type will be then be available in C++ so that you can create it in C++ if you so desire.
You may replace UUserWidget type with a C++ widget type if you have one for the widget you are creating. This is optional.
Another option are classes derived from UDeveloperSettings. When you make one of those classes you can access the properties through the Project Settings instead of through an asset. You would still give the class UPROPERTYs of any of the TSoft* kind and you get the same benefits of picking the classes with the Editor UI.
But this is only an option for asset references that are meant to be used globally in some way.
Thanks for the answer! I think I will go with the Project Settings way but I sill have a question. Does it crash if I access project settings from in-game build?
Nope!