GetControlState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 1: Line 1:
__NOTOC__  
{{Server client function}}
__NOTOC__
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.
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.


Line 12: Line 13:


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


==Example==   
==Example==   
This example starts a repeating check when a player spawns, if a player presses the fire key, they'll be killed.  
This example starts a repeating check when a player spawns, if a player presses the fire key, they'll be killed.  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerSpawn", root, "onPlayerSpawn" )
function onPlayerSpawn ( theSpawnpoint )
function onPlayerSpawn ( theSpawnpoint )
  killPlayerIfTheyPressThisKey ( source, "fire" ) -- start a repeating check
    killPlayerIfTheyPressThisKey ( source, "fire" ) -- start a repeating check
end
end
addEventHandler ( "onPlayerSpawn", root, onPlayerSpawn )


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 )
        outputChatBox ( "Violence will not be tolerated!", thePlayer )
    killPlayer ( thePlayer ) -- kill them
        killPlayer ( thePlayer )                         -- kill them
  else -- otherwise..
    else                                                 -- otherwise..
    setTimer ( "killPlayerIfTheyPressThisKey", 500, 1, thePlayer, key ) -- call this function again in 500ms
        setTimer ( "killPlayerIfTheyPressThisKey", 500, 1, thePlayer, key ) -- call this function again in 500ms
  end
    end
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:02, 20 August 2007

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 control states are sent to the server 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. For increased accuracy (and also increased bandwidth usage) use bindKey instead to bind a GTA control name to a console function.

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. See control names for a list of possible controls.

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.

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

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

See Also