GetVehicleEntryPoints: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Client function}} __NOTOC__ {{New feature/item|3.0161|1.6.0|22649| This function returns a table containing the positions to 4 possible entry points to a vehicle. This function can be used alongside setPedEnterVehicle to make a ped enter a specific seat by first moving the ped to a entry point retrieved through '''getVehicleEntryPoints''' and then using setPedEnterVehicle to make them enter. }} {{Note|This does not directly relate to the amount of doors a ve...")
 
m (Added the argument name for the vehicle to the function syntax)
 
(4 intermediate revisions by 2 users not shown)
Line 6: Line 6:


{{Note|This does not directly relate to the amount of doors a vehicle has as vehicles with two doors can have multiple entry points to the same door.}}
{{Note|This does not directly relate to the amount of doors a vehicle has as vehicles with two doors can have multiple entry points to the same door.}}
{{Note|The vehicle needs to be streamed in for this function to work.}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table getVehicleEntryPoints(vehicle)
table getVehicleEntryPoints ( vehicle theVehicle )
</syntaxhighlight>  
</syntaxhighlight>  


Line 21: Line 22:


==Example==  
==Example==  
{{Needs Example}}
 
'''Example 1''': This example renders 3D (''text & circle'') on each '''streamed-in''' vehicle entry point
<syntaxhighlight lang="lua">
addEventHandler("onClientRender", root, function()
    for _, vehicle in pairs(getElementsByType("vehicle", root, true)) do
        local entryX, entryY, entryZ = getElementPosition(vehicle)
        for index, position in pairs(getVehicleEntryPoints(vehicle)) do
            local drawX, drawY, distance = getScreenFromWorldPosition(
                position[1], position[2], position[3], 100
            )
 
            if (drawX ~= false) then
                local cameraX, cameraY, cameraZ = getCameraMatrix()
 
                dxDrawCircle(drawX, drawY, 8 / (distance * 0.1), 0, 360, tocolor(255, 0, 0, 255))
                dxDrawText(tostring(index), drawX, drawY + ((8 / (distance * 0.1)) / 2) + ((2 / (distance * 0.1)) + 0.5), 0, 0, tocolor(255, 0, 0, 255),
                    3 / (distance * 0.1), "default-bold")
            end
        end
    end
end)
</syntaxhighlight>
 
'''Example 2''': This example checks if the player is near the vehicle door to ''enter''
 
<syntaxhighlight lang="lua">
addEventHandler("onClientVehicleStartEnter", root, function(thePlayer, seat, door)
    if thePlayer == localPlayer then
        local entryPoints = getVehicleEntryPoints(source)
        if entryPoints then
            local entryPoint = entryPoints[door + 1]
            if entryPoint then
                local distance = getDistanceBetweenPoints3D(Vector3(getElementPosition(localPlayer)), unpack(entryPoint))
                if distance > 2.5 then
                    outputChatBox("You are too far away from the door to enter this vehicle!")
                    cancelEvent()
                end
            end
        end
    end
end)
</syntaxhighlight>


==See Also==
==See Also==
{{Client vehicle functions}}
{{Client vehicle functions}}

Latest revision as of 06:43, 11 October 2024

ADDED/UPDATED IN VERSION 1.6.0 r22649:

This function returns a table containing the positions to 4 possible entry points to a vehicle. This function can be used alongside setPedEnterVehicle to make a ped enter a specific seat by first moving the ped to a entry point retrieved through getVehicleEntryPoints and then using setPedEnterVehicle to make them enter.

[[{{{image}}}|link=|]] Note: This does not directly relate to the amount of doors a vehicle has as vehicles with two doors can have multiple entry points to the same door.
[[{{{image}}}|link=|]] Note: The vehicle needs to be streamed in for this function to work.

Syntax

table getVehicleEntryPoints ( vehicle theVehicle )


OOP Syntax Help! I don't understand this!

Method: vehicle:getEntryPoints(...)


Required arguments

  • vehicle: The vehicle from which we want to get entry points.

Returns

If the vehicle has entry points, it returns a table containing the positions of the 4 possible entry points to the vehicle, otherwise it returns false.

Example

Example 1: This example renders 3D (text & circle) on each streamed-in vehicle entry point

addEventHandler("onClientRender", root, function()
    for _, vehicle in pairs(getElementsByType("vehicle", root, true)) do
        local entryX, entryY, entryZ = getElementPosition(vehicle) 
        for index, position in pairs(getVehicleEntryPoints(vehicle)) do
            local drawX, drawY, distance = getScreenFromWorldPosition(
                position[1], position[2], position[3], 100
            )

            if (drawX ~= false) then
                local cameraX, cameraY, cameraZ = getCameraMatrix()

                dxDrawCircle(drawX, drawY, 8 / (distance * 0.1), 0, 360, tocolor(255, 0, 0, 255))
                dxDrawText(tostring(index), drawX, drawY + ((8 / (distance * 0.1)) / 2) + ((2 / (distance * 0.1)) + 0.5), 0, 0, tocolor(255, 0, 0, 255),
                    3 / (distance * 0.1), "default-bold")
            end
        end
    end
end)

Example 2: This example checks if the player is near the vehicle door to enter

addEventHandler("onClientVehicleStartEnter", root, function(thePlayer, seat, door)
    if thePlayer == localPlayer then
        local entryPoints = getVehicleEntryPoints(source)
        if entryPoints then
            local entryPoint = entryPoints[door + 1]
            if entryPoint then
                local distance = getDistanceBetweenPoints3D(Vector3(getElementPosition(localPlayer)), unpack(entryPoint))
                if distance > 2.5 then
                    outputChatBox("You are too far away from the door to enter this vehicle!")
                    cancelEvent()
                end
            end
        end
    end
end)

See Also