From 2a3303dcf99f35a40451ba23c93d8b46b22f5fa9 Mon Sep 17 00:00:00 2001 From: Micha Date: Fri, 7 Feb 2025 15:21:27 +0000 Subject: [PATCH] Basic Instanced State Mechanics --- .../Private/LuaBlueprintFunctionLibrary.cpp | 37 +++++++++++++++++++ .../Public/LuaBlueprintFunctionLibrary.h | 5 +++ 2 files changed, 42 insertions(+) diff --git a/Source/LuaMachine/Private/LuaBlueprintFunctionLibrary.cpp b/Source/LuaMachine/Private/LuaBlueprintFunctionLibrary.cpp index df12eceb..0003f54d 100644 --- a/Source/LuaMachine/Private/LuaBlueprintFunctionLibrary.cpp +++ b/Source/LuaMachine/Private/LuaBlueprintFunctionLibrary.cpp @@ -2033,3 +2033,40 @@ ULuaState* ULuaBlueprintFunctionLibrary::CreateDynamicLuaState(UObject* WorldCon return NewLuaState->GetLuaState(WorldContextObject->GetWorld()); } +FLuaValue ULuaBlueprintFunctionLibrary::LuaStateRunFile(UObject* WorldContextObject, ULuaState* State, const FString& Filename, const bool bIgnoreNonExistent) +{ + FLuaValue ReturnValue; + + if (!State->RunFile(Filename, bIgnoreNonExistent, 1)) + { + if (State->bLogError) + State->LogError(State->LastError); + State->ReceiveLuaError(State->LastError); + } + else + { + ReturnValue = State->ToLuaValue(-1); + } + + State->Pop(); + return ReturnValue; +} +FLuaValue ULuaBlueprintFunctionLibrary::LuaStateGlobalCall(UObject* WorldContextObject, ULuaState* State, const FString& Name, TArray Args) +{ + FLuaValue ReturnValue; + int32 ItemsToPop = State->GetFieldFromTree(Name); + + int NArgs = 0; + for (FLuaValue& Arg : Args) + { + State->FromLuaValue(Arg); + NArgs++; + } + + State->PCall(NArgs, ReturnValue); + + // we have the return value and the function has been removed, so we do not need to change ItemsToPop + State->Pop(ItemsToPop); + + return ReturnValue; +} diff --git a/Source/LuaMachine/Public/LuaBlueprintFunctionLibrary.h b/Source/LuaMachine/Public/LuaBlueprintFunctionLibrary.h index 2a0195d9..41a4000c 100644 --- a/Source/LuaMachine/Public/LuaBlueprintFunctionLibrary.h +++ b/Source/LuaMachine/Public/LuaBlueprintFunctionLibrary.h @@ -430,6 +430,11 @@ class LUAMACHINE_API ULuaBlueprintFunctionLibrary : public UBlueprintFunctionLib UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "Lua") static ULuaState* CreateDynamicLuaState(UObject* WorldContextObject, TSubclassOf LuaStateClass); + UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject", AutoCreateRefTerm = "Args"), Category = "Lua") + static FLuaValue LuaStateGlobalCall(UObject* WorldContextObject, ULuaState* State, const FString& Name, TArray Args); + + UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "Lua") + static FLuaValue LuaStateRunFile(UObject* WorldContextObject, ULuaState* State, const FString& Filename, const bool bIgnoreNonExistent); private: static void HttpRequestDone(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful, TSubclassOf LuaState, TWeakObjectPtr World, const FString SecurityHeader, const FString SignaturePublicExponent, const FString SignatureModulus, FLuaHttpSuccess Completed); static void HttpGenericRequestDone(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful, TWeakPtr Context, FLuaHttpResponseReceived ResponseReceived, FLuaHttpError Error);