Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unintuitive scripting behavior: source variable in Lua becomes nil if any time passes during event handler #1502

Closed
Lucas7yoshi opened this issue Jun 23, 2022 · 3 comments
Labels
ScRT: Lua Issues/PRs related to the Lua scripting runtime

Comments

@Lucas7yoshi
Copy link
Contributor

When a server event handler is being run, the source variable thats inherently provided becomes nil if any time passes i.e Wait()'ing

This is confusing behavior and presumably not intended

it can be worked around by simply defining a second variable and setting it to the value of source at the beginning but is obviously not ideal

Repro code:

CLIENT:

RegisterCommand("sourcerepro", function(source, args, raw)
    TriggerServerEvent("reproevent")
end)

SERVER:

RegisterNetEvent("reproevent")
AddEventHandler("reproevent", function()
    print(source)
    Wait(0)
    print(source)
end)
@maicek
Copy link

maicek commented Jun 23, 2022

It's not bug, the source variable is defined in scheduler.lua as global variable

Citizen.SetEventRoutine(function(eventName, eventPayload, eventSource)
-- set the event source
local lastSource = _G.source
_G.source = eventSource

So, you can simply use:

local src = source

at top of event handler to use event source after wait

@Lucas7yoshi
Copy link
Contributor Author

It's not bug, the source variable is defined in scheduler.lua as global variable

Citizen.SetEventRoutine(function(eventName, eventPayload, eventSource)
-- set the event source
local lastSource = _G.source
_G.source = eventSource

So, you can simply use:

local src = source

at top of event handler to use event source after wait

Noted, i figured this was some weird behavior but seeing how that works now makes sense and any alternative would not be within the realm of easy fixing. Closing

@Lucas7yoshi Lucas7yoshi closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2022
@blattersturm
Copy link
Contributor

This is still somewhat illogical behavior, but somehow dynamically adding upvalues for a handler sounds like a potential mess (and requiring the lexer/parser to be aware of this beforehand, even).

A similar alternative could be something like the .NET SynchronizationContext where state like 'source' is saved and restored when yielding with such set.

@blattersturm blattersturm reopened this Jun 23, 2022
@blattersturm blattersturm changed the title source variable in lua becomes nil if any time passes during event handler Unintuitive scripting behavior: source variable in Lua becomes nil if any time passes during event handler Jun 23, 2022
@gottfriedleibniz gottfriedleibniz added the ScRT: Lua Issues/PRs related to the Lua scripting runtime label Feb 1, 2024
@Lucas7yoshi Lucas7yoshi closed this as not planned Won't fix, can't repro, duplicate, stale Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ScRT: Lua Issues/PRs related to the Lua scripting runtime
Projects
None yet
Development

No branches or pull requests

4 participants