继承蓝图函数库UBlueprintFunctionLibrary,实现保存文件到硬盘
大概流程:
1+创建一个C++类,继承自UBlueprintFunctionLibrary。这里需要注意一点,在编辑器中不能添加父类为UBlueprintFunctionLibrary的c++类. 所以需要先利用编辑器一个继承自任意类的c++类,之后在头文件中更改.
2+UBlueprintFunctionLibrary子类中,带有BlueprintCallable属性的函数都可以在Blueprint中调用.
3+若函数带有BlueprintPure属性,则说明此函数不会修改任何游戏状态,因此无需exec链的触发(在Blueprint中体现为没有白线输入),可以在任何时刻被调用获取其结果.
4+声明为静态函数,故此创建的蓝图函数库可以在任何工程中使用.
具体代码如下:
UxwlBlueNode.h
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "xwlBlueNode.generated.h"
/**
*
*/
UCLASS()
class UxwlBlueNode : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Test")
static FString GetHappyMessage();
UFUNCTION(BlueprintCallable, Category = "Test")
static bool SaveToFile(FString Dir, FString Name, FString Text, bool Overwrite = false);
};
UxwlBlueNode.cpp
#include "xwlBlueNode.h"
UxwlBlueNode::UxwlBlueNode(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
}
FString UxwlBlueNode::GetHappyMessage() {
return FString("Hello, I'm Happy!");
}
bool UxwlBlueNode::SaveToFile(
FString Dir,
FString Name,
FString Text,
bool Overwrite
)
{
IPlatformFile& F = FPlatformFileManager::Get().GetPlatformFile();
if (!F.DirectoryExists(*Dir))
{
F.CreateDirectoryTree(*Dir);
if (!F.DirectoryExists(*Dir))
{
return false;
}
}
Dir += "\\";
Dir += Name;
if (!Overwrite)
{
if (F.FileExists(*Dir))
{
return false;
}
}
return FFileHelper::SaveStringToFile(Text, *Dir);
}
其中特别注意到SaveToFile参数FString 类型,当参数传进来后,居然用到了*操作符,这是很怪异的.其实我们参看FString定义可以看出:
UnrealString.h
/**
* Get pointer to the string
*
* @Return Pointer to Array of TCHAR if Num, otherwise the empty string
*/
FORCEINLINE const TCHAR* operator*() const
{
return Data.Num() ? Data.GetTypedData() : TEXT("");
}
这是FString内部对*操作符的重载.一切尽在不言中!
最后在蓝图中使用:
参考链接:
https://wiki.unrealengine.com/Blueprint_Function_Library,_Create_Your_Own_to_Share_With_Others
————————————————
版权声明:本文为CSDN博主「A3630623」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a3630623/article/details/22956769