SetAnalogControlState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{Needs Example}}
This sets the analog control state of a control for the local player. To change the analog controls for a [[ped]], please use [[setPedAnalogControlState]].
This sets the analog control state of a control.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua"> bool setAnalogControlState ( string controlName , float state ) </syntaxhighlight>
<syntaxhighlight lang="lua">bool setAnalogControlState ( string control [, float state, bool forceOverrideNextFrame = false ] ) </syntaxhighlight>
===Required Arguement===
===Required Arguments===
* '''controlName''': the control name you wish to set the analog state of. Here's a list:
*'''control:''' The control that you want to set the state of. See [[control names]] for a list of possible controls.
**'''left'''
 
**'''right'''
===Optional Arguments===
**'''forwards'''
*'''state:''' A [[float]] between 0 and 1 indicating the amount the control is pressed. If no value is provided, the analog control is removed.
**'''backwards'''
{{Added feature/item|1.5.9|1.5.8|20756|
**'''vehicle_left'''
*'''forceOverrideNextFrame: ''' A [[bool]] indicating if the player input should force fully overriden for the next frame.
**'''vehicle_right'''
}}
**'''steer_forward'''
**'''steer_back'''
**'''accelerate'''
**'''brake_reverse'''
**'''special_control_left'''
**'''special_control_right'''
**'''special_control_up'''
**'''special_control_down'''
* '''state''': a float between 0 and 1 indicating the amount the control is pressed.


===Returns===
===Returns===
Returns true, else false if invalid argument.
Returns ''true'' if the control state was successfully set, ''false'' otherwise.


==Example==
==Example==
<section name="Client" class="client" show="true">
This creates an ''/forwards'' command, which toggles your ''forwards'' control state between 0 and 1.
/analog command starts to go forward control state if you are not, if you are going forward by control state then you will stop.
<syntaxhighlight lang="lua">
addCommandHandler( "forwards",
    function( )
        if ( getAnalogControlState( "forwards" ) == 0 ) then
            setAnalogControlState( "forwards", 1 )
        else
            setAnalogControlState( "forwards", 0 )
        end
    end
)
</syntaxhighlight>
 
This script invertes left and right vehicle steering for the player.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function analog()
addEventHandler("onClientPreRender", root,
if (getAnalogControlState("forwards") == 0) then
    function()
setAnalogControlState ("forwards",1)
        local right = getAnalogControlState("vehicle_right", true)
else
        local left = getAnalogControlState("vehicle_left", true)
setAnalogControlState ("forwards",0)
       
end
        if right > left then
end
            setAnalogControlState("vehicle_left", right, true)
addCommandHandler("analog",analog)
        else
            setAnalogControlState("vehicle_right", left, true)
        end
    end
)
</syntaxhighlight>
</syntaxhighlight>
This example written by '''Samurai'''
</section>


==See Also==
==See Also==
{{Client input functions}}
{{Client input functions}}

Latest revision as of 21:06, 23 September 2021

This sets the analog control state of a control for the local player. To change the analog controls for a ped, please use setPedAnalogControlState.

Syntax

bool setAnalogControlState ( string control [, float state, bool forceOverrideNextFrame = false ] ) 

Required Arguments

  • control: The control that you want to set the state of. See control names for a list of possible controls.

Optional Arguments

  • state: A float between 0 and 1 indicating the amount the control is pressed. If no value is provided, the analog control is removed.
  • forceOverrideNextFrame: A bool indicating if the player input should force fully overriden for the next frame.

Returns

Returns true if the control state was successfully set, false otherwise.

Example

This creates an /forwards command, which toggles your forwards control state between 0 and 1.

addCommandHandler( "forwards",
    function( )
        if ( getAnalogControlState( "forwards" ) == 0 ) then
            setAnalogControlState( "forwards", 1 )
        else
            setAnalogControlState( "forwards", 0 )
        end
    end
)

This script invertes left and right vehicle steering for the player.

addEventHandler("onClientPreRender", root,
    function()
        local right = getAnalogControlState("vehicle_right", true)
        local left = getAnalogControlState("vehicle_left", true)
        
        if right > left then
            setAnalogControlState("vehicle_left", right, true)
        else
            setAnalogControlState("vehicle_right", left, true)
        end
    end
)

See Also