Jump to content

Removing a specific event from the EventCallback list.


Kzisor

Recommended Posts

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	endend

I hope this helps anyone who wants to remove specific events.

Link to comment
Share on other sites

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
Share on other sites

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 :razz:.

Link to comment
Share on other sites

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 :razz:.

 

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...