Eluna icon indicating copy to clipboard operation
Eluna copied to clipboard

Eluna states communication

Open iThorgrim opened this issue 1 year ago • 4 comments

This system introduces a Mediator pattern to Eluna, allowing communication between different Lua states based on "mapId". It enables the registration and notification of events across states, such as player logins or custom actions.

How It Works:

  • Each Lua state is identified by its mapId, and a "main/world" state (mapId == -1).
  • Events can be registered in one state and triggered from another, allowing cross-state communication.
  • Currently, only the string type can be passed between states.

Little Example

local mapId = GetStateMapId()

if mapId == -1 then
    -- World state (-1)
    local function OnGiveExperience(playerName)
        print(playerName .. " has connected from another state!")
    end
    RegisterStateEvent(mapId, "OnGiveExperience", OnGiveExperience)
else
    -- Map state (mapId)
    local function OnGiveExperience(event, player)
        NotifyStateEvent(-1, "OnGiveExperience", tostring(player:GetName()))
    end
    RegisterPlayerEvent(12, OnGiveExperience)
end

This system can be expanded in the future to support additional data types and more complex interactions between Lua states.

iThorgrim avatar Sep 05 '24 11:09 iThorgrim

Modifying a global unordered_map is not threadsafe

Shauren avatar Sep 06 '24 16:09 Shauren

I think a better approach would be to implement something like a topic based asynchronous message queue, where each state would have a consumer to just consume messages from the queue at the start of the state update cycle. It would probably be relatively easy to implement something like AMQP and just use an external message queue server.

Foereaper avatar Sep 07 '24 13:09 Foereaper

I think a better approach would be to implement something like a topic based asynchronous message queue, where each state would have a consumer to just consume messages from the queue at the start of the state update cycle. It would probably be relatively easy to implement something like AMQP and just use an external message queue server.

Integrating AMQP would be an excellent idea, especially as it would also allow you to “chat” with Eluna via external applications, but I think that's a bit too advanced for me ^^.

However, I've taken note of your messages and tried to make the code a bit ""cleaner"", I've used ChatGPT for some things (especially on the mutex issue), I took the opportunity to comment my code, so that it's a bit more readable.

There's the beginnings of a message system that I think is better than the old one, at least better than my first draft.

iThorgrim avatar Sep 08 '24 05:09 iThorgrim

Thread issues are still there, for example this map modification is still completely unprotected https://github.com/ElunaLuaEngine/Eluna/pull/496/files#diff-292f7269c07f69f285305bde0d329bf6235c448853ba6fd0df6e67590f8176acR34

From what I see this is also broken if you use RegisterStateEvent with a instance map id, only the last instance registered will be receiving events with multistate

Shauren avatar Sep 09 '24 16:09 Shauren