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 diff --git a/Source/LuaMachine/Private/LuaComponent.cpp b/Source/LuaMachine/Private/LuaComponent.cpp index 8a50de6..dd351a9 100644 --- a/Source/LuaMachine/Private/LuaComponent.cpp +++ b/Source/LuaMachine/Private/LuaComponent.cpp @@ -111,9 +111,13 @@ FLuaValue ULuaComponent::LuaCallFunction(const FString& Name, TArray int32 ItemsToPop = L->GetFieldFromTree(Name, bGlobal); + bool bAddSelfArg = bImplicitSelfForLuaCalls && bImplicitSelf; // first argument (self/actor) - L->PushValue(-(ItemsToPop + 1)); - int NArgs = 1; + if (bAddSelfArg) + { + L->PushValue(-(ItemsToPop + 1)); + } + int NArgs = bAddSelfArg ? 1 : 0; for (FLuaValue& Arg : Args) { L->FromLuaValue(Arg); @@ -151,9 +155,13 @@ TArray ULuaComponent::LuaCallFunctionMulti(FString Name, TArrayGetFieldFromTree(Name, bGlobal); int32 StackTop = L->GetTop(); + bool bAddSelfArg = bImplicitSelfForLuaCalls && bImplicitSelf; // first argument (self/actor) - L->PushValue(-(ItemsToPop + 1)); - int NArgs = 1; + if (bAddSelfArg) + { + L->PushValue(-(ItemsToPop + 1)); + } + int NArgs = bAddSelfArg ? 1 : 0; for (FLuaValue& Arg : Args) { L->FromLuaValue(Arg); @@ -199,11 +207,15 @@ FLuaValue ULuaComponent::LuaCallValue(FLuaValue Value, TArray Args) // push function L->FromLuaValue(Value); + bool bAddSelfArg = bImplicitSelfForLuaCalls && bImplicitSelf; // push component pointer as userdata - L->NewUObject(this, nullptr); + if (bAddSelfArg) + { + L->NewUObject(this, nullptr); + } L->SetupAndAssignUserDataMetatable(this, Metatable, nullptr); - int NArgs = 1; + int NArgs = bAddSelfArg ? 1 : 0; for (FLuaValue& Arg : Args) { L->FromLuaValue(Arg); @@ -282,11 +294,15 @@ TArray ULuaComponent::LuaCallValueMulti(FLuaValue Value, TArrayFromLuaValue(Value); int32 StackTop = L->GetTop(); - // push component pointer as userdata - L->NewUObject(this, nullptr); + bool bAddSelfArg = bImplicitSelfForLuaCalls && bImplicitSelf; + if (bAddSelfArg) + { + // push component pointer as userdata + L->NewUObject(this, nullptr); + } L->SetupAndAssignUserDataMetatable(this, Metatable, nullptr); - int NArgs = 1; + int NArgs = bAddSelfArg ? 1 : 0; for (FLuaValue& Arg : Args) { L->FromLuaValue(Arg); @@ -374,4 +390,4 @@ FLuaValue ULuaComponent::ReceiveLuaMetaIndex_Implementation(FLuaValue Key) bool ULuaComponent::ReceiveLuaMetaNewIndex_Implementation(FLuaValue Key, FLuaValue Value) { return false; -} \ No newline at end of file +} diff --git a/Source/LuaMachine/Public/LuaComponent.h b/Source/LuaMachine/Public/LuaComponent.h index 208715c..940e958 100644 --- a/Source/LuaMachine/Public/LuaComponent.h +++ b/Source/LuaMachine/Public/LuaComponent.h @@ -43,9 +43,25 @@ class LUAMACHINE_API ULuaComponent : public UActorComponent UPROPERTY(EditAnywhere, Category="Lua") bool bLogError; - UPROPERTY(EditAnywhere, Category = "Lua") + /** + * Whether to implicitly pass this component as the first argument (usually self when calling a Lua + * function. + * + * Currently, this is only respected by ULuaState::MetaTableFunction__call and + * ULuaState::MetaTableFunction__rawcall. If you want this to work for LuaCall... functions + * as well, you need to enable bImplicitSelfForFunctionCalls, too. + */ + UPROPERTY(EditAnywhere, Category = "Lua") bool bImplicitSelf; + /** + * When enabled, bImplicitSelf will be respected by all LuaCall... functions. + * + * See this PR. + */ + UPROPERTY(EditAnywhere, Category = "Lua") + bool bImplicitSelfForLuaCalls; + UPROPERTY(EditAnywhere, Category = "Lua") TArray GlobalNames;