ElunaLuaEngine / Eluna

Eluna Lua Engine © for WoW Emulators
https://elunaluaengine.github.io
GNU General Public License v3.0
372 stars 356 forks source link

Eluna Running and reloading on Multiple maps causing issues #485

Closed JadaDev closed 2 months ago

JadaDev commented 2 months ago

When i use reload Eluna it actually reloads on all maps i understand that but -1 should be all maps,

image

Now my problem is i wrote normal script and it's running multiple times :

image

Simple Script :

-- Simple server time announcing.

local eachmsec = 60000

local function AnnounceServerTime(eventId, delay, repeats)
    local currentTime = GetGameTime()
    local hours = math.floor(currentTime / 3600) % 24
    local minutes = math.floor(currentTime / 60) % 60
    local ampm = "AM"
    if hours >= 12 then
        ampm = "PM"
    end
    if hours > 12 then
        hours = hours - 12
    elseif hours == 0 then
        hours = 12
    end
    local timeString = string.format("%02d:%02d %s", hours, minutes, ampm)
    SendWorldMessage("Server Time: " .. timeString)
end

CreateLuaEvent(AnnounceServerTime, eachmsec, 0)
Foereaper commented 2 months ago

That's as expected. Each map has its own Lua state, and each state runs every script that is considered global.

If you want a script to only run on a specific map, put it in a subdirectory prefixed with the map id. If you only want it to run in the global state, add the following to the top of the script.

-- Do not load for the global state
if(GetStateMapId() ~= -1) then return; end

If you want it to run on all states except the global state, then reverse the logic and do ==.

JadaDev commented 2 months ago

That wasn't the case before, why this change all of sudden ?

Foereaper commented 2 months ago

I imagine you didn't intend to close the issue, so I'll reopen it.

From July 1st we've officially switched from the old, single state implementation to multistate by default. What multistate is, is explained in length on our discord server. I'm not sure if you're a member there or not, but it's in the announcement channel.

I'm short, multistate has unique Lua states per map for pretty large performance reasons. The old single state scheme forced emulators to run single threaded, which is pretty bad. Some scripts therefore need to be adjusted to work as intended with multistate.