CreateVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">vehicle createVehicle ( model, x, y, z, [rx, ry, rz, string numberplate] )</syntaxhighlight>
<syntaxhighlight lang="lua">vehicle createVehicle ( int model, float x, float y, float z, [float rx, float ry, float rz, string numberplate] )</syntaxhighlight>


===Required Arguments===
===Required Arguments===

Revision as of 19:36, 20 September 2006

This function creates a vehicle at the specified location.

Syntax

vehicle createVehicle ( int model, float x, float y, float z, [float rx, float ry, float rz, string numberplate] )

Required Arguments

  • model: The Vehicle IDs|vehicle ID of the vehicle being created.
  • x: A floating point number representing the X coordinate on the map.
  • y: A floating point number representing the Y coordinate on the map.
  • z: A floating point number representing the Z coordinate on the map.

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.

  • rx: A floating point number representing the rotation about the X axis in Degrees.
  • ry: A floating point number representing the rotation about the Y axis in Degrees.
  • rz: A floating point number representing the rotation about the Z axis in Degrees.
  • numberplate: A string that will go on the number plate of the car (max 8 letters). This is only applicable to cars.

Returns

Returns the vehicle element that was created. Returns false if the arguments are incorrect, or if the vehicle limit of 65535 is exceeded.

Example

Example 1: This example creates a vehicle five units to the right of a player when they type "createvehicle" and it's name in the console:

-- call the 'consoleCreateVehicle' function when the "createvehicle" command is used:
addCommandHandler ( "createvehicle", "consoleCreateVehicle" )
function consoleCreateVehicle ( player, commandName, first, second )
   if ( player ) then
      -- calculate the position of the vehicle based on the player's position and rotation:
      local id, x, y, z, r, d = 0, 0, 0, 0, 0, 5
      r = getPlayerRotation ( player ) -- get the player's rotation
      x, y, z = getElementPosition ( player ) -- get the player's position
      x = x + ( ( math.cos ( math.rad ( r ) ) ) * d ) -- calculate the X position of the vehicle
      y = y + ( ( math.sin ( math.rad ( r ) ) ) * d ) -- calculate the Y position of the vehicle
      -- check whether the second argument exists (or whether the vehicle's name consists of two words):
      if ( second ) then
         id = getVehicleIDFromName ( first .. " " .. second ) -- get the vehicle's model ID from the name
      else
         id = getVehicleIDFromName ( first ) -- get the vehicle's model ID from the name
      end
      -- create the vehicle using the information gathered above:
      local vehicle = createVehicle ( id, x, y, z, 0, 0, r )
      if ( vehicle == false ) then -- if vehicle creation failed, give the player a message
         outputConsole ( "Failed to create vehicle.", player )
      end
   end
end

Example 2: Create a Hydra at the given position:

createVehicle ( 520, 0, 0, 5 )

See Also

GTASA IDs (vehicles, weapons, weathers, characters, colors): http://info.vces.net/ (Special thanks to Brophy and Ratt for making these lists)