How do you use FDataTableRowHandle and FindRow

Here is a snippet of code from my personal project:

if (WeatherData)
{
	
	FName WeatherRowName = UEnum::GetValueAsName(InitWeather);
	EGetByNameFlags Flags = EGetByNameFlags::None;
	FString NameString = FString::FromInt(StaticEnum<EWeather>()->GetIndexByName(WeatherRowName, Flags));

	if (Flags != EGetByNameFlags::ErrorIfNotFound)
	{
		FWeather* WeatherRow = WeatherData->FindRow<FWeather>(FName(*NameString), "", true);

		if (WeatherRow)
		{
			InitializeWeatherStruct(WeatherRow);
		}
	}

}

Some information about the variables used:

InitWeather - This is a custom Enum variable of type EWeather as shown below:

UENUM(BlueprintType)
enum class EWeather : uint8
{
	SUNNY = 0 UMETA(DisplayName = "Sunny"),
	CLOUDY = 1 UMETA(DisplayName = "Cloudy"),
	RAINY = 2 UMETA(DisplayName = "Rainy"),
	THUNDER = 3 UMETA(DisplayName = "Thunder"),
	NEWWEATHER = 4 UMETA(DisplayName = "NewWeather")
};

FWeather - This is a custom struct derived from FTableRowBase as shown below:

USTRUCT(BlueprintType)
struct FWeather : public FTableRowBase
{
	GENERATED_BODY()

	//Type of weather based off of EWeather enum type.
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	EWeather WeatherType;

	//
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int WeatherWeight;

	//
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UMaterialInstance* WeatherMaterialInstance = nullptr;

	//Color curve used for the sun.
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UCurveLinearColor* WeatherSunColor = nullptr;

	//Color curve for the skylight
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UCurveLinearColor* WeatherSkyLightColor = nullptr;

	//Color curve for the fog.
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UCurveLinearColor* WeatherFogColor = nullptr;

	//Color curve general/atmosphere color.
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UCurveLinearColor* WeatherOverallColor = nullptr;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TArray<FWeatherScalarParamOverride> ScalarParamOverride;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	bool bSunVisible;

	FWeather()
	{
		WeatherType = EWeather::SUNNY;
		WeatherWeight = 1;
		bSunVisible = true;
	}
};

WeatherData - This is a UDataTable variable that uses the same FTableRowBase as FWeather:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weather")
UDataTable* WeatherData;

What the original snippet of code does is check whether or not the WeatherData UDataTable parameter is valid and contains the appropriate data table. Then, we attempt to get the name of the row represented by the current value of InitWeather by using the function UEnum::GetValueAsName, and we also initialize an EGetByNameFlags value that is returned GetIndexByName(); this tells us if the index is valid or not. As long as Flags != EGetByNameFlags::ErrorIfNotFound, then we continue. From there, we use FindRow from our data table variable, checking for FWeather struct and pass in the NameString, an empty Context String, and true for the parameter bWarnIfRowMissing. Lastly, we do one more valid check for WeatherRow and then move on; WeatherRow being our found Row.

Here is a thread that helped me out when originally writing this code: Get Data Table Row in c++

I hope this helps, and good luck :slight_smile:

2 Likes