Skip to content

Conversation

@micheljung
Copy link

@micheljung micheljung commented Jul 20, 2025

My idea is to connect a Lua table to an Actor, but without "mixing" them.

So UShipLuaComponent::LuaObject references the Lua table:

/** The Lua table that is connected to this actor component. */
FLuaValue LuaObject;

When the actor is spawned, a Lua table should be created for it:

void UShipLuaComponent::BeginPlay()
{
  Super::BeginPlay();

  const TArray Args = {FLuaValue(this)};
  LuaObject = LuaCallFunction("Ship.Init", Args, true);
}
--- This function is called by the engine whenever a [Ship] actor is spawned.
---@param actor ShipActor the engine actor that is connected to this table
function Ship.Init(actor)
  local instance = setmetatable({}, Ship)
  instance.Actor = actor
  return instance
end

The reason I do this is because modders should be able to add functions to Ship:

-- HasTarget should not have to exist in the LuaComponent
function Ship:HasTarget()
  return self.Target ~= nil
end

But they can also delegate to the LuaComponent:

---@param input number Value from `0.0` - `1.0`
function Ship:SetThrottleInput(input)
  self.Actor.SetThrottleInput(input)
end

And when the engine calls a function like Tick, self should be the Lua table, not the LuaComponent:

function Ship:Tick(deltaTime)
  print("Tick "..deltaTime)
end

To achieve this, I need to be able to call a Lua function without passing the LuaComponent implicitly as self. I thought that the setting bImplicitSelf = false would achieve this. But in methods like LuaCallFunction, this flag is not respected.

This PR fixes that.

Unfortunately, this is a breaking change. To preserve the old behavior, bImplicitSelf would need to default to true, but that would also be a breaking change.

Maybe I'm not getting it right anyway. In that case, maybe a new flag could be introduced to control the behavior? Then it would also not be breaking.

@micheljung
Copy link
Author

micheljung commented Jul 20, 2025

How about this as a (transitional?) opt-in solution:

  /**
   * Whether to implicitly pass this component as the first argument (usually <code>self</code> when calling a Lua
   * function.
   *
   * Currently, this is only respected by <code>ULuaState::MetaTableFunction__call</code> and
   * <code>ULuaState::MetaTableFunction__rawcall</code>. If you want this to work for <code>LuaCall...</code> functions
   * as well, you need to enable <code>bImplicitSelfForLuaCalls</code>, too.
   */
  UPROPERTY(EditAnywhere, Category = "Lua")
  bool bImplicitSelf;

  /**
   * When enabled, <code>bImplicitSelf</code> will be respected by all <code>LuaCall...</code> functions.
   *
   * See <a href="https://github.com/rdeioris/LuaMachine/pull/77">this PR</a>.
   */
  UPROPERTY(EditAnywhere, Category = "Lua")
  bool bImplicitSelfForLuaCalls;

@micheljung micheljung force-pushed the mj/fix-implicit-self branch from 62e6869 to 3b16669 Compare July 20, 2025 19:18
Comment on lines +114 to +120
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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that I'm not doing it wrong here; see line 108

Comment on lines +158 to +164
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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, see line 151

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.
@micheljung micheljung force-pushed the mj/fix-implicit-self branch from 3b16669 to 24d7b60 Compare July 22, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant