#717 蓝图里获取配置信息   bp     setting     snippets     UE4     8 months ago (owner) Document
// DeveloperSettings.Build.cs
#include "Kismet/BlueprintFunctionLibrary.h"
#include "xxx.generated.h"

UCLASS(Config=Game, defaultconfig)
class UMySettings : public UDeveloperSettings
{
	GENERATED_BODY()

public:
	UPROPERTY(Config, EditAnywhere, BlueprintReadOnly, Category = "General")
	bool DisableScreenMessages = false; // 在蓝图里可以直接访问该变量(GetClassDefaults)

	/** Gets the settings container name for the settings, either Project or Editor */
	virtual FName GetContainerName() const override { return TEXT("Project"); }
	/** Gets the category for the settings, some high level grouping like, Editor, Engine, Game...etc. */
	virtual FName GetCategoryName() const override { return TEXT("Game"); }
	/** The unique name for your section of settings, uses the class's FName. */
	virtual FName GetSectionName() const override { return TEXT("General"); }
};


UCLASS()
class UMyBPLibrary : public UBlueprintFunctionLibrary
{
        GENERATED_BODY()
public:
	UFUNCTION(BlueprintCallable, Category = "MyBP")
	static bool GetGameConfig(const FString& Section, const FString& Key, FString& Val);

	UFUNCTION(BlueprintCallable, Category = "MyBP")
	static bool DumpGameConfig();
};


bool UMyBPLibrary::GetGameConfig(const FString& Section, const FString& Key, FString& Val)
{
	if (GConfig == nullptr)
	{
		return false;
	}

	FString ConfigFilePath = FPaths::ProjectDir() + TEXT("GameConfig/GameConfig.ini");
	ConfigFilePath = FPaths::ConvertRelativePathToFull(ConfigFilePath);
	return GConfig->GetString(*Section, *Key, Val, ConfigFilePath);
}

bool UMyBPLibrary::DumpGameConfig()
{
	if (GConfig == nullptr)
	{
		return false;
	}

	FString ConfigFilePath = FPaths::ProjectDir() + TEXT("GameConfig/GameConfig.ini");
	ConfigFilePath = FPaths::ConvertRelativePathToFull(ConfigFilePath);
	UE_LOG(LogTemp, Log, TEXT("Dumping GameConfig file: [%s] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"), *ConfigFilePath);

	TArray<FString> OutSectionNames;
	bool bOk = GConfig->GetSectionNames(ConfigFilePath, OutSectionNames);

	for(auto s: OutSectionNames)
	{
		TArray<FString> OutResult;
		GConfig->GetSection(*s, OutResult, ConfigFilePath);
		UE_LOG(LogTemp, Log, TEXT("Section: [%s]"), *s);
		for(auto line: OutResult)
		{
			UE_LOG(LogTemp, Log, TEXT("key-value: %s"), *line);
		}
		UE_LOG(LogTemp, Log, TEXT("End Section <<<<<<<<<<<<<<<<<"));
	}
	UE_LOG(LogTemp, Log, TEXT("End Dumping <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"));

	return bOk;
}