DetachTrailerFromVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
Line 15: Line 15:


==Example==
==Example==
'''Example 1:''' This example will create a trailer and a trailer-tower, attach them, then detach them.
'''Example 1:''' This example attaches a trailer to a truck, and detaches it if the trailer is damaged:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
theVehicle = createVehicle ( 515, 500, 500, 40 ) -- create a trailer-tower (roadtrain)
function onTrailerDamage ( loss )
trailer = createVehicle ( 435, 500, 505, 40 ) -- create a trailer
  if ( loss > 50 ) then --if the health loss was more than 50
detachTrailerFromVehicle ( theVehicle ) -- detach any old trailers from this vehicle
      detachTrailerFromVehicle ( source ) --detach the truck from the trailer
attachTrailerToVehicle ( theVehicle, trailer ) -- attach them
      removeEventHandler ( "onVehicleDamage", source, onTrailerDamage )
</syntaxhighlight>
  end
end


'''Example 2:''' <syntaxhighlight lang="lua">This example will detach any trailers from a vehicle if it was damaged significantly.
function createVehiclesAndAttachThem ()
function removeTrailerOnDamage ( loss )
  local theTruck = createVehicle ( 515, 500, 500, 40 ) -- create a trailer-tower (roadtrain)
if loss > 20 then --if the loss was more than 20
  local theTrailer = createVehicle ( 435, 500, 505, 40 ) -- create a trailer
detachTrailerFromVehicle ( source ) --detach any vehicles
  attachTrailerToVehicle ( theTruck, theTrailer ) -- attach them
end
  -- add an event handler for when the trailer is damaged
  addEventHandler ( "onVehicleDamage", theTrailer, onTrailerDamage )
end
end
addEventHandler ( "onVehicleDamage", getRootElement(), removeTrailerOnDamage  ) --add the event handler for onVehicleDamage</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Vehicle functions}}
{{Vehicle functions}}

Revision as of 23:54, 28 July 2007

This function detaches an already attached trailer from a vehicle.

Syntax

bool detachTrailerFromVehicle ( vehicle theVehicle, [vehicle theTrailer] )

Required Arguments

  • theVehicle: The vehicle you wish to detach a trailer from.

Optional Arguments

  • theTrailer: The trailer you wish to be detached.

Note: If 'theTrailer' is specified, it will only detach if this matches. If it is not specified, any trailer attached to 'theVehicle' will be detached.

Returns

Returns 'true' if the vehicle's were successfully detached, 'false' otherwise.

Example

Example 1: This example attaches a trailer to a truck, and detaches it if the trailer is damaged:

function onTrailerDamage ( loss )
   if ( loss > 50 ) then --if the health loss was more than 50
      detachTrailerFromVehicle ( source ) --detach the truck from the trailer
      removeEventHandler ( "onVehicleDamage", source, onTrailerDamage )
   end
end

function createVehiclesAndAttachThem ()
   local theTruck = createVehicle ( 515, 500, 500, 40 ) -- create a trailer-tower (roadtrain)
   local theTrailer = createVehicle ( 435, 500, 505, 40 ) -- create a trailer
   attachTrailerToVehicle ( theTruck, theTrailer ) -- attach them
   -- add an event handler for when the trailer is damaged
   addEventHandler ( "onVehicleDamage", theTrailer, onTrailerDamage )
end

See Also