From 7ba9e174e92c9c87ac3ea983c88b30db1fb84d89 Mon Sep 17 00:00:00 2001 From: Michel Jung Date: Mon, 21 Jul 2025 18:12:57 +0200 Subject: [PATCH 1/2] Add .editorconfig This makes it easier for contributors. I'm not sure what the current max line length is, but I guess it's 140. Also, the current code isn't always properly formatted, this should be fixed in a separate commit. --- .editorconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ca40849 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[{*.h,*.c,*.cpp,*.cs}] +charset = utf-8 +end_of_line = lf +indent_size = 4 +tab_width = 4 +indent_style = tab +insert_final_newline = true +max_line_length = 140 From 9647047ff2c1cb982e958050f0f4370fbfdb56e7 Mon Sep 17 00:00:00 2001 From: Michel Jung Date: Mon, 21 Jul 2025 18:18:37 +0200 Subject: [PATCH 2/2] Respect the function owner when calling a function from Lua In the case of LuaComponent, this allows the user to specify whether the function is to be called on the actor (default behavior) or on the LuaComponent itself. --- Source/LuaMachine/Private/LuaState.cpp | 56 +++++++++++++++++--------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/Source/LuaMachine/Private/LuaState.cpp b/Source/LuaMachine/Private/LuaState.cpp index 023e453..8bb410d 100644 --- a/Source/LuaMachine/Private/LuaState.cpp +++ b/Source/LuaMachine/Private/LuaState.cpp @@ -747,11 +747,19 @@ void ULuaState::FromLuaValue(FLuaValue& LuaValue, UObject* CallContext, lua_Stat } if (CallContext) { - UObject* FunctionOwner = CallContext; - if (ULuaComponent* LuaComponent = Cast(CallContext)) + const UObject* FunctionOwner; + if (LuaValue.Object != nullptr) + { + FunctionOwner = LuaValue.Object; + } + else if (const ULuaComponent* LuaComponent = Cast(CallContext)) { FunctionOwner = LuaComponent->GetOwner(); } + else + { + FunctionOwner = CallContext; + } if (FunctionOwner) { @@ -1344,9 +1352,11 @@ int ULuaState::MetaTableFunction__call(lua_State* L) bool bImplicitSelf = false; int StackPointer = 2; + UFunction* Function = LuaCallContext->Function.Get(); if (ULuaComponent* LuaComponent = Cast(CallScope)) { - CallScope = LuaComponent->GetOwner(); + UClass* DeclaringClass = Cast(Function->GetOuter()); + CallScope = DeclaringClass->IsChildOf(ULuaComponent::StaticClass()) ? LuaCallContext->Context.Get() : LuaComponent->GetOwner(); if (NArgs > 0) { FLuaValue LuaFirstArgument = LuaState->ToLuaValue(StackPointer, L); @@ -1369,13 +1379,13 @@ int ULuaState::MetaTableFunction__call(lua_State* L) } FScopeCycleCounterUObject ObjectScope(CallScope); - FScopeCycleCounterUObject FunctionScope(LuaCallContext->Function.Get()); + FScopeCycleCounterUObject FunctionScope(Function); void* Parameters = FMemory_Alloca(LuaCallContext->Function->ParmsSize); FMemory::Memzero(Parameters, LuaCallContext->Function->ParmsSize); #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) + for (TFieldIterator It(Function); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) { FProperty* Prop = *It; #else @@ -1397,7 +1407,7 @@ int ULuaState::MetaTableFunction__call(lua_State* L) // arguments #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator FArgs(LuaCallContext->Function.Get()); FArgs && ((FArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++FArgs) + for (TFieldIterator FArgs(Function); FArgs && ((FArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++FArgs) { FProperty* Prop = *FArgs; FStructProperty* LuaProp = CastField(Prop); @@ -1454,7 +1464,7 @@ int ULuaState::MetaTableFunction__call(lua_State* L) } LuaState->InceptionLevel++; - CallScope->ProcessEvent(LuaCallContext->Function.Get(), Parameters); + CallScope->ProcessEvent(Function, Parameters); check(LuaState->InceptionLevel > 0); LuaState->InceptionLevel--; @@ -1487,7 +1497,7 @@ int ULuaState::MetaTableFunction__call(lua_State* L) // get return value #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator FArgs(LuaCallContext->Function.Get()); FArgs; ++FArgs) + for (TFieldIterator FArgs(Function); FArgs; ++FArgs) { FProperty* Prop = *FArgs; #else @@ -1550,7 +1560,7 @@ int ULuaState::MetaTableFunction__call(lua_State* L) } #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) + for (TFieldIterator It(Function); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) #else for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) #endif @@ -1582,9 +1592,11 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) bool bImplicitSelf = false; int StackPointer = 2; + UFunction* Function = LuaCallContext->Function.Get(); if (ULuaComponent* LuaComponent = Cast(CallScope)) { - CallScope = LuaComponent->GetOwner(); + UClass* DeclaringClass = Cast(Function->GetOuter()); + CallScope = DeclaringClass->IsChildOf(ULuaComponent::StaticClass()) ? LuaCallContext->Context.Get() : LuaComponent->GetOwner(); if (NArgs > 0) { FLuaValue LuaFirstArgument = LuaState->ToLuaValue(StackPointer, L); @@ -1607,13 +1619,13 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) } FScopeCycleCounterUObject ObjectScope(CallScope); - FScopeCycleCounterUObject FunctionScope(LuaCallContext->Function.Get()); + FScopeCycleCounterUObject FunctionScope(Function); void* Parameters = FMemory_Alloca(LuaCallContext->Function->ParmsSize); FMemory::Memzero(Parameters, LuaCallContext->Function->ParmsSize); #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) + for (TFieldIterator It(Function); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) { FProperty* Prop = *It; #else @@ -1635,7 +1647,7 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) // arguments #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator FArgs(LuaCallContext->Function.Get()); FArgs && ((FArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++FArgs) + for (TFieldIterator FArgs(Function); FArgs && ((FArgs->PropertyFlags & (CPF_Parm | CPF_ReturnParm)) == CPF_Parm); ++FArgs) { FProperty* Prop = *FArgs; #else @@ -1648,7 +1660,7 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) } LuaState->InceptionLevel++; - CallScope->ProcessEvent(LuaCallContext->Function.Get(), Parameters); + CallScope->ProcessEvent(Function, Parameters); check(LuaState->InceptionLevel > 0); LuaState->InceptionLevel--; @@ -1681,7 +1693,7 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) // get return value #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator FArgs(LuaCallContext->Function.Get()); FArgs; ++FArgs) + for (TFieldIterator FArgs(Function); FArgs; ++FArgs) { FProperty* Prop = *FArgs; #else @@ -1707,7 +1719,7 @@ int ULuaState::MetaTableFunction__rawcall(lua_State * L) } #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION >= 25 - for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) + for (TFieldIterator It(Function); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) #else for (TFieldIterator It(LuaCallContext->Function.Get()); (It && It->HasAnyPropertyFlags(CPF_Parm)); ++It) #endif @@ -2985,11 +2997,19 @@ void ULuaState::SetupAndAssignUserDataMetatable(UObject * Context, TMap(Context)) + const UObject* FunctionOwner; + if (Pair.Value.Object != nullptr) + { + FunctionOwner = Pair.Value.Object; + } + else if (const ULuaComponent* LuaComponent = Cast(Context)) { FunctionOwner = LuaComponent->GetOwner(); } + else + { + FunctionOwner = Context; + } if (FunctionOwner) {