RemoveCommandHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Needs_Checking|Problems in example. [[User:Erorr404|Erorr404]]}}
{{Server client function}}
{{Needs_Checking|Well, I fixed the syntax issues, but its a bad example (why remove the command for everyone?). Can't think of another one though... [[User:EAi|eAi]]}}
 
__NOTOC__
__NOTOC__
This function removes a command handler, that is one that has been added using addCommandHandler. This function can only remove command handlers that were added by the [[virtual machine]] that it is called in.
This function removes a command handler, that is one that has been added using [[addCommandHandler]]. This function can only remove command handlers that were added by the resource that it is called in.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool removeCommandHandler ( string commandName )               
bool removeCommandHandler ( string commandName [, function handler] )               
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''commandName:''' The string of the command you wish to remove.
*'''commandName:''' the name of the command you wish to remove.
 
===Optional Arguments===
*'''handler:''' the specific handler function to remove. If not specified, all handler functions for the command (from the calling resource) will be removed. ''This argument is only available in the server.''


===Returns===
===Returns===
Line 17: Line 18:


==Example==  
==Example==  
<section name="Server" class="server" show="true">
This example adds a command handler that briefly shows the position of 'huntedPlayer', and removes the command handler when 'huntedPlayer' dies:
This example adds a command handler that briefly shows the position of 'huntedPlayer', and removes the command handler when 'huntedPlayer' dies:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- add a command that allows players to see the position of the 'huntedPlayer' for 5 seconds:
-- add a command that allows players to see the position of the 'huntedPlayer' for 5 seconds:
addCommandHandler ( "showhuntedblip", "consoleShowHuntedBlip" )
function consoleShowHuntedBlip ( thePlayer, commandName )
function consoleShowHuntedBlip ( player, commandName )
    local x, y, z = getElementPosition ( huntedPlayer )
  if ( player ) then
    local huntedblip = createBlip ( x, y, z, 0, 2, 255, 0, 0, 255, thePlayer )
      local x, y, z = getElementPosition ( huntedPlayer )
    setTimer ( "destroyElement", 5000, 1, huntedblip )
      local blip = createBlip ( x, y, z, 0, 2, 255, 0, 0, 255, player )
      setTimer ( "destroyElement", 5000, 1, blip )
  end
end
end
addCommandHandler ( "showhuntedblip", consoleShowHuntedBlip )


-- remove the command once the hunter player dies:
-- remove the command once the hunter player dies:
addEventHandler ( "onPlayerWasted", huntedPlayer, "onHuntedPlayerWasted" )
function onHuntedPlayerWasted ( ammo, killer, killerweapon, bodypart )
function onHuntedPlayerWasted ( ammo, killer, killerweapon, bodypart )
  removeCommandHandler ( "showhuntedblip" )
    removeCommandHandler ( "showhuntedblip" )
end
end
addEventHandler ( "onPlayerWasted", huntedPlayer, onHuntedPlayerWasted )
</syntaxhighlight>
</syntaxhighlight>
</section>
<section name="Client" class="client" show="true">
This example adds a command handler that briefly shows the position of 'huntedPlayer', and removes the command handler when 'huntedPlayer' dies:
<syntaxhighlight lang="lua">
-- add a command that allows players to see the position of the 'huntedPlayer' for 5 seconds:
function consoleShowHuntedBlip ( commandName )
    local x, y, z = getElementPosition ( huntedPlayer )
    local huntedblip = createBlip ( x, y, z, 0, 2, 255, 0, 0, 255, thePlayer )
    setTimer ( "destroyElement", 5000, 1, huntedblip )
end
addCommandHandler ( "showhuntedblip", consoleShowHuntedBlip )


-- remove the command once the hunter player dies:
function onHuntedPlayerWasted ( killer, killerweapon, bodypart )
    removeCommandHandler ( "showhuntedblip" )
end
addEventHandler ( "onClientPlayerWasted", huntedPlayer, onHuntedPlayerWasted )
</syntaxhighlight>
</section>
==See Also==
==See Also==
{{Server_functions}}
{{Server_functions}}

Latest revision as of 01:59, 9 July 2011

This function removes a command handler, that is one that has been added using addCommandHandler. This function can only remove command handlers that were added by the resource that it is called in.

Syntax

bool removeCommandHandler ( string commandName [, function handler] )              

Required Arguments

  • commandName: the name of the command you wish to remove.

Optional Arguments

  • handler: the specific handler function to remove. If not specified, all handler functions for the command (from the calling resource) will be removed. This argument is only available in the server.

Returns

Returns true if the command handler was removed successfully, false if the command doesn't exist.

Example

Click to collapse [-]
Server

This example adds a command handler that briefly shows the position of 'huntedPlayer', and removes the command handler when 'huntedPlayer' dies:

-- add a command that allows players to see the position of the 'huntedPlayer' for 5 seconds:
function consoleShowHuntedBlip ( thePlayer, commandName )
    local x, y, z = getElementPosition ( huntedPlayer )
    local huntedblip = createBlip ( x, y, z, 0, 2, 255, 0, 0, 255, thePlayer )
    setTimer ( "destroyElement", 5000, 1, huntedblip )
end
addCommandHandler ( "showhuntedblip", consoleShowHuntedBlip )

-- remove the command once the hunter player dies:
function onHuntedPlayerWasted ( ammo, killer, killerweapon, bodypart )
    removeCommandHandler ( "showhuntedblip" )
end
addEventHandler ( "onPlayerWasted", huntedPlayer, onHuntedPlayerWasted )
Click to collapse [-]
Client

This example adds a command handler that briefly shows the position of 'huntedPlayer', and removes the command handler when 'huntedPlayer' dies:

-- add a command that allows players to see the position of the 'huntedPlayer' for 5 seconds:
function consoleShowHuntedBlip ( commandName )
    local x, y, z = getElementPosition ( huntedPlayer )
    local huntedblip = createBlip ( x, y, z, 0, 2, 255, 0, 0, 255, thePlayer )
    setTimer ( "destroyElement", 5000, 1, huntedblip )
end
addCommandHandler ( "showhuntedblip", consoleShowHuntedBlip )

-- remove the command once the hunter player dies:
function onHuntedPlayerWasted ( killer, killerweapon, bodypart )
    removeCommandHandler ( "showhuntedblip" )
end
addEventHandler ( "onClientPlayerWasted", huntedPlayer, onHuntedPlayerWasted )

See Also