SetElementFrozen: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Fix example)
Line 21: Line 21:
This example binds the "p" key to a function to freeze/unfreeze the player's current vehicle.
This example binds the "p" key to a function to freeze/unfreeze the player's current vehicle.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- This function freezes the specified player's vehicle, if he's in one
function toggleFreezeStatus(thePlayer) -- This function freezes the specified player's vehicle, if he's in one
function toggleFreezeStatus ( thePlayer )
local playerVehicle = getPedOccupiedVehicle(thePlayer) -- Check if player's in vehicle
-- if he is in a vehicle,
 
if getPedOccupiedVehicle ( thePlayer ) then
if playerVehicle then -- if so
-- get the vehicle element
local isPlayerVehicleFrozen = isElementFrozen(playerVehicle) -- Check if vehicle is frozen
local playerVehicle = getPlayerOccupiedVehicle ( thePlayer )
 
-- get the current freeze status
setElementFrozen(playerVehicle, not isPlayerVehicleFrozen) -- Set opposite state
local currentFreezeStatus = isElementFrozen ( playerVehicle )
-- get the new freeze status (the opposite of the previous state)
local newFreezeStatus = not currentFreezeStatus
-- set the new freeze status
setElementFrozen ( playerVehicle, newFreezeStatus )
end
end
end
end


-- now bind a key to this function for all players.
local connectedPlayers = getElementsByType("player") -- Get all connected players
-- first get a list of all players
local playerElement = false -- Store reference to player
local connectedPlayers = getElementsByType ( "player" )
 
-- then, for each player,
for playerID = 1, #connectedPlayers do -- Loop through each player
for i, aPlayer in ipairs(connectedPlayers) do
playerElement = connectedPlayers[playerID] -- Get player element by accessing it via playerID
-- bind the player's "p" key to the toggleFreezeStatus function
 
bindKey ( aPlayer, "p", "down", "Toggle freeze status", toggleFreezeStatus )
bindKey(playerElement, "p", "down", toggleFreezeStatus, "Toggle freeze status") -- Bind function for player
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 13:26, 24 February 2021

This function freezes an element (stops it in its position and disables movement) or unfreezes it.

[[|link=|]] Warning: This function cancels any animation set by setPedAnimation if you freeze the ped.

Syntax

bool setElementFrozen ( element theElement, bool freezeStatus )

OOP Syntax Help! I don't understand this!

Method: element:setFrozen(...)
Variable: .frozen
Counterpart: isElementFrozen


Required Arguments

  • theElement: The element whose freeze status we want to change.
  • freezeStatus: A boolean denoting whether we want to freeze (true) or unfreeze (false) it.

Returns

Returns true if the element was frozen, false if it wasn't or if invalid arguments are passed.

Example

Click to collapse [-]
Serverside example

This example binds the "p" key to a function to freeze/unfreeze the player's current vehicle.

function toggleFreezeStatus(thePlayer) -- This function freezes the specified player's vehicle, if he's in one
	local playerVehicle = getPedOccupiedVehicle(thePlayer) -- Check if player's in vehicle

	if playerVehicle then -- if so
		local isPlayerVehicleFrozen = isElementFrozen(playerVehicle) -- Check if vehicle is frozen

		setElementFrozen(playerVehicle, not isPlayerVehicleFrozen) -- Set opposite state
	end
end

local connectedPlayers = getElementsByType("player") -- Get all connected players
local playerElement = false -- Store reference to player

for playerID = 1, #connectedPlayers do -- Loop through each player
	playerElement = connectedPlayers[playerID] -- Get player element by accessing it via playerID

	bindKey(playerElement, "p", "down", toggleFreezeStatus, "Toggle freeze status") -- Bind function for player
end

Issues

Issue ID Description
#9522 Freezing a ped or player whilst occupying a jetpack will remove their jetpack, but not the jetpack sound

See Also