SetElementPosition

From Multi Theft Auto: Wiki
Revision as of 13:15, 12 August 2007 by AlienX (talk | contribs) (→‎Example)
Jump to navigation Jump to search

This function sets the position of an element to the co-ordinates specified.

Syntax

bool setElementPosition ( element theElement, float x, float y, float z )  

Required Arguments

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

Returns

Returns true if the function was successful, false otherwise.

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.

function consoleWarpTo ( player, commandName, player2nick )
	if ( player ) then
		--Setup the variables we will be using for teleportation
    	local x, y, z, r, d = 0, 0, 0, 0, 2.5
    	--Grab the players element identifer we are trying to warp too
    	local player2 = getPlayerFromNick ( player2nick )
    	--Make sure it exists!
    	if ( player2 ) then
    		--Is the player were warping too in a vehicle?
        	if ( isPlayerInVehicle ( player2 ) ) then
        		--Indeed they are, lets get the vehicle information such as the vehicle element itself, and the seats its got.
        		local player2vehicle = getPlayerOccupiedVehicle ( player2 )
				local maxseats = getVehicleMaxPassengers ( player2vehicle ) + 1
				local i = 0
				--Loop around the max seats seeing if there is a free seat
				while ( i < maxseats ) do
					if ( getVehicleOccupant ( player2vehicle, i ) ) then
						--There isnt a free seat, so lets goto the next seat and check that one
						i = i + 1
					else
						--
						break
					end
				end
				--Check if the value in i, is under the max seats, if it is it means its a free seat
				if ( i < maxseats ) then
					--Teleport the player into the seat
					local status = warpPlayerIntoVehicle ( player, player2vehicle, i )
				else
					--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
		else
			--No player by the name was found, tell the warper this.
			outputChatBox ( "No such player.", player )
		end
	end
end
addCommandHandler ( "warpto", consoleWarpTo )

See Also