GetLocalPlayer: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Improved first example, added new one)
No edit summary
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Client function}}
__NOTOC__
__NOTOC__
This function gets the player element of the client running the current script.
This function gets the player element of the client running the current script.  
 
{{New feature/item|3.0110|1.1||
You should use predefined variable '''localPlayer''' instead of typing getLocalPlayer() for better readability.
}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
player getLocalPlayer ()
player getLocalPlayer ( )
</syntaxhighlight>
</syntaxhighlight>


===Returns===
===Returns===
Returns a [[player]] element.
Returns the local [[player]] element.


==Example==  
==Example==  
'''Example 1:''' This clientside function outputs the player's current location to the console.
'''Example 1:''' This client side function outputs the player's current location to the console.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function outputLocalPlayerPosition ()
function outputLocalPlayerPosition()
--we get the local player
-- get the local player's position
local localPlayer = getLocalPlayer ()
local px, py, pz = getElementPosition(localPlayer)
--we get the local player's position
-- output it to the console
local px, py, pz = getElementPosition ( localPlayer )
outputConsole("Your location: "..px.." "..py .." "..pz)
--we output it to the console
outputConsole ( "Your location: "..px.." "..py.." "..pz )
end
end
</syntaxhighlight>
</syntaxhighlight>


'''Example 2:''' This clientside script makes the local player's camera flash red after being hit.
'''Example 2:''' This client side script makes the local player's camera flash red after being hit.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function flashRed ()
function flashRed()
--we fade the local player's camera red during a second
-- fade out the local player's camera to red during a second
fadeCamera( false, 1.0, 255, 0, 0 )
fadeCamera(false, 1.0, 255, 0, 0)
--we set a 1000 ms (1 sec) timer to fade it back out
-- set a 500 ms (0.5 sec) timer to fade it back in before it has completely faded out
setTimer( fadeCamera, 1000, 1, true, 1.0 )
setTimer(fadeCamera, 500, 1, true, 1.0)
end
end
--we attach our 'flashRed' function to be a handler of "onClientPlayerDamage" when its source (that is, the hit player) is the local player
-- we attach our 'flashRed' function to be a handler of "onClientPlayerDamage" when its source (that is, the hit player) is the local player
addEventHandler( "onClientPlayerDamage", getLocalPlayer(), flashRed )
addEventHandler("onClientPlayerDamage", localPlayer, flashRed)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Client_player_functions}}
{{Client_player_functions}}
[[ru:getLocalPlayer]]

Latest revision as of 16:19, 8 April 2024

This function gets the player element of the client running the current script.

You should use predefined variable localPlayer instead of typing getLocalPlayer() for better readability.

Syntax

player getLocalPlayer ( )

Returns

Returns the local player element.

Example

Example 1: This client side function outputs the player's current location to the console.

function outputLocalPlayerPosition()
	-- get the local player's position
	local px, py, pz = getElementPosition(localPlayer)
	-- output it to the console
	outputConsole("Your location: "..px.." "..py .." "..pz)
end

Example 2: This client side script makes the local player's camera flash red after being hit.

function flashRed()
	-- fade out the local player's camera to red during a second
	fadeCamera(false, 1.0, 255, 0, 0)
	-- set a 500 ms (0.5 sec) timer to fade it back in before it has completely faded out
	setTimer(fadeCamera, 500, 1, true, 1.0)
end
-- we attach our 'flashRed' function to be a handler of "onClientPlayerDamage" when its source (that is, the hit player) is the local player
addEventHandler("onClientPlayerDamage", localPlayer, flashRed)

See Also