Skip to content

Server and Client sharing LuaState in single process PIE #37

@Ollinator

Description

@Ollinator

Hey there!

I've recently played around with LuaMachine and replication and noticed some weirdness going on: When launching 2 instances in PIE using "run in one process" and executing an IsServer() call would report FALSE on the server when executed through an RPC which calls a lua function calling the IsServer() function. The issue (naturally) does not happen when running with multiple processes, so it appears like the two instances somehow share the same luastate.
I've first experienced it in 4.26, but also reproduced it in 4.27 and 5.0, each with latest plugin version available. Also attaching two test projects demonstrating it both in 4.27 and 5.0. Just run them with 2 players and press space on the client, this will execute the chain of functions and print each step on screen and in the log.
LuaTest.zip
LuaTest_UE5.zip

As its a bit complicated to understand perhaps, lets go through the setup step by step! :D

  1. the LuaState defines a function IsServer() which just returns the result of the engines IsServer() to check the state:
    image

  2. A simple Lua script making a table with a single function that just executes the IsServer() function above and compares it with the expected result (which is coming from a native call of the engines IsServer() function):

TestActor = {}

function TestActor:TestFunction(msg, should_be_server)
    mytype = IsServer() and "SERVER" or "CLIENT"
    wrongType = IsServer() ~= should_be_server

    if wrongType then msg = msg .." ---- wrong type reported!" end

    Print("Lua: "..mytype..": "..msg, wrongType)
end
  1. then, in a test actor, making a copy of the table above and keeping it as a reference for later calls:
    image

  2. in the test actor, a call into the lua function, providing the native result of IsServer():
    image

  3. and finally the chain of RPCs executed in the test actor calling the lua function in each step to report the results of the IsServer() function:
    image

when executing this, this is the print out:

[BP_TestPawn_C_1] Server: BP_TestPawn_C_1: BeginPlay SERVER
[BP_LuaState_Test_C_0] Server: BP_LuaState_Test_C_0: Lua: SERVER: BeginPlay
[BP_TestPawn_C_0] Client 1: BP_TestPawn_C_0: BeginPlay CLIENT
[BP_LuaState_Test_C_0] Client 1: BP_LuaState_Test_C_0: Lua: CLIENT: BeginPlay
[BP_TestPawn_C_1] Client 1: BP_TestPawn_C_1: BeginPlay CLIENT
[BP_LuaState_Test_C_0] Client 1: BP_LuaState_Test_C_0: Lua: CLIENT: BeginPlay
[BP_TestPawn_C_0] Client 1: BP_TestPawn_C_0: Input CLIENT
[BP_LuaState_Test_C_0] Client 1: BP_LuaState_Test_C_0: Lua: CLIENT: input
[BP_TestPawn_C_1] Server: BP_TestPawn_C_1: received ServerRPC SERVER
[BP_LuaState_Test_C_0] Client 0: BP_LuaState_Test_C_0: Lua: CLIENT: on server ---- wrong type reported!
[BP_TestPawn_C_1] Server: BP_TestPawn_C_1: received MulticastRPC SERVER
[BP_LuaState_Test_C_0] Client 0: BP_LuaState_Test_C_0: Lua: CLIENT: multicast ---- wrong type reported!
[BP_TestPawn_C_0] Client 1: BP_TestPawn_C_0: received MulticastRPC CLIENT
[BP_LuaState_Test_C_0] Client 1: BP_LuaState_Test_C_0: Lua: CLIENT: multicast

As you can see, both client and server execute within the same lua state, and despite the native call reports to be the server properly, the call from the lua function (executed from the RPC) is reporting on the server to be "Client 0".

I'm quite sure this has to do with the lua state being a singleton, but for some reason client and server share the same singleton instead of creating its own. Not sure if that is easily fixable or at all. Workaround for now is to always run in multiple processes, but this makes testing and debugging really slow and cumbersome...

Maybe I'm also doing something stupid though :D

Thx for your support and that awesome plugin!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions