LuckyRobotsUnreal/Source/Luckyrobots/Private/LuckyRobotsFunctionLibrary.cpp
martinluckyrobots 4b116bf9da Optimize CaptureSetting, GoalTask, etc.
2025-04-02 13:40:55 +08:00

126 lines
3.9 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "LuckyRobotsFunctionLibrary.h"
#include "LuckyRobotsGameInstance.h"
#include "GameFramework/GameUserSettings.h"
#include "Kismet/GameplayStatics.h"
#include "SG_CaptureSetting.h"
ULuckyRobotsGameInstance* ULuckyRobotsFunctionLibrary::GetLuckyRobotsGameInstance(const UObject* WorldContextObject)
{
return Cast<ULuckyRobotsGameInstance>(WorldContextObject->GetWorld()->GetGameInstance());
}
TArray<FRobotData> ULuckyRobotsFunctionLibrary::GetActiveRobotDataList(const UObject* WorldContextObject)
{
TArray<FRobotData> RobotDataList;
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = GetLuckyRobotsGameInstance(WorldContextObject);
if (LuckyRobotsGameInstance)
{
if (LuckyRobotsGameInstance->RobotDataDataTable)
{
FString ContextString;
TArray<FName> RowNames = LuckyRobotsGameInstance->RobotDataDataTable->GetRowNames();
for (auto RowString : RowNames)
{
FRobotData* pRow = LuckyRobotsGameInstance->RobotDataDataTable->FindRow<FRobotData>(FName(RowString), ContextString);
if (pRow)
{
if (pRow->bActive)
{
RobotDataList.Add(*pRow);
}
}
}
}
}
return RobotDataList;
}
TArray<FLevelData> ULuckyRobotsFunctionLibrary::GetActiveLevelDataList(const UObject* WorldContextObject)
{
TArray<FLevelData> LevelDataList;
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = GetLuckyRobotsGameInstance(WorldContextObject);
if (LuckyRobotsGameInstance)
{
if (LuckyRobotsGameInstance->LevelDataTable)
{
FString ContextString;
TArray<FName> RowNames = LuckyRobotsGameInstance->LevelDataTable->GetRowNames();
for (auto RowString : RowNames)
{
FLevelData* pRow = LuckyRobotsGameInstance->LevelDataTable->FindRow<FLevelData>(FName(RowString), ContextString);
if (pRow)
{
if (pRow->bActive)
{
LevelDataList.Add(*pRow);
}
}
}
}
}
return LevelDataList;
}
void ULuckyRobotsFunctionLibrary::UpdateQualitySettings(const UObject* WorldContextObject)
{
ULuckyRobotsGameInstance* LuckyRobotsGameInstance = GetLuckyRobotsGameInstance(WorldContextObject);
if (LuckyRobotsGameInstance)
{
// Load game user settings and apply
UGameUserSettings* GameUserSettings = GEngine->GetGameUserSettings();
if (GameUserSettings)
{
GameUserSettings->SetOverallScalabilityLevel(int(LuckyRobotsGameInstance->CurrentSelectQuality));
GameUserSettings->SaveSettings();
GameUserSettings->ApplySettings(true);
}
}
}
FCaptureSettingsData ULuckyRobotsFunctionLibrary::LoadCaptureSettings()
{
FCaptureSettingsData DefaultCaptureSetting;
DefaultCaptureSetting.FolderName = FText::FromString("robotdata");
DefaultCaptureSetting.FileName = FText::FromString("FILE");
DefaultCaptureSetting.WritesPerSec = FText::FromString("1");
DefaultCaptureSetting.NumberOfPeople = FText::FromString("1");
DefaultCaptureSetting.NumberOfObjects = FText::FromString("1");
DefaultCaptureSetting.NumberOfCaptures = FText::FromString("1");
USG_CaptureSetting* SaveGame = nullptr;
SaveGame = Cast<USG_CaptureSetting>(UGameplayStatics::LoadGameFromSlot("SGCaptureSettings", 0));
if (SaveGame)
{
return SaveGame->CaptureSetting;
}
else
{
SaveGame = Cast<USG_CaptureSetting>(UGameplayStatics::CreateSaveGameObject(USG_CaptureSetting::StaticClass()));
if (SaveGame)
{
SaveGame->CaptureSetting = DefaultCaptureSetting;
UGameplayStatics::SaveGameToSlot(SaveGame, "SGCaptureSettings", 0);
}
}
return DefaultCaptureSetting;
}
void ULuckyRobotsFunctionLibrary::SaveCaptureSettings(FCaptureSettingsData CaptureSetting)
{
USG_CaptureSetting* SaveGame = nullptr;
SaveGame = Cast<USG_CaptureSetting>(UGameplayStatics::LoadGameFromSlot("SGCaptureSettings", 0));
if (!SaveGame)
{
SaveGame = Cast<USG_CaptureSetting>(UGameplayStatics::CreateSaveGameObject(USG_CaptureSetting::StaticClass()));
}
if (SaveGame)
{
SaveGame->CaptureSetting = CaptureSetting;
UGameplayStatics::SaveGameToSlot(SaveGame, "SGCaptureSettings", 0);
}
}