Jump to content

How to check if a commandHandler already exist?


WiBox

Recommended Posts

example I want to check if 

addCommandHandler("usemedkit", usemedkits) 

already exist example:

if ( addCommandHandler("usemedkit", usemedkits) ) then

    removeCommandHandler("usemedkit")

elseif ( removeCommandHandler("usemedkit" ) then

    addCommandHandler("usemedkit, usemedkits)

end

I know that this 5 lines don't work  but if someone can give me a hint how to check if a specific command is already exist I would appreciate that ^_^

Link to comment

Or you can run a local variable with this. I mean, when you add this command, give a variable the value true. when the command is removed, give it the value false.

local isActive = false -- our local variable. it's currently in false state

if ( isActive == true ) then -- if variable's value = true

    removeCommandHandler("usemedkit") -- remove command
    isActive = false -- and value of variable is false

elseif ( isActive == false ) then -- if variable's value already false

    addCommandHandler("usemedkit", usemedkits) -- then add command --
    isActive = true -- variable is now true state --

end

 

Link to comment

Dude in my last topic, I can make with Timer if this command is removed then it get added and if it wasn't removed then I kill the Timer but the thing is you gave me as a variable and I don't know how to use a variable to check if it's removed or not.. Kind of hard to me as I'm new at coding.. example 

local isActive = faslse

if ( isActive == true ) then

    removeCommandHandler("usemedkit")

    isActive = false

elseif ( isActive == false ) then

    addCommandHandler("usemedkit", usemedkits)

    isActive = true

end

                it means like do:

if ( isActive == true ) then

    killTimer(Timer10)

elseif ( isActive == false ) then

    addCommandHandler("usemedkit", usemedkits)

    killTimer(Timer10)

end

something like that?

 

Link to comment
1 hour ago, SSKE said:

Dude in my last topic, I can make with Timer if this command is removed then it get added and if it wasn't removed then I kill the Timer but the thing is you gave me as a variable and I don't know how to use a variable to check if it's removed or not.. Kind of hard to me as I'm new at coding.. example 

                it means like do:

if ( isActive == true ) then

    killTimer(Timer10)

elseif ( isActive == false ) then

    addCommandHandler("usemedkit", usemedkits)

    killTimer(Timer10)

end

something like that?

 

when isActive == true , it means that command is added. And isActive == false means commands is not added. When you add the command using addCommandHandler, you should change the value of isActive to true. when removing the command, value of isActive should change to false. isActive variable is created by us. So, it doesn't change automatically. we should manually change it, Like below.

local isActive = false -- initial value of "isActive"

if ( isActive == true ) then -- if true

    killTimer(Timer10) -- function
  
    isActive = false -- change value to false

elseif ( isActive == false ) then --else if false

    addCommandHandler("usemedkit", usemedkits) -- function

    killTimer(Timer10)
  
    isActive = true  -- change valu to true

end

 

Link to comment
Timer10 = setTimer(
    function ()
        for _,plrs in ipairs(getElementsByType("player")) do
            if ( getElementDimension(plrs) == 0 ) then
                addCommandHandler("usemedkit", usethemedkit)  
                addCommandHandler("acceptmedkit", healOthers)
                addEventHandler("meds.giveMedKit", root, giveMedKit)
                addEventHandler("meds.useMedKitOnOther", root, usemedkitanother)
            end
        end
    end, 1000, 0)

For I feel that I made myself clearxD, In this code I want to add if;

addEventHandler("meds.useMedKitOnOther", root, usemedkitanother) 

If it's already handled! Is there a way that I check if a Event Handler already handled?

Because It seems that which spamming is those addEventHandler if there is a way I can check if they are already handled it will help so much, and thanks for all who helped me here ^_^ 

Edited by SSKE
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...