SetElementPosition: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Undo revision 79831 by AliiAm (talk))
Tag: Undo
 
(31 intermediate revisions by 17 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__
This function sets the position of an element to the co-ordinates specified.
{{Shared function}}
This function sets the position of an element to the specified coordinates.
{{Warning|Do not use this function to spawn a [[player]]. It will cause problems with other functions like [[warpPedIntoVehicle]]. Use [[spawnPlayer]] instead.}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setElementPosition ( element theElement, float x, float y, float z )   
bool setElementPosition ( element theElement, float x, float y, float z [, bool warp = true ] )   
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[element]]:setPosition|position|getElementPosition}}


===Required Arguments===  
===Required Arguments===  
*'''theElement:''' A valid [[element]] to be moved.
*'''theElement:''' A valid [[element]] to be moved.
*'''x:''' The x co-ordinate of the destination.
*'''x:''' The x coordinate of the destination.
*'''y:''' The y co-ordinate of the destination.
*'''y:''' The y coordinate of the destination.
*'''z:''' The z co-ordinate of the destination.
*'''z:''' The z coordinate of the destination.
 
===Optional Arguments===
*'''warp:''' teleports players, resetting any animations they were doing. Setting this to ''false'' preserves the current animation.


===Returns===
===Returns===
Returns ''true'' if the function was successful, ''false'' otherwise.
Returns ''true'' if the function was successful, ''false'' otherwise.


==Example==  
==Example==
This example enables a player to type /warpto <playername> to warp to them, if the player being warped to is in a vehicle with a free passenger seat, it will warp into the vehicle.
<section name="Server" class="server" show="true">
This example lets admins teleport 5 random players to themselves
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function consoleWarpTo ( player, commandName, player2nick )
function randomPlayersToLocation(p)
if ( player ) then
    if not isPlayerStaff(p) then return end
--Setup the variables we will be using for teleportation
 
    local x, y, z, r, d = 0, 0, 0, 0, 2.5
local playersOnline = getElementsByType("player")
    --Grab the players element identifer we are trying to warp too
local amount = #playersOnline
    local player2 = getPlayerFromNick ( player2nick )
 
    --Make sure it exists!
if amount == 0 then return end
    if ( player2 ) then
 
    --Is the player were warping too in a vehicle?
for index = 1,(amount > 5 and 5 or amount) do
        if ( isPlayerInVehicle ( player2 ) ) then
local player = playersOnline[index]
        --Indeed they are, lets get the vehicle information such as the vehicle element itself, and the seats its got.
setElementPosition(player, getElementPosition(p))
        local player2vehicle = getPlayerOccupiedVehicle ( player2 )
end
local maxseats = getVehicleMaxPassengers ( player2vehicle ) + 1
end
local i = 0
addCommandHandler("randomtp", randomPlayersToLocation)
--Loop around the max seats seeing if there is a free seat
addCommandHandler("playershere", randomPlayersToLocation)
while ( i < maxseats ) do
 
if ( getVehicleOccupant ( player2vehicle, i ) ) then
-- Utility function
--There isnt a free seat, so lets goto the next seat and check that one
local staffACLs = {
i = i + 1
    aclGetGroup("Admin"),
else
    aclGetGroup("Moderator")
--
}
break
 
end
function isPlayerStaff(p)
end
if isElement(p) and getElementType(p) == "player" and not isGuestAccount(getPlayerAccount(p)) then
--Check if the value in i, is under the max seats, if it is it means its a free seat
local object = getAccountName(getPlayerAccount(p))
if ( i < maxseats ) then
 
--Teleport the player into the seat
for _, group in ipairs(staffACLs) do
local status = warpPlayerIntoVehicle ( player, player2vehicle, i )
if isObjectInACLGroup("user." .. object, group) then
else
return true
--There are no free seats, tell the player that.
outputChatBox ( "Sorry, the player's vehicle is full (" .. getVehicleName ( player2vehicle ) .. " " .. i .. "/" .. maxseats .. ")", player )
end
else
--The player isnt in a vehicle, lets get the players position and warp us to them.
x, y, z = getElementPosition ( player2 )
r = getPlayerRotation ( player2 )
--Make sure we get interior data, they might be inside one!
interior = getElementInterior ( player2 )
dimension = getElementDimension ( player2 )
--Do some funky math to make sure that we dont teleport inside of them (get us both stuck inside eachother)
  x = x - ( ( math.cos ( math.rad ( r + 90 ) ) ) * d )
  y = y - ( ( math.sin ( math.rad ( r + 90 ) ) ) * d )
  --Set a few timers for setting interiors, dimensions and positions
setTimer ( setElementInterior, 800, 1, player, interior )
setTimer ( setElementDimension, 900, 1, player, dimension )
  setTimer ( setElementPosition, 1000, 1, player, x, y, z )
  setTimer ( setPlayerRotation, 1000, 1, player, r )
  --Fade the camera to make it look nicer
fadeCamera ( player, false, 1, 0, 0, 0 )
--Fade it back once its all complete!
setTimer ( fadeCamera, 1000, 1, player, true, 1 )
end
end
else
--No player by the name was found, tell the warper this.
outputChatBox ( "No such player.", player )
end
end
end
end
return false
end
end
addCommandHandler ( "warpto", consoleWarpTo )
</syntaxhighlight>
</syntaxhighlight>
</section>
If you want to put a vehicle or player out of the water or simulate the position-resetting behaviour if CJ goes below the ground too far, then you need to retrieve a recommended coordinate on ground to place the element at. Take a look at [https://forum.mtasa.com/topic/132891-important-helprespawn-vehicle/?do=findComment&comment=1003198 this MTA forums post] for steps in the right direction.
==Issues==
{{Issues|
{{Issue|539|Changing player position when he/she has a jetpack will remove the jetpack and bug when skin is changed}}
{{Issue|529|Player falls from his bike when its teleported by setElementPosition}}
}}


==See Also==
==See Also==
{{Element functions}}
{{Element functions}}
[[Category:Incomplete]]
 
[[hu:setElementPosition]]
[[ru:setElementPosition]]

Latest revision as of 18:59, 11 July 2024

This function sets the position of an element to the specified coordinates.

[[|link=|]] Warning: Do not use this function to spawn a player. It will cause problems with other functions like warpPedIntoVehicle. Use spawnPlayer instead.

Syntax

bool setElementPosition ( element theElement, float x, float y, float z [, bool warp = true ] )  

OOP Syntax Help! I don't understand this!

Method: element:setPosition(...)
Variable: .position
Counterpart: getElementPosition


Required Arguments

  • theElement: A valid element to be moved.
  • x: The x coordinate of the destination.
  • y: The y coordinate of the destination.
  • z: The z coordinate of the destination.

Optional Arguments

  • warp: teleports players, resetting any animations they were doing. Setting this to false preserves the current animation.

Returns

Returns true if the function was successful, false otherwise.

Example

Click to collapse [-]
Server

This example lets admins teleport 5 random players to themselves

function randomPlayersToLocation(p)
    if not isPlayerStaff(p) then return end

	local playersOnline = getElementsByType("player")
	local amount = #playersOnline

	if amount == 0 then return end

	for index = 1,(amount > 5 and 5 or amount) do
		local player = playersOnline[index]
		setElementPosition(player, getElementPosition(p))
	end
end
addCommandHandler("randomtp", randomPlayersToLocation)
addCommandHandler("playershere", randomPlayersToLocation)

-- Utility function
local staffACLs = {
    aclGetGroup("Admin"),
    aclGetGroup("Moderator")
}

function isPlayerStaff(p)
	if isElement(p) and getElementType(p) == "player" and not isGuestAccount(getPlayerAccount(p)) then
		local object = getAccountName(getPlayerAccount(p))

		for _, group in ipairs(staffACLs) do
			if isObjectInACLGroup("user." .. object, group) then
				return true
			end
		end
	end
	return false
end

If you want to put a vehicle or player out of the water or simulate the position-resetting behaviour if CJ goes below the ground too far, then you need to retrieve a recommended coordinate on ground to place the element at. Take a look at this MTA forums post for steps in the right direction.

Issues

Issue ID Description
#539 Changing player position when he/she has a jetpack will remove the jetpack and bug when skin is changed
#529 Player falls from his bike when its teleported by setElementPosition

See Also