Jump to content

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.

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.

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

Edited by Mobbstar

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...