Download

Referencing one of multiple Blueprints in c++.

Typically, whenever I need to reference a blueprint in code, I’ll use the following code snippet in my constructor:



static ConstructorHelpers::FObjectFinder<UBlueprint> BlueprintItem(TEXT("Blueprint'/Game/Item/Item.Item'"));
if(BlueprintItem.Object)
      Blueprint = (UClass*)BlueprintItem.Objec->GeneratedClass;


However, this is limited to the single blueprint. I currently have multiple blueprint instances of this class meaning that having a hardcoded single blueprint will not work. Is it possible to differentiate or potentially assign the BlueprintItem based off of the UStaticMeshComponent that it contains instead? My initial thoughts were to pass it and FString of the concatenated blueprint name in the constructor, but it won’t take strings.

Your code sample isn’t referencing a blueprint, it’s referencing a class. Whether you have one, ten or one million instances of that class isn’t going to change the behaviour of class instances.

Getting the instances is going to be the same as any other UObject. I’m gleaning from your question that you want to obtain the StaticMeshComponent contained in your blueprint?

The easiest solution here would be to create a native base class for your blueprint with a TSubobjectPtr<UStaticMeshComponent>, and then reparent you blueprint class to this new native base class. Then you can just reference your blueprint instances in native code as you would any other object and do logic on it.

Barring that, you can still obtain your static mesh component given only an Actor reference. Use MyActor->FindComponentByType<UStaticMeshComponent>() if you only have one StaticMeshComponent.

Without knowing where this would be used, I can’t really recommend a particular means of accessing those instances in order to call these functions. But the jist of it is, you could add a property:


UPROPERTY( Category=Items, EditAnywhere, BlueprintReadWrite )
TArray<AActor*> ItemBlueprints;

This will expose an editable array in which you can set your blueprint instances in order to access them in native code. But this will take any actor reference, whereas if you create a native base class, you could instead make TArray<AMyItemBase*> which will restrict the type that can be stored in this array.