This is my code calling the Widgets:
void AGameController::BeginPlay()
{
Super::BeginPlay();
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
// Show the mouse cursor
PlayerController->SetShowMouseCursor(true);
UWorld* world = GetWorld();
FActorSpawnParameters spawnParams;
spawnParams.Owner = this;
// Spawn the fixed camera
instance_AFixedCamera = world->SpawnActor<AFixedCamera>(*aFixedCamera, initialLocationCamera, initialRotationCamera, spawnParams);
// Spawn the main character
instance_AMainCharacter = world->SpawnActor<AMainCharacter>(*aMainCharacter, initialLocationMC, FRotator::ZeroRotator, spawnParams);
// Spawn the NPC
instance_ANPC = world->SpawnActor<ANPC>(*aNPC, initialLocationNPC, FRotator::ZeroRotator, spawnParams);
// Set the initial camera focus to look at the main character and the NPC
LookAt(instance_AMainCharacter, instance_ANPC, 90.f);
TArray<FString> skillNames; // get the list of skill names from somewhere
skillNames.Add("BP_FireTornado");
for (size_t i = 0; i < MAXNUMBERSKILL-1; i++)
{
skillNames.Add("BP_Empty");
}
// Get the players screen size
FVector2D screenSize = GEngine->GameViewport->Viewport->GetSizeXY();
float screenWidth = screenSize.X;
float screenHeight = screenSize.Y;
// Calculate the scale factor for the widget size
float referenceScreenWidth = 1920;
float referenceScreenHeight = 1080;
float scaleFactorX = screenWidth / referenceScreenWidth;
float scaleFactorY = screenHeight / referenceScreenHeight;
// Calculate the new widget size
float newSizeX = SKILLSIZEX * scaleFactorX;
float newSizeY = SKILLSIZEY * scaleFactorY;
// Calculate the total width occupied by all widgets and the space between them
float totalWidth = MAXNUMBERSKILL * newSizeX + (MAXNUMBERSKILL - 1) * SKILLPADDING;
// Calculate the starting X position for the first widget
float startX = -totalWidth / 2 + (newSizeX + SKILLPADDING) / 2;
float accumulatedWidth = 0.0f;
for (const FString& skillName : skillNames)
{
FString assetPath = FString::Printf(TEXT("/Game/Blueprints/Widgets/Skills/%s.%s_C"), *skillName, *skillName);
UClass* skillClass = LoadClass<USkillWidget>(nullptr, *assetPath);
if (skillClass != nullptr)
{
// Spawn a new widget
USkillWidget* skillWidget = CreateWidget<USkillWidget>(GetWorld(), skillClass);
skillBlueprints.Add(skillWidget);
// Set its position and size
float posX = startX + accumulatedWidth;
float posY = SKILLYPOS;
skillWidget->SetPosition(posX, posY, newSizeX, newSizeY);
// Add it to the player screen
skillWidget->AddToPlayerScreen();
// Update the accumulated width
accumulatedWidth += newSizeX + SKILLPADDING;
}
}
if (uGameplayWidget)
{
instance_UGameplayWidget = CreateWidget<UGameplayWidget>(GetWorld(), uGameplayWidget);
instance_UGameplayWidget->AddToPlayerScreen();
maxTime = TIMELIMIT + extraTime;
totalTime = maxTime;
}
}
Just ignore those lines inside the code, it’s just for Debugging:
TArray<FString> skillNames; // get the list of skill names from somewhere
skillNames.Add("BP_FireTornado");
for (size_t i = 0; i < MAXNUMBERSKILL-1; i++)
{
skillNames.Add("BP_Empty");
}
My problem is:
I have a for instantiating 10 widgets (skillWidget) that I get it from my folder
And
One if instantiating 1 widget (instance_UGameplayWidget), this one I get from TSubclassOf
Both work fine while in the editor, but when I try the “Quick Launch” to see if the HUD is fine, only the instance_UGameplayWidget appears.
I’m new at UE5, so I think the path: “/Game/Blueprints/Widgets/Skills/%s.%s_C” changes with the “Quick Launch” (That’s the only thing that I can think of), I tried with FPaths::ProjectContentDir()
FString assetPath = FPaths::ProjectContentDir() + FString::Printf(TEXT("Blueprints/Widgets/Skills/%s.%s_C"), *skillName, *skillName);
But I got this error:
LogUObjectGlobals: Warning: LoadPackage can’t find package /Unreal Projects/MyProject/Content/Blueprints/Widgets/Skills/BP_FireTornado.
And if I try to *UE_LOG(LogTemp, Warning, TEXT(“%s”), FPaths::ProjectContentDir()); it gives me this path:
LogTemp: Warning: …/…/…/…/…/Unreal Projects/MyProject/Content/
I also tried to use AddToViewport(), didn’t work, and tried without the position calculations, didn’t work either.
So, I have no idea left, I don’t know what to do and what to try, if anyone know how to fix it, please, let me know.