2017年4月16日 星期日

Unreal: How To Read Customized Configuration File

since: 2017/04/16
update: 2017/04/16
reference:
1. I touchs: Unreal: Create C++ Blueprint Function Library
2. How can I load text from file with blueprint? - UE4 AnswerHub
3. Newline character in UE4 – Bocil Mania
4. set array elem - UE4 AnswerHub


A. 新增專案
    1. 新增 BlueprintBlank 專案, 取名為: ReadConfigFile

    2. 新增 MapsData 資料夾:
        Maps 用來存放預設的關卡地圖; Data 用來放置 config.ini

    3. 新增一個檔名為 config.ini 的檔案, 內容如下:
# show debug information (1:true / 0:false)
debug=1

# remote IP (String)
remoteIP=192.168.1.1

# connect timeout (int)
timeout=10


說明: a. # 開頭為註解
         b. 變數名稱與變數值之間用 "=" 號連接, 且前後不可以有空白
         c. 此例有 3 行是變數的設定(之後需要在字串陣列變數指定此數量)


    4. 點選 Data 資料夾 > 滑鼠右鍵 > Show in Explorer

    5. 將剛剛的 config.ini 檔案 copy 進來

-----------------------------------------------------------------------------------------------

B. 新增 C++ Blueprint Function Library
    1. Add New > New C++ Class...

    2. 點選: Blueprint Function Library > Next

    3. Name: FileHelperFunction > Create Class
        > 自動開啟 visual studio 2015


    4. 修改 FileHelperFunction.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "FileHelperFunction.generated.h"

/**
 *
 */
UCLASS()
class READCONFIGFILE_API UFileHelperFunction : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

   
    //@add ######
    public:

    UFUNCTION(BlueprintCallable, Category = "save")
        static bool FileSaveString(FString SaveString, FString SavePath);

    UFUNCTION(BlueprintPure, Category = "save")
        static bool FileLoadString(FString LoadPath, FString& LoadString);

};

    5. 修改 FileHelperFunction.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "ReadConfigFile.h"
#include "FileHelperFunction.h"


//@add ######
bool UFileHelperFunction::FileSaveString(FString SaveString, FString SavePath)
{
    return FFileHelper::SaveStringToFile(SaveString, *(FPaths::GameDir() + SavePath));
}

bool UFileHelperFunction::FileLoadString(FString LoadPath, FString& LoadString)
{
    return FFileHelper::LoadFileToString(LoadString, *(FPaths::GameDir() + LoadPath));
}


    6. 建置 > 建置方案

-----------------------------------------------------------------------------------------------

C. Level Blueprint: 新增變數
    1. ConfigStrings: 字串陣列

    2. FilteredStrings: 字串陣列

    3. FilteredIndex: 整數

    4. debug: 布林

    5. remoteIP: 字串

    6. timeout: 整數

-----------------------------------------------------------------------------------------------

D. Level Blueprint: Load Config Strings



-----------------------------------------------------------------------------------------------

E. Level Blueprint: Filter Config Strings




-----------------------------------------------------------------------------------------------

F.
Level Blueprint: Set Config Vars








-----------------------------------------------------------------------------------------------

G. Package Project: Windows


-----------------------------------------------------------------------------------------------

H. Package Project: Mac



沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。