Kzisor Posted December 15, 2014 Share Posted December 15, 2014 I just wanted to document this for everyone so they would know how to do it if they have questions. The basic principal was to modify the RemoveAllEventCallbacks to only search for specific events and/or specific prefabs for said event. Here is the code:--[[ RemoveEventCallback ]]RemoveEventCallback = function(inst, event, prefab) if inst.event_listening then for e, sources in pairs(inst.event_listening) do if e == event then for s, fns in pairs(sources) do if prefab == nil then if s.event_listeners then local listeners = s.event_listeners[e] if listeners then listeners[inst] = nil end end end if s.prefab == prefab then if s.event_listeners then local listeners = s.event_listeners[e] if listeners then listeners[inst] = nil end end end end end end end if inst.event_listeners then for e, listeners in pairs(inst.event_listeners) do if e == event then for listener, fns in pairs(listeners) do if listener.event_listening then local sources = listener.event_listening[e] if sources then sources[inst] = nil end end end end end endendI hope this helps anyone who wants to remove specific events. Link to comment https://forums.kleientertainment.com/forums/topic/46524-removing-a-specific-event-from-the-eventcallback-list/ Share on other sites More sharing options...
Mobbstar Posted December 16, 2014 Share Posted December 16, 2014 Neat. But I'd change those two "if ... then if ... then" to "if ... and ... then" to make the thing beetle less. Link to comment https://forums.kleientertainment.com/forums/topic/46524-removing-a-specific-event-from-the-eventcallback-list/#findComment-587404 Share on other sites More sharing options...
Kzisor Posted December 16, 2014 Author Share Posted December 16, 2014 Neat. But I'd change those two "if ... then if ... then" to "if ... and ... then" to make the thing beetle less. I agree that optimizing the code would probably be more beneficial, however, I wanted to show proof of concept with working code in a more legible manner for those which didn't have much programming experience. Link to comment https://forums.kleientertainment.com/forums/topic/46524-removing-a-specific-event-from-the-eventcallback-list/#findComment-587604 Share on other sites More sharing options...
Mobbstar Posted December 16, 2014 Share Posted December 16, 2014 (edited) I agree that optimizing the code would probably be more beneficial, however, I wanted to show proof of concept with working code in a more legible manner for those which didn't have much programming experience. Yeah, I'm just bugged by easily compressable information (EDIT: or rather long if-for combinations in general), it's a pet peeve. Other than that, the code looks nice.EDIT:It seems obvious now, that you meant this to be solely educating, as you refrain of using _ as the variable dump (for example). To be fair, it wouldn't make much difference here, there's only one unused variable name anyways. It wouldn't make much difference in general . Edited December 16, 2014 by Mobbstar Link to comment https://forums.kleientertainment.com/forums/topic/46524-removing-a-specific-event-from-the-eventcallback-list/#findComment-587610 Share on other sites More sharing options...
Kzisor Posted December 17, 2014 Author Share Posted December 17, 2014 Yeah, I'm just bugged by easily compressable information (EDIT: or rather long if-for combinations in general), it's a pet peeve. Other than that, the code looks nice.EDIT:It seems obvious now, that you meant this to be solely educating, as you refrain of using _ as the variable dump (for example). To be fair, it wouldn't make much difference here, there's only one unused variable name anyways. It wouldn't make much difference in general . Here is the actual one that I use, let me know if there is anyway to optimize it more.--[[ RemoveEventCallback ]]RemoveEventCallback = function(inst, event, prefab) if inst.event_listening then for e, sources in pairs(inst.event_listening) do if e == event then for s, _ in pairs(sources) do if prefab == nil and s.event_Listeners and s.event_listeners[e] then s.event_listeners[e][inst] = nil elseif s.prefab == prefab and s.event_listeners and s.event_listeners[e] then s.event_listeners[e][inst] = nil end end end end end if inst.event_listeners then for e, listeners in pairs(inst.event_listeners) do if e == event then for listener, _ in pairs(listeners) do if listener.event_listening and listener.event_listening[e] then listener.event_listening[e][inst] = nil end end end end endend Link to comment https://forums.kleientertainment.com/forums/topic/46524-removing-a-specific-event-from-the-eventcallback-list/#findComment-587954 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now