SetPedEnterVehicle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Remove obsolete Requirements section)
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{New feature/item|3.0160|1.5.8|20740|This function makes a [[ped]] enter a [[vehicle]], similar to the enter_exit control state.}}
{{Added feature/item|1.5.9|1.5.8|20740|This function makes a [[ped]] enter a [[vehicle]], similar to the enter_exit control state.}}
 
{{Note|For this function to work on peds, the server and all clients are required to be on version '''1.5.8 r20740''' or newer.}}
{{Note|This function only works on synced peds and vehicles (ie. created serverside).}}
{{Note|This function only works on synced peds and vehicles (ie. created serverside).}}
{{Note|This function only works within the following limits:
* If forced to enter as a passenger, it doesn't work if all passenger seats are occupied. Only the driver seat can be jacked.
* If forced to enter as a driver, the ped can carjack the current driver.
** If the driver's door is blocked by something, the ped can use the opposite front door to reach the driver's seat, jacking the passenger in the process.
* If a vehicle is not specified:
** The ped will search for a vehicle door within 20 m.
** If the vehicle has a driver, the limit becomes 10 m.
* If a vehicle is specified:
** The vehicle has to be within 50 m. The doors aren't taken into account. It means that it doesn't work if the vehicle's door is in range but the vehicle itself is not.
** If the vehicle has a driver, the limit becomes 10 m.
* When entering, the ped will run toward a vehicle if it is less than 50 m away.
* The ped reserves the seat he is trying to use. It means nobody can enter the respective seat while the ped is running toward it.
** Exception: If the ped is forced to enter as a passenger and is going to use the front door, the ped can wait if someone is using it to go the driver seat.
}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setPedEnterVehicle ( ped thePed [, vehicle = nil, passenger = false ] )
bool setPedEnterVehicle ( ped thePed [, vehicle theVehicle = nil, bool passenger = false ] )
</syntaxhighlight>  
</syntaxhighlight>  


Line 22: Line 34:


When this function returns ''true'', the client will ask server for permission to enter a vehicle. Actually entering can still fail in the following cases
When this function returns ''true'', the client will ask server for permission to enter a vehicle. Actually entering can still fail in the following cases
*The function is used on a ped, but another client is not on version '''1.5.8 r20740''' or newer.
*Another player or ped is already entering, exiting or jacking the same vehicle and seat.
*Another player or ped is already entering, exiting or jacking the same vehicle and seat.
*Movement input or damage can interrupt the task. Use [[getPedTask]] to monitor what the ped is doing.
*Movement input or damage can interrupt the task. Use [[getPedTask]] to monitor what the ped is doing.
Line 28: Line 39:
This function returns ''false'' in the following cases
This function returns ''false'' in the following cases
*Invalid arguments were parsed.
*Invalid arguments were parsed.
*The function is used on a ped, but the server is not on version '''1.5.8 r20740''' or newer.
*Time passed since last enter/exit for this ped is less than 1500 ms.
*Time passed since last enter/exit for this ped is less than 1500 ms.
*[[onClientVehicleStartEnter]] was cancelled by a script.
*[[onClientVehicleStartEnter]] was cancelled by a script.
Line 34: Line 44:


==Example==  
==Example==  
Make ped Sweet enter his car
Make [[ped]] Sweet enter his car:
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local sweet = createPed(270, 0, 0, 3)
local sweet = createPed (270, 0, 0, 3)
setElementID(sweet , 'sweet')
setElementID (sweet, "sweet")


local sweets_car = createVehicle(492, 3, 0, 3)
local sweetscar = createVehicle (492, 3, 0, 3)
setElementID(sweets_car, 'sweets_car')
setElementID (sweetscar, "sweetscar")
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
Line 47: Line 57:
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Code will work only if client is syncing Sweet
-- Code works only if client is syncing ped Sweet
addCommandHandler('sweet_enter_car',
addCommandHandler ("sweetentercar",
function()
function()
     local sweet = getElementByID('sweet')
     local sweet = getElementByID ("sweet")


     if isElementSyncer(sweet) then
     if isElementSyncer (sweet) then
         setPedEnterVehicle(getElementByID('sweet'), getElementByID('sweets_car'), true)
         setPedEnterVehicle (sweet, getElementByID("sweetscar"), true)
     end
     end
end)
end)

Latest revision as of 17:22, 7 November 2024

This function makes a ped enter a vehicle, similar to the enter_exit control state.

[[{{{image}}}|link=|]] Note: This function only works on synced peds and vehicles (ie. created serverside).
[[{{{image}}}|link=|]] Note: This function only works within the following limits:
  • If forced to enter as a passenger, it doesn't work if all passenger seats are occupied. Only the driver seat can be jacked.
  • If forced to enter as a driver, the ped can carjack the current driver.
    • If the driver's door is blocked by something, the ped can use the opposite front door to reach the driver's seat, jacking the passenger in the process.
  • If a vehicle is not specified:
    • The ped will search for a vehicle door within 20 m.
    • If the vehicle has a driver, the limit becomes 10 m.
  • If a vehicle is specified:
    • The vehicle has to be within 50 m. The doors aren't taken into account. It means that it doesn't work if the vehicle's door is in range but the vehicle itself is not.
    • If the vehicle has a driver, the limit becomes 10 m.
  • When entering, the ped will run toward a vehicle if it is less than 50 m away.
  • The ped reserves the seat he is trying to use. It means nobody can enter the respective seat while the ped is running toward it.
    • Exception: If the ped is forced to enter as a passenger and is going to use the front door, the ped can wait if someone is using it to go the driver seat.

Syntax

bool setPedEnterVehicle ( ped thePed [, vehicle theVehicle = nil, bool passenger = false ] )

Required Arguments

  • thePed: The player or ped to enter the vehicle.
    • Note: The player must be the local player.
    • Note: The ped must be synced by the client. Use isElementSyncer clientside to check if the client is syncing. Use setElementSyncer serverside to change the syncer manually.
  • vehicle: The vehicle to enter. If no vehicle is set, the ped will enter the nearest vehicle within 20 m.
  • passenger: If set to true, the ped will enter as passenger in the nearest available seat, otherwise he will enter as driver.

Returns

Returns true if the function was successful, false otherwise.

When this function returns true, the client will ask server for permission to enter a vehicle. Actually entering can still fail in the following cases

  • Another player or ped is already entering, exiting or jacking the same vehicle and seat.
  • Movement input or damage can interrupt the task. Use getPedTask to monitor what the ped is doing.

This function returns false in the following cases

  • Invalid arguments were parsed.
  • Time passed since last enter/exit for this ped is less than 1500 ms.
  • onClientVehicleStartEnter was cancelled by a script.
  • The ped has an active TASK_PRIMARY task. Use getPedTask to monitor what the ped is doing.

Example

Make ped Sweet enter his car:

Click to collapse [-]
Server
local sweet = createPed (270, 0, 0, 3)
setElementID (sweet, "sweet")

local sweetscar = createVehicle (492, 3, 0, 3)
setElementID (sweetscar, "sweetscar")
Click to collapse [-]
Client
-- Code works only if client is syncing ped Sweet
addCommandHandler ("sweetentercar",
function()
    local sweet = getElementByID ("sweet")

    if isElementSyncer (sweet) then
        setPedEnterVehicle (sweet, getElementByID("sweetscar"), true)
    end
end)

See Also