Jump to content

How to remove "open" event handlers?


Recommended Posts

Hey everyone! I have a question for scripters. How can I remove event handlers written like this?

addEventHandler("onClientPreRender", root, 
      function() 
          -- script stuff 
      end 
) 

Or how to call it in another resource? I would be grateful if someone could help me, I'm really interested in this. Thank you!

Link to comment
Hey everyone! I have a question for scripters. How can I remove event handlers written like this?
addEventHandler("onClientPreRender", root, 
      function() 
          -- script stuff 
      end 
) 

Or how to call it in another resource? I would be grateful if someone could help me, I'm really interested in this. Thank you!

As you can see there is no function defined for your onClientPreRender event. So what you got to do is that define the function something like this:

  
      function yourFunctionName () 
          -- script stuff 
      end      
       addEventHandler("onClientPreRender", root,yourFunctionName) 
  
  

Now since you've defined your function, you can use removeEventHandler. I doubt there is any other way of getting this done. Is there ? :3 Probably someone else might come up with an alternative. Well, you could also define a particular global or local variable such as (local allowRender or allowRender = false) in the beggining of your code and accordingly play around by using the two boolean values to use it in place of removeEventHandler. But this seems ineffecient as hell doesn't it cause your function will be attached to the onClientPreRender event permanently plus it's a silly thing to do imo. I can't think of anything other than that lol.

And for your 2nd issue, export your defined function and then either use "call" or "exports" to call it from another resource.

Link to comment

A little piece of code from my lobby script:

  
  
local EventNHandlers = { } 
local _functions = { } 
  
_addEventHandler = addEventHandler 
addEventHandler = function (eventName, attachedTo, handlerFunction) 
    if handlerFunction then 
        if event == "onClientPreRender" then 
            setTimer( 
            function() 
                handlerFunction() 
            end,2000,1) 
        elseif event == "onClientRender" then 
            setTimer( 
            function() 
                handlerFunction() 
            end,2000,1) 
        elseif event == "onClientHUDRender" then 
setTimer( 
            function() 
                handlerFunction() 
            end,2000,1) 
        else 
            if _addEventHandler(eventName, attachedTo, handlerFunction) then 
                EventNHandlers[#EventNHandlers+1] = {eventName, attachedTo, handlerFunction} 
            end 
        end 
        if not _functions[handlerFunction] then 
            _functions[handlerFunction] = true -- set it to nil when you want to remove the event handler 
        end 
    end 
end 
  

--- If you want to remove then then loop through the tables ^

  
for k,v in ipairs(EventNHandlers) do 
removeEventHandler(EventNHandlers[k][1],getRootElement(),EventNHandlers[k][3])  end 
  
    for k,v in pairs(_functions) do 
       k = nil -- set the function to nil now 
    end 

Remember this will insert all event handlers in your current script (file).

Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...