SetPedAnimation: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Updated issues)
(Fix memory leak in example)
Line 48: Line 48:
This example makes the player sit down and stand up using the command /sit.
This example makes the player sit down and stand up using the command /sit.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local sitting = {}
local playerSitState = {}
 
function toggleSit(thePlayer)
function toggleSit(thePlayer)
if not sitting[thePlayer] then
local playerSitting = playerSitState[thePlayer]
 
if (not playerSitting) then
setPedAnimation(thePlayer, "ped", "seat_down", -1, false, false, false, false)
setPedAnimation(thePlayer, "ped", "seat_down", -1, false, false, false, false)
-- Store the player state in the table
playerSitState[thePlayer] = true -- store the player state in the table
                sitting[thePlayer] = true
 
else
return true
-- If you use again this command then your character stand up
setPedAnimation(thePlayer)
                -- Delete player from table
sitting[thePlayer] = nil
end
end
setPedAnimation(thePlayer) -- if you use again this command then your character stand up
playerSitState[thePlayer] = nil -- remove player sit state from table
end
end
addCommandHandler("sit", toggleSit)
addCommandHandler("sit", toggleSit)
function onPlayerQuitClearSitState()
playerSitState[source] = nil -- clear state at player quit, to prevent memory leak
end
addEventHandler("onPlayerQuit", root, onPlayerQuitClearSitState)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 14:01, 16 December 2023

Sets the current animation of a player or ped. Not specifying the type of animation will automatically cancel the current one.

[[|link=|]] Warning: It is possible that an animation will be cancelled if you use setElementFrozen on the ped, but this does not happen all the time.

Syntax

bool setPedAnimation ( ped thePed [, string block = nil, string anim = nil, int time = -1, bool loop = true, bool updatePosition = true,
                       bool interruptable = true, bool freezeLastFrame = true, int blendTime = 250, bool retainPedState = false ] )

OOP Syntax Help! I don't understand this!

Method: ped:setAnimation(...)
Counterpart: getPedAnimation


Required Arguments

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • block: the animation block's name.
  • anim: the name of the animation within the block.
  • time: how long the animation will run for in milliseconds.
  • loop: indicates whether or not the animation will loop.
  • updatePosition: will change the actual coordinates of the ped according to the animation. Use this for e.g. walking animations.
  • interruptable: if set to false other tasks wont be able to interupt the animation. Setting this to 'false' also gives this function more power to override other animations that are running. For example, squatting after a jump can be terminated.
  • freezeLastFrame: if set to true after animation the last frame will be frozen, otherwise the animation will end and controls will return.
  • blendTime: how long the animation will mixed with the previous one in milliseconds.
  • retainPedState: will restore the task which was playing before calling this function. Useful for restoring the crouch task after animation ends. This may be extended in the future to support other states/tasks.

Returns

Returns true if succesful, false otherwise.

Examples

Click to collapse [-]
Server

This example creates a ped, rotates him, and makes him walk:

function makePed()
	local thePed = createPed(56, 1, 1, 4, 315)
	setPedAnimation(thePed, "ped", "WOMAN_walknorm")
end
addCommandHandler("makemyped", makePed)
Click to collapse [-]
Server

This example makes the player sit down and stand up using the command /sit.

local playerSitState = {}

function toggleSit(thePlayer)
	local playerSitting = playerSitState[thePlayer]

	if (not playerSitting) then
		setPedAnimation(thePlayer, "ped", "seat_down", -1, false, false, false, false)
		playerSitState[thePlayer] = true -- store the player state in the table

		return true
	end

	setPedAnimation(thePlayer) -- if you use again this command then your character stand up
	playerSitState[thePlayer] = nil -- remove player sit state from table
end
addCommandHandler("sit", toggleSit)

function onPlayerQuitClearSitState()
	playerSitState[source] = nil -- clear state at player quit, to prevent memory leak
end
addEventHandler("onPlayerQuit", root, onPlayerQuitClearSitState)

Changelog

Version Description
1.5.7-9.16632 Added retainPedState argument

Issues

Issue ID Description
#1110 retainPedState in setPedAnimation() does not work when latency reduction is set to 1
#953 setPedAnimation() "interrupt" and "time" has no effect in certain situations
#512 setPedAnimation isn't synced properly on server side
#467 Ped animations don't sync for new players
#463 setPedAnimation() does not work when a ped is attached and floating in air
#1173 setPedAnimation() removes player jetpack
#884 setPedAnimation() messes up collisions of peds inside vehicles

See Also