Skip to content

Instantly share code, notes, and snippets.

@throwarray
Last active June 16, 2022 20:45
Show Gist options
  • Save throwarray/54959a69161a40d1a2d5f678dbc809fc to your computer and use it in GitHub Desktop.
Save throwarray/54959a69161a40d1a2d5f678dbc809fc to your computer and use it in GitHub Desktop.
local setGear = GetHashKey('SET_VEHICLE_CURRENT_GEAR') & 0xFFFFFFFF
local function SetVehicleCurrentGear(veh, gear)
Citizen.InvokeNative(setGear, veh, gear)
end
local nextGear = GetHashKey('SET_VEHICLE_NEXT_GEAR') & 0xFFFFFFFF
local function SetVehicleNextGear(veh, gear)
Citizen.InvokeNative(nextGear, veh, gear)
end
local function ForceVehicleGear (vehicle, gear)
SetVehicleCurrentGear(vehicle, gear)
SetVehicleNextGear(vehicle, gear)
return gear
end
--------------------------------------------------------------------------------
local mode
Citizen.CreateThread(function ()
local vehicle, lastVehicle, ped
local gear, maxGear
local nextMode, maxMode
local braking
local function HandleVehicleBrake ()
if gear == 0 then -- Prevent reversing
DisableControlAction(2, 72, true)
-- use parking brake once stopped
if IsDisabledControlPressed(2, 72) then
SetControlNormal(2, 76, 1.0)
braking = true
end
elseif IsControlPressed(2, 72) then
braking = true
end
end
local function OnTick()
braking = false
-- Reverse
if mode == 1 then
DisableControlAction(2, 71, true)
-- gas
if IsDisabledControlPressed(2, 71) then
SetControlNormal(2, 72, GetDisabledControlNormal(2, 71))
else
HandleVehicleBrake()
end
-- Neutral
elseif mode == 2 then
HandleVehicleBrake()
ForceVehicleGear(vehicle, 1)
-- gas
DisableControlAction(2, 71, true)
if IsDisabledControlPressed(2, 71) then
SetVehicleCurrentRpm(vehicle, 1.0)
end
-- Drive
else
HandleVehicleBrake()
ForceVehicleGear(vehicle, mode - 2)
end
-- Brake
if braking or IsControlPressed(2, 76) then
SetVehicleBrakeLights(vehicle, true)
SetVehicleNextGear(vehicle, 0)
braking = true
end
end
while true do
ped = PlayerPedId()
vehicle = GetVehiclePedIsUsing(ped)
if DoesEntityExist(vehicle) and GetPedInVehicleSeat(vehicle, -1) == ped then
gear = GetVehicleCurrentGear(vehicle)
-- Entered vehicle
if lastVehicle ~= vehicle then
lastVehicle = vehicle
maxGear = GetVehicleHighGear(vehicle)
maxMode = 2 + maxGear
-- Use current gear | neutral
if gear >= 1 then
mode = gear + 2
else
mode = 2
end
end
-- Gear up | down
if IsControlJustPressed(0, 172) then
nextMode = math.min(mode + 1, maxMode)
elseif IsControlJustPressed(0, 173) then
nextMode = math.max(mode - 1, 1)
else
nextMode = mode
end
-- On Shift
if nextMode ~= mode then
mode = nextMode
end
OnTick()
elseif lastVehicle then
lastVehicle = false
mode = false
end
Wait(0)
end
end)
--------------------------------------------------------------------------------
function DrawText2d (text, x, y, width, height)
SetTextEntry("STRING")
width = width or 1.0
height = height or width or 1.0
for k = 0, #text, 99 do
AddTextComponentString(string.sub(text, k, k + 98))
end
SetTextScale(width, height)
DrawText(x or 0.0, y or 0.0)
end
Citizen.CreateThread(function ()
local gear
local text
local modeText = { 'R', 'N', 'D' }
while true do
if mode then
if mode >=3 then
text = modeText[3]
gear = mode - 2
else
text = modeText[mode]
gear = mode - 1 -- GetVehicleCurrentGear(vehicle)
end
DrawText2d(text .. ':' .. gear)
end
Wait(0)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment