Last active
February 20, 2025 14:52
-
-
Save Ryan-DowlingSoka/ffac550725714671c22db04c32c2bf0b to your computer and use it in GitHub Desktop.
Example file for making a FName row selector based on a fixed data table asset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "CoreMinimal.h" | |
#include "Engine/DataTable.h" | |
#include "UObject/Object.h" | |
#include "DataTableSelectorSettings.generated.h" | |
// DON'T FORGET TO DO THIS: !!!!!!!!! | |
// You will need to add "DeveloperSettings" to YourModuleName.Build.cs 's "PublicDependencyModuleNames" array. | |
// You probably don't need the SILO_API, but if you do it should be YOURMODULENAMENOSPACES_API also feel free to remove it | |
// from all of the following classes and structs. | |
USTRUCT(BlueprintType) | |
struct SILO_API FTestDataTableType : public FTableRowBase | |
{ | |
GENERATED_BODY() | |
UPROPERTY(EditAnywhere) | |
FName TestValue; | |
}; | |
/** | |
* | |
*/ | |
UCLASS(Config=Engine) | |
class SILO_API UDataTableSelectorSettings : public UDeveloperSettings | |
{ | |
GENERATED_BODY() | |
public: | |
UPROPERTY(Config, EditAnywhere, BlueprintReadOnly, Category=DataTableSelectorSettings, meta=(RowType="TestDataTableType")) | |
TSoftObjectPtr<UDataTable> DataTableAsset; | |
UFUNCTION() | |
static TArray<FName> GetDataTableRowNames() | |
{ | |
if(!GetDefault<UDataTableSelectorSettings>()->DataTableAsset.LoadSynchronous()) | |
{ | |
return TArray<FName>(); | |
} | |
// OPTION 1: | |
// Get just the Row Name. Comment this line out if you want to use Option and remove the /**/ comments | |
// on option 2. | |
return GetDefault<UDataTableSelectorSettings>()->DataTableAsset.Get()->GetRowNames(); | |
/* | |
// OPTION 2: | |
// Get a value from the Row instead of using the row name, not guaranteed to be unique. | |
static const FString ContextString(TEXT("UDataTableSelectorSettings::GetDataTableRowNames")); | |
TArray<FTestDataTableType*> AllRows; | |
GetDefault<UDataTableSelectorSettings>()->DataTableAsset.Get()->GetAllRows<FTestDataTableType>(ContextString, AllRows); | |
TArray<FName> RowNames; | |
Algo::Transform(AllRows, RowNames, [](FTestDataTableType* Item) | |
{ | |
// Return here whatever you want from the row, | |
// note: this will show two different items in the list if they have the same values, | |
// but it won't differentiate nor store an index. so be careful. you won't be able to restore which one the | |
// user actually selected when you go to use it. | |
return Item->TestValue; | |
}); | |
return RowNames; | |
*/ | |
} | |
}; | |
/** | |
* | |
*/ | |
UCLASS() | |
class SILO_API ADataTableSelectorTestActor : public AActor | |
{ | |
GENERATED_BODY() | |
public: | |
//The Silo in the below should be replaced with your module's name. The DataTableSelectorSettings is whatever you name the developer settings class above | |
//without the U prefix. The GetDataTableRowNames is the static function above. | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=DataTableSelector, meta=(GetOptions="Silo.DataTableSelectorSettings.GetDataTableRowNames")) | |
FName TestRowName; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment