代码拉取完成,页面将自动刷新
同步操作将从 Tencent/UnLua 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "TutorialBlueprintFunctionLibrary.h"
#include "Kismet/KismetSystemLibrary.h"
#include "UnLua.h"
static void PrintScreen(const FString& Msg)
{
UKismetSystemLibrary::PrintString(nullptr, Msg, true, false, FLinearColor(0, 0.66, 1), 100);
}
void UTutorialBlueprintFunctionLibrary::CallLuaByGlobalTable()
{
PrintScreen(TEXT("[C++]CallLuaByGlobalTable 开始"));
UnLua::FLuaEnv Env;
const auto bSuccess = Env.DoString("G_08_CppCallLua = require 'Tutorials.08_CppCallLua'");
check(bSuccess);
const auto RetValues = UnLua::CallTableFunc(Env.GetMainState(), "G_08_CppCallLua", "CallMe", 1.1f, 2.2f);
check(RetValues.Num() == 1);
const auto Msg = FString::Printf(TEXT("[C++]收到来自Lua的返回,结果=%f"), RetValues[0].Value<float>());
PrintScreen(Msg);
PrintScreen(TEXT("[C++]CallLuaByGlobalTable 结束"));
}
void UTutorialBlueprintFunctionLibrary::CallLuaByFLuaTable()
{
PrintScreen(TEXT("[C++]CallLuaByFLuaTable 开始"));
UnLua::FLuaEnv Env;
const auto Require = UnLua::FLuaFunction(&Env, "_G", "require");
const auto RetValues1 = Require.Call("Tutorials.08_CppCallLua");
check(RetValues1.Num() == 1);
const auto RetValue = RetValues1[0];
const auto LuaTable = UnLua::FLuaTable(&Env, RetValue);
const auto RetValues2 = LuaTable.Call("CallMe", 3.3f, 4.4f);
check(RetValues2.Num() == 1);
const auto Msg = FString::Printf(TEXT("[C++]收到来自Lua的返回,结果=%f"), RetValues2[0].Value<float>());
PrintScreen(Msg);
PrintScreen(TEXT("[C++]CallLuaByFLuaTable 结束"));
}
bool CustomLoader1(const FString& RelativePath, TArray<uint8>& Data, FString& FullPath)
{
const auto SlashedRelativePath = RelativePath.Replace(TEXT("."), TEXT("/"));
FullPath = FString::Printf(TEXT("%s%s.lua"), *GLuaSrcFullPath, *SlashedRelativePath);
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
FullPath.ReplaceInline(TEXT(".lua"), TEXT("/Index.lua"));
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
return false;
}
bool CustomLoader2(const FString& RelativePath, TArray<uint8>& Data, FString& FullPath)
{
const auto SlashedRelativePath = RelativePath.Replace(TEXT("."), TEXT("/"));
const auto L = UnLua::GetState();
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
const char* Path = lua_tostring(L, -1);
lua_pop(L, 2);
if (!Path)
return false;
TArray<FString> Parts;
FString(Path).ParseIntoArray(Parts, TEXT(";"), false);
for (auto& Part : Parts)
{
Part.ReplaceInline(TEXT("?"), *SlashedRelativePath);
FPaths::CollapseRelativeDirectories(Part);
if (FPaths::IsRelative(Part))
FullPath = FPaths::ConvertRelativePathToFull(GLuaSrcFullPath, Part);
else
FullPath = Part;
if (FFileHelper::LoadFileToArray(Data, *FullPath, FILEREAD_Silent))
return true;
}
return false;
}
void UTutorialBlueprintFunctionLibrary::SetupCustomLoader(int Index)
{
switch (Index)
{
case 1:
FUnLuaDelegates::CustomLoadLuaFile.BindStatic(CustomLoader1);
break;
case 2:
FUnLuaDelegates::CustomLoadLuaFile.BindStatic(CustomLoader2);
break;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。