spawn user widget in another user widget

Hello,

i just started with unreal engine. i am working with unity and Godot but unreal is something completely new.
i have problem how to save, create instance of user widget blueprint in another widget blueprint.
so my idea was to create perk system so i created one user widget blueprint named WB_Perk where i can set name and description and i also added button event. when i had main blueprint i just duplicated that and changed parameters and saved them as “Perk_something”.
i also created another widget named PerkSystem. for this i wanted to show 3 random perks. i wanted to read folder Perks, fill the array with perks from folder, pick 3 random perks and show them in viewport. in unity it takes me 10 min of work :smiley: here i lost 2 days.

Try it like so:

Adding them to a vertical box because it’s neater. Use the viewport if that’s more convenient.

for this i wanted to show 3 random perks

Oops, forgot about that:

it takes me 10 min of work

pfeeew, made it in under 10 mins:

:innocent:

Thank you, you were fast! :smiley:
Is there a way to dynamically fill an array by opening a folder and reading all files, checking if the file name has the substring “Perk_”, and adding it to the array? I tried to create a C++ function, and I got to the part where I have the proper asset, but I don’t know how to create an instance. WidgetClass is always null

TArray<UWidgetBlueprint*> UPerkController::FindAllPerks()
{
    TArray<UWidgetBlueprint*> Perks;

    // Find all User Widgets in the 'Perks' folder
    TArray<FAssetData> AssetList;
    const FString PerksFolder = TEXT("/Game/Perks");
    FAssetRegistryModule& AssetRegistry = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
    AssetRegistry.Get().GetAssetsByPath(*PerksFolder, AssetList, true);

    for (const FAssetData& Asset : AssetList)
    {        
        if (Asset.AssetName.ToString().Find(TEXT("Perk_")) != INDEX_NONE)
        {   
            FString AssetPath = Asset.PackageName.ToString() + TEXT("/") + Asset.AssetName.ToString();
            UClass* WidgetClass = FindObject<UClass>(nullptr, *AssetPath);

            if (!WidgetClass)
            {
                WidgetClass = LoadClass<UWidgetBlueprint>(nullptr, *AssetPath);
            }

            if (WidgetClass)
            {
                UWidgetBlueprint* Widget = NewObject<UWidgetBlueprint>(GetTransientPackage(), WidgetClass);

                if (Widget != nullptr)
                {
                    Perks.Add(Widget);
                }
            }
        }
    }

    return Perks;
}