CreateVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (fixed command handler)
(Improved main example, removed useless example, minor fixes)
Line 5: Line 5:


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">vehicle createVehicle ( int model, float x, float y, float z, [float rx, float ry, float 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===
Line 15: Line 17:
===Optional Arguments===
===Optional Arguments===
{{OptionalArg}}
{{OptionalArg}}
* '''rx''': A floating point number representing the rotation about the X axis in Degrees.
* '''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.
* '''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.
* '''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.
* '''numberplate''': A string that will go on the number plate of the car (max 8 characters). This is only applicable to cars.


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


==Using Trains==
==Using trains==
Trains are created using the createVehicle function. They are placed at the nearest point of the GTASA train pathing (railroad tracks) from their spawning point.  
Trains are created using the createVehicle function. They are placed at the nearest point of the GTASA train pathing (railroad tracks) from their spawning point.  


==Example==
==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:
This example creates a vehicle five units to the right of a player when they type "createvehicle" and its name in the console:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- call the 'consoleCreateVehicle' function when the "createvehicle" command is used:
-- define our handler (we'll take a variable number of parameters where the name goes, because there are vehicle names with more than one word)
function consoleCreateVehicle ( player, commandName, first, second )
function consoleCreateVehicle ( player, commandName, ... )
  -- if a player triggered it, not the admin,
   if ( player ) then
   if ( player ) then
       -- calculate the position of the vehicle based on the player's position and rotation:
       -- 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
       local distance = 5 --units
      r = getPlayerRotation ( player ) -- get the player's rotation
      local x, y, z = getElementPosition ( player ) -- get the player's position
       x, y, z = getElementPosition ( player ) -- get the player's position
       local rotZ = getPlayerRotation ( player ) -- get the player's rotation around the Z axis in degrees
       x = x + ( ( math.cos ( math.rad ( r ) ) ) * d ) -- calculate the X position of the vehicle
       x = x + ( ( math.cos ( math.rad ( rotZ ) ) ) * distance ) -- calculate the X position of the vehicle
       y = y + ( ( math.sin ( math.rad ( r ) ) ) * d ) -- calculate the Y position of the vehicle
       y = y + ( ( math.sin ( math.rad ( rotZ ) ) ) * distance ) -- 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
       -- get the complete vehicle name by joining all passed parameters using Lua's table.concat
        id = getVehicleIDFromName ( first .. " " .. second ) -- get the vehicle's model ID from the name
       local vehicleName = table.concat({...}, " ")
       else
      -- get the vehicle's model ID from the name
        id = getVehicleIDFromName ( first ) -- get the vehicle's model ID from the name
       local vehicleID = getVehicleIDFromName ( vehicleName )
       end
      -- if vehicle ID is valid,
      -- create the vehicle using the information gathered above:
       if vehicleID then
      local vehicle = createVehicle ( id, x, y, z, 0, 0, r )
            -- create the vehicle using the information gathered above:
      if ( vehicle == false ) then -- if vehicle creation failed, give the player a message
            local vehicle = createVehicle ( vehicleID, x, y, z, 0, 0, rotZ )
        outputConsole ( "Failed to create vehicle.", player )
            -- if vehicle creation failed, give the player a message
            if ( vehicle == false ) then
              outputConsole ( "Failed to create vehicle.", player )
            end
       end
       end
   end
   end
end
end
-- Attach the 'consoleCreateVehicle' function to the "createvehicle" command
addCommandHandler ( "createvehicle", consoleCreateVehicle )
addCommandHandler ( "createvehicle", consoleCreateVehicle )
</syntaxhighlight>
'''Example 2:''' Create a Hydra at the given position:
<syntaxhighlight lang="lua">
createVehicle ( 520, 0, 0, 5 ) -- 520 is the model id for the Hydra
</syntaxhighlight>
</syntaxhighlight>



Revision as of 16:04, 30 July 2007

This function creates a vehicle at the specified location.

Its worth noting that the position of the vehicle is the center point of the vehicle, not its base. As such, you need to ensure that the z value (vertical axis) is some height above the ground. You can find the exact height using the client side function getElementDistanceFromCentreOfMassToBaseOfModel, or you can estimate it yourself and just spawn the vehicle so it drops to the ground.

Syntax

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

Required Arguments

  • model: The 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 characters). 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.

Using trains

Trains are created using the createVehicle function. They are placed at the nearest point of the GTASA train pathing (railroad tracks) from their spawning point.

Example

This example creates a vehicle five units to the right of a player when they type "createvehicle" and its name in the console:

-- define our handler (we'll take a variable number of parameters where the name goes, because there are vehicle names with more than one word)
function consoleCreateVehicle ( player, commandName, ... )
   -- if a player triggered it, not the admin,
   if ( player ) then
      -- calculate the position of the vehicle based on the player's position and rotation:
      local distance = 5 --units
      local x, y, z = getElementPosition ( player ) -- get the player's position
      local rotZ = getPlayerRotation ( player ) -- get the player's rotation around the Z axis in degrees
      x = x + ( ( math.cos ( math.rad ( rotZ ) ) ) * distance ) -- calculate the X position of the vehicle
      y = y + ( ( math.sin ( math.rad ( rotZ ) ) ) * distance ) -- calculate the Y position of the vehicle

      -- get the complete vehicle name by joining all passed parameters using Lua's table.concat
      local vehicleName = table.concat({...}, " ")
      -- get the vehicle's model ID from the name
      local vehicleID = getVehicleIDFromName ( vehicleName )
      -- if vehicle ID is valid,
      if vehicleID then
            -- create the vehicle using the information gathered above:
            local vehicle = createVehicle ( vehicleID, x, y, z, 0, 0, rotZ )
            -- if vehicle creation failed, give the player a message
            if ( vehicle == false ) then
               outputConsole ( "Failed to create vehicle.", player )
            end
      end
   end
end
-- Attach the 'consoleCreateVehicle' function to the "createvehicle" command
addCommandHandler ( "createvehicle", consoleCreateVehicle )

See Also

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