You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the following example the "handle" value has an ID and unsubscribing works :
local handle = 0
local function _OnStateChange(e)
if e.ToState == "Running" then
_P("Handle=")
_D(handle)
Ext.Events.GameStateChanged:Unsubscribe(handle)
end
end
handle = Ext.Events.GameStateChanged:Subscribe(_OnStateChange)
In the following case "null" is displayed :
local handle = Ext.Events.GameStateChanged:Subscribe(function(e)
if e.ToState == "Running" then
_P("Handle=")
_D(handle)
Ext.Events.GameStateChanged:Unsubscribe(handle)
end
end)
If this is not a bug and an expected behavior it would be nice to have a way to unsubcribe with something like "Ext.Events.GameStateChanged:Unsubscribe()" without parameter or e.handle when called inside the event handler.
The text was updated successfully, but these errors were encountered:
d3xMachina
changed the title
[BUG ?] Variable scoping
[BUG ?] Variable scope in event handler
Sep 4, 2023
Not a bug, this is how the language works: https://www.lua.org/pil/6.1.html
In your second example, local handle is later in the execution order than the closure, and the closure will not capture handle as an upvalue.
Ext.Events.GameStateChanged:Unsubscribe() will not get a version that deletes all subscribers, since it would affect all other mods too and would end up breaking others' code.
In the following example the "handle" value has an ID and unsubscribing works :
In the following case "null" is displayed :
If this is not a bug and an expected behavior it would be nice to have a way to unsubcribe with something like "Ext.Events.GameStateChanged:Unsubscribe()" without parameter or e.handle when called inside the event handler.
The text was updated successfully, but these errors were encountered: