DetachTrailerFromVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
This function detaches an already attached trailer from a vehicle.
This function detaches an already attached trailer from a vehicle.
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">bool detachTrailerFromVehicle ( vehicle theVehicle, [vehicle theTrailer] )</syntaxhighlight>
<syntaxhighlight lang="lua">bool detachTrailerFromVehicle ( vehicle theVehicle [, vehicle theTrailer = nil ] )</syntaxhighlight>
 
{{OOP||[[vehicle]]:detachTrailer}}
===Required Arguments===
===Required Arguments===
*'''theVehicle''': The [[vehicle]] you wish to detach a trailer from.
*'''theVehicle''': The [[vehicle]] you wish to detach a trailer from.
Line 9: Line 10:
===Optional Arguments===
===Optional Arguments===
*'''theTrailer''': The trailer you wish to be detached.
*'''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.
{{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==
Line 15: Line 16:


==Example==
==Example==
'''Example 1:''' This example will create a trailer and a trailer-tower, attach them, then detach them.
'''Example 1:''' A command for players to detach a trailer attached to their vehicle:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
theVehicle = createVehicle ( 515, 500, 500, 40 ) -- create a trailer-tower (roadtrain)
-- the handler function for the "unhook" console command
trailer = createVehicle ( 435, 500, 505, 40 ) -- create a trailer
function unhookTrailer(playerSource, commandName)
detachTrailerFromVehicle ( theVehicle ) -- detach any old trailers from this vehicle
  -- check if a player triggered this command and that they are in a vehicle
attachTrailerToVehicle ( theVehicle, trailer ) -- attach them
  if (playerSource and isPedInVehicle(playerSource)) then
      local theVehicle = getPedOccupiedVehicle(playerSource) -- get the vehicle the player is in
      local success = detachTrailerFromVehicle(theVehicle) -- attempt to detach a trailer from this vehicle
      -- report whether the operation waa a success
      if (success) then
        outputConsole("Trailer detached!", playerSource)
      else
        outputConsole("Failed to detach trailer.", playerSource)
      end
  end
end
 
-- make the function above handle the "unhook" command
addCommandHandler("unhook", unhookTrailer)
</syntaxhighlight>
</syntaxhighlight>


'''Example 2:''' <syntaxhighlight lang="lua">This example will detach any trailers from a vehicle if it was damaged significantly.
'''Example 2:''' This example attaches a trailer to a truck, and detaches it if the trailer is damaged:
function removeTrailerOnDamage ( loss )
<syntaxhighlight lang="lua">
if loss > 20 then --if the loss was more than 20
function onTruckDamage ( loss )
detachTrailerFromVehicle ( source ) --detach any vehicles
  if ( loss > 50 ) then --if the health loss was more than 50
end
      detachTrailerFromVehicle ( source ) --detach the truck from the trailer
      -- remove the event handler so that this function is no longer called when the trailer is damaged
      removeEventHandler ( "onVehicleDamage", source, onTrailerDamage )
  end
end
end
addEventHandler ( "onVehicleDamage", getRootElement(), removeTrailerOnDamage  ) --add the event handler for onVehicleDamage</syntaxhighlight>
 
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", theTruck, onTruckDamage )
end
</syntaxhighlight>


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

Latest revision as of 04:20, 11 August 2019

This function detaches an already attached trailer from a vehicle.

Syntax

bool detachTrailerFromVehicle ( vehicle theVehicle [, vehicle theTrailer = nil ] )

OOP Syntax Help! I don't understand this!

Method: vehicle:detachTrailer(...)


Required Arguments

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

Optional Arguments

  • theTrailer: The trailer you wish to be detached.
[[{{{image}}}|link=|]] 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: A command for players to detach a trailer attached to their vehicle:

-- the handler function for the "unhook" console command
function unhookTrailer(playerSource, commandName)
   -- check if a player triggered this command and that they are in a vehicle
   if (playerSource and isPedInVehicle(playerSource)) then
      local theVehicle = getPedOccupiedVehicle(playerSource) -- get the vehicle the player is in
      local success = detachTrailerFromVehicle(theVehicle) -- attempt to detach a trailer from this vehicle
      -- report whether the operation waa a success
      if (success) then
         outputConsole("Trailer detached!", playerSource)
      else
         outputConsole("Failed to detach trailer.", playerSource)
      end
   end
end

-- make the function above handle the "unhook" command
addCommandHandler("unhook", unhookTrailer)

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

function onTruckDamage ( loss )
   if ( loss > 50 ) then --if the health loss was more than 50
      detachTrailerFromVehicle ( source ) --detach the truck from the trailer
      -- remove the event handler so that this function is no longer called when the trailer is damaged
      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", theTruck, onTruckDamage )
end

See Also