GetControlState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
Gets a state of a specified player's control.<br/>
This function will check if a player is pressing a particular control. Controls are those that affect GTA. If you wish to get the state of another key, use [[bindKey]] and a command function.
Note: Only works for the keys MTA synchronises between clients.
 
Note: Not all keys are syncronized between clients at all times, as such their state may be given incorrectly. As a rule, keys that move or affect the player or their vehicle are most likely to be accurate.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">bool getControlState ( player thePlayer, string control ) </syntaxhighlight>  
<syntaxhighlight lang="lua">bool getControlState ( player thePlayer, string controlName ) </syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''thePlayer:''' The player you wish to get the control state of.
*'''thePlayer:''' The player you wish to get the control state of.
*'''control:''' The control that you want to get the state of.
*'''controlName:''' The control that you want to get the state of.


===Returns===
===Returns===
Line 23: Line 24:
function killPlayerIfTheyPressThisKey ( thePlayer, key )
function killPlayerIfTheyPressThisKey ( thePlayer, key )
   if ( getControlState ( thePlayer, key ) ) then -- if they're pressing the fire key
   if ( getControlState ( thePlayer, key ) ) then -- if they're pressing the fire key
    outputChatBox ( "Violence will not be tollerated!", thePlayer )
     killPlayer ( thePlayer ) -- kill them
     killPlayer ( thePlayer ) -- kill them
   else -- otherwise..
   else -- otherwise..

Revision as of 18:13, 8 July 2006

This function will check if a player is pressing a particular control. Controls are those that affect GTA. If you wish to get the state of another key, use bindKey and a command function.

Note: Not all keys are syncronized between clients at all times, as such their state may be given incorrectly. As a rule, keys that move or affect the player or their vehicle are most likely to be accurate.

Syntax

bool getControlState ( player thePlayer, string controlName ) 

Required Arguments

  • thePlayer: The player you wish to get the control state of.
  • controlName: The control that you want to get the state of.

Returns

Returns the state of the control if found, 'false' otherwise.

Example

This example starts a repeating check when a player spawns, if a player presses the fire key, they'll be killed.

addEventHandler ( "onPlayerSpawn", root, "onPlayerSpawn" )
function onPlayerSpawn ( theSpawnpoint )
  killPlayerIfTheyPressThisKey ( source, "fire" ) -- start a repeating check
end

function killPlayerIfTheyPressThisKey ( thePlayer, key )
  if ( getControlState ( thePlayer, key ) ) then -- if they're pressing the fire key
    outputChatBox ( "Violence will not be tollerated!", thePlayer )
    killPlayer ( thePlayer ) -- kill them
  else -- otherwise..
    setTimer ( "killPlayerIfTheyPressThisKey", 500, 1, thePlayer, key ) -- call this function again in 500ms
  end
end

See Also