GetControlState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
m (This function is shared)
Tag: Manual revert
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Server client function}}
{{Shared function}}
__NOTOC__
__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 6: Line 6:


==Syntax==  
==Syntax==  
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">bool getControlState ( player thePlayer, string controlName )</syntaxhighlight>  
<syntaxhighlight lang="lua">bool getControlState ( player thePlayer, string controlName )</syntaxhighlight>  


Line 12: Line 11:
*'''thePlayer:''' The player you wish to get the control state of. Do not use this parameter when scripting for client.
*'''thePlayer:''' The player you wish to get the control state of. Do not use this parameter when scripting for client.
*'''controlName:''' The control that you want to get the state of. See [[control names]] for a list of possible controls.
*'''controlName:''' The control that you want to get the state of. See [[control names]] for a list of possible controls.
'''Note:''' several controls are not synched with the server, therefore the function will always return ''false'' for these controls serverside. The nonsynched controls are:
'''Note:''' several controls are not synched with the server, therefore the function will always return ''false'' for these controls serverside. These controls are:
*next_weapon
*next_weapon
*previous_weapon
*previous_weapon
Line 24: Line 23:
*group_control_forwards
*group_control_forwards
*group_control_back
*group_control_back
*sub_mission
*sub_mission
*radio_next
*radio_next
Line 33: Line 31:
*vehicle_mouse_look
*vehicle_mouse_look
*special_control_*
*special_control_*
</section>
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">bool getControlState ( string controlName ) </syntaxhighlight>
===Required Arguments===
*'''controlName:''' The control that you want to get the state of. See [[control names]] for a list of possible controls.
</section>


===Returns===
===Returns===
Returns the state of the control if found, ''false'' otherwise.
Returns the state of the control, ''false'' if the control doesn't exist or if the player is dead.


==Example==   
==Example==   
<section name="Server" class="server" show="true">
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">
Line 57: Line 46:
     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 tolerated!", thePlayer )
         outputChatBox ( "Violence will not be tolerated!", thePlayer )
         killPlayer ( thePlayer )                          -- kill them
         killPed ( 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
Line 63: Line 52:
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>
 
==Changelog==
{{ChangelogHeader}}
{{ChangelogItem|1.5.5-3.11427|Deprecated client-side. Use [[setPedControlState]] and [[getPedControlState]] client-side.}}


==See Also==
==See Also==
{{Input functions}}
{{Input functions}}

Latest revision as of 00:00, 30 April 2024

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

Syntax

bool getControlState ( player thePlayer, string controlName )

Required Arguments

  • thePlayer: The player you wish to get the control state of. Do not use this parameter when scripting for client.
  • controlName: The control that you want to get the state of. See control names for a list of possible controls.

Note: several controls are not synched with the server, therefore the function will always return false for these controls serverside. These controls are:

  • next_weapon
  • previous_weapon
  • jump
  • zoom_in
  • zoom_out
  • look_behind
  • change_camera
  • conversation_yes
  • conversation_no
  • group_control_forwards
  • group_control_back
  • sub_mission
  • radio_next
  • radio_previous
  • vehicle_look_left
  • vehicle_look_right
  • vehicle_look_behind
  • vehicle_mouse_look
  • special_control_*

Returns

Returns the state of the control, false if the control doesn't exist or if the player is dead.

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 )
        killPed ( thePlayer )                          -- kill them
    else                                                  -- otherwise..
        setTimer ( killPlayerIfTheyPressThisKey, 500, 1, thePlayer, key ) -- call this function again in 500ms
    end
end

Changelog

Version Description
1.5.5-3.11427 Deprecated client-side. Use setPedControlState and getPedControlState client-side.

See Also