I want to load assets (Reference them in my code)…
As an example, I need to set a USoundBase’s value to a default value so, I’ll need to point to an asset in my project’s content. How can I achieve this?
This usually isn’t the best thing to do, but you can create a hardcoded reference like FSoftObjectPath("/Game/Path/To/AssetName.AssetName") and call TryLoad() on it. It’ll load and you can cast the UObject* to the correct type. Loading from disk can take a long time, but this is a cheap call if the asset is loaded already.
Usually though (if this is on a UObject or Actor or in a UStruct) you just want to declare a UPROPERTY pointer with an asset picker UI, which is easy enough:
UPROPERTY(EditAnywhere, Category="MyCategory")
USoundBase* SoundBase;
That’ll give you an asset picker dropdown where you can choose the asset to reference. Then SoundBase will point to that asset.
Hi. Thanks for the reference to FSoftObjectPath. That was exactly what I needed.
Yeah, I don’t want to use UPROPERTY because I’ll have to re-assign a repetitive asset over and over…
Thanks!
“Repetitive picking” sounds like you’re not considering blueprints, and blueprint inheritance, and, most importantly, bulk/matrix editing.
You really should define at least one blueprint class for each of your C++ classes, and use that wherever you need an actual-instance. And, ideally, everywhere you need an actual-instance, make it a TSubclassOf<> UPROPERTY so that you can switch things around in the editor at will.
You don’t need to use the graph scripting at all for Blueprints, just the data editing/inheritance/wiring makes a lot of sense on its own.
Hi and thanks for your concern.
I do that for some specific stuff but in this case, I was just dealing with one component variable that I thought creating a blueprint class would be a waste for this.
Also, it’s understandable that blueprints would make it easier but is it really something that I “should” do? How beneficial can it be from other aspects?
Blueprints let you set default values for properties, and use configurable classes/assets. As long as C++ works for you, sure, you can make do entirely without! But if you find that you need to put in hard-coded asset paths, why not make that asset/class name instead be a configurable value and use a Blueprint? It buys a lot of flexibility, at very little additional cost (either runtime or maintenance.)
Absolutely. Thank you!
I’ll utilize blueprints a bit more from now.
Thank you for your time!