Sign in to follow this  
.:HyPeX:.

addEventHandler warning

Recommended Posts

Hello guys i've done this way to check something, wich was handy, but i'm not sure how to avoid the debug error (It still works perfectly thought)

  
addCommandHandler("turnOffMapInfo", function() 
local event = addEventHandler("onClientRender",root, MapInfo) 
    if event ~= true then 
    removeEventHandler("onClientRender", root, MapInfo) 
    end 
end) 
bindKey("F3", "down", "turnOffMapInfo") 

Share this post


Link to post

you have us this:

function isEventHandlerAdded( sEventName, pElementAttachedTo, func ) 
    if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then 
        local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo ) 
        if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then 
            for i, v in ipairs( aAttachedFunctions ) do 
                if v == func then 
                    return true 
                end 
            end 
        end 
    end 
    return false 
end 

Share this post


Link to post
  
local event = false 
  
addCommandHandler("turnOffMapInfo",  
    function() 
        if not event then 
            addEventHandler("onClientRender", root, MapInfo) 
        else 
            removeEventHandler("onClientRender", root, MapInfo) 
        end 
        event = not event 
    end 
) 
bindKey("F3", "down", "turnOffMapInfo") 

Share this post


Link to post
  
function isEventHandlerAdded (name, element, func) 
    if type(name) ~= "string" or not isElement(element) or type(func) ~= "function" then return end 
    for i, v in ipairs(getEventHandlers(name, element)) do 
        if v == func then return true end 
    end 
end 
-- 
addCommandHandler("turnOffMapInfo", 
    function() 
        local name = "onClientRender" 
        local element = root 
        local func = MapInfo 
        return ((isEventHandlerAdded(name, element, func) and removeEventHandler(name, element, func)) or addEventHandler(name, element, func)) 
    end 
) 
bindKey("F3", "down", "turnOffMapInfo") 
  

Either way, you shouldn't have used "event ~= true" that way since addEventHandler returns true if the event handler was attached successfully or returns false if the specified event could not be found or any parameters were invalid.

Share this post


Link to post

There's no need to go that far over-the-top for such a simple thing, just use a variable - simple.

local isMapInfoActive = false; 
  
function mapInfo_Handler() 
    if(isMapInfoActive == false) then 
        addEventHandler("onClientRender", root, MapInfo); 
        isMapInfoActive = true; 
    else 
        removeEventHandler("onClientRender", root, MapInfo); 
        isMapInfoActive = false; 
    end 
end 
addCommandHandler("TurnOffMapInfo", mapInfo_Handler, false); -- False means it will not be case-sensitive 
bindKey("F3", "down", mapInfo_Handler); 

Share this post


Link to post
There's no need to go that far over-the-top for such a simple thing, just use a variable - simple.
local isMapInfoActive = false; 
  
function mapInfo_Handler() 
    if(isMapInfoActive == false) then 
        addEventHandler("onClientRender", root, MapInfo); 
        isMapInfoActive = true; 
    else 
        removeEventHandler("onClientRender", root, MapInfo); 
        isMapInfoActive = false; 
    end 
end 
addCommandHandler("TurnOffMapInfo", mapInfo_Handler, false); -- False means it will not be case-sensitive 
bindKey("F3", "down", mapInfo_Handler); 

That's what I did lol

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.