Jump to content

addEventHandler warning


.:HyPeX:.

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") 

Link to comment

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 

Link to comment
  
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") 

Link to comment
  
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.

Link to comment

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); 

Link to comment
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

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