Skip to content

Instantly share code, notes, and snippets.

@ihyajb
Created December 26, 2023 19:02
Show Gist options
  • Save ihyajb/3c518c56b3c5b2dd63e96b91e93f3277 to your computer and use it in GitHub Desktop.
Save ihyajb/3c518c56b3c5b2dd63e96b91e93f3277 to your computer and use it in GitHub Desktop.
carscaleform.lua
--* idc if you use this in public / private script *--
--* But if you use *most / all* of my code credit would be nice 💖 *--
local MaxCars = 20 --Max is a HARD CAP OF 20, only make this number lower!
local function func_7050(string)
local scaleform = RequestScaleformMovie(string)
repeat
Wait(100)
until HasScaleformMovieLoaded(scaleform)
return scaleform
end
--? [BRAND] = YTD_NAME (Support for custom logos and shit)?--
local RandomBrands = {
['LCC'] = 'MPCarHUD2',
['GROTTI_2'] = 'MPCarHUD2',
['PROGEN'] = 'MPCarHUD2',
['RUNE'] = 'MPCarHUD2',
['VYSSER'] = 'MPCarHUD3',
['MAXWELL'] = 'MPCarHUD4',
}
local RequestedYTDs = {'MPCarHUD', 'MPCarHUD2', 'MPCarHUD3', 'MPCarHUD4'}
local function LocateBrandYTD(model)
local YTD = 'MPCarHUD'
local brand = GetLabelText(GetMakeNameFromVehicleModel(model))
--! Stupid Failsafe or whatever !--
if brand == 'NULL' then
return {
YTD = '',
name = ''
}
end
if RandomBrands[brand] then YTD = RandomBrands[brand] end
return {
YTD = YTD,
name = brand
}
end
--? I have no idea if any of this is right lol
local function func_7045(vehicle)
local VehicleData = {}
local fVar1 = 100.0
local div = 4
local model = GetEntityModel(vehicle)
VehicleData.MaxSpeed = GetVehicleEstimatedMaxSpeed(vehicle) * 2.236936 / 1.2
VehicleData.MaxAcceleration = GetVehicleAcceleration(vehicle) * 200
VehicleData.MaxBreaking = GetVehicleMaxBraking(vehicle) * 100 / 2.5
VehicleData.Traction = GetVehicleMaxTraction(vehicle) * 100 / 3.5
VehicleData.Name = GetLabelText(GetDisplayNameFromVehicleModel(model))
VehicleData.Brand = LocateBrandYTD(model)
return VehicleData
end
local function CallScaleformMethod(scaleform, method, ...)
local t
local args = { ... }
BeginScaleformMovieMethod(scaleform, method)
for k, v in ipairs(args) do
t = type(v)
if t == 'string' then
PushScaleformMovieMethodParameterString(v)
elseif t == 'number' then
if string.match(tostring(v), "%.") then
PushScaleformMovieFunctionParameterFloat(v)
else
PushScaleformMovieFunctionParameterInt(v)
end
elseif t == 'boolean' then
PushScaleformMovieMethodParameterBool(v)
end
end
EndScaleformMovieMethod()
end
local DisplayingStats = false
local function GetStatSlot(a)
local base = 'MP_CAR_STATS_'
local offset = '0'
if a >= 10 then
offset = ''
end
return tostring(base..''..offset..''..a)
end
local function UnloadAll(VehicleArray)
for i = 1, #VehicleArray, 1 do
SetScaleformMovieAsNoLongerNeeded(VehicleArray.scaleform)
end
for _, v in ipairs(RequestedYTDs) do
--print('Unloading YTD: '..v)
SetStreamedTextureDictAsNoLongerNeeded(v)
end
end
local function DisplayVehicleStats(vehicles)
local ped = PlayerPedId()
for _, v in ipairs(RequestedYTDs) do
--print('Requesting YTD: '..v)
RequestStreamedTextureDict(v, false)
end
local VehicleArray = {}
local scaleformOffset = 0
local PedCoords = GetEntityCoords(ped)
for index, value in ipairs(vehicles) do
--! Dont count cars far away!
local dist = #(PedCoords - GetEntityCoords(value))
if dist < 50 and IsVehicleSeatFree(value, -1) and #VehicleArray < MaxCars then
scaleformOffset = scaleformOffset + 1
VehicleArray[#VehicleArray+1] = {
entity = value,
data = func_7045(value),
scaleform = func_7050(GetStatSlot(scaleformOffset))
}
end
end
--print('Cached '..#VehicleArray..' cars that *could* display')
CreateThread(function()
while DisplayingStats do
for i = 1, #VehicleArray, 1 do
local vehicle = VehicleArray[i]
if DoesEntityExist(vehicle.entity) and GetEntityBoneIndexByName(vehicle.entity, 'windscreen') ~= -1 then
local dist = #(GetEntityCoords(ped) - GetEntityCoords(vehicle.entity))
if dist < 7 then
local DisplayCoords = GetWorldPositionOfEntityBone(vehicle.entity, GetEntityBoneIndexByName(vehicle.entity, 'windscreen'))
local CarRot = GetEntityRotation(vehicle.entity)
local CamRot = GetFinalRenderedCamRot(2)
CallScaleformMethod(vehicle.scaleform, 'SET_VEHICLE_INFOR_AND_STATS', vehicle.data.Brand.name..' '..vehicle.data.Name, "Insured", vehicle.data.Brand.YTD, vehicle.data.Brand.name, "Top Speed", "Acceleration", "Braking", "Traction", vehicle.data.MaxSpeed, vehicle.data.MaxAcceleration, vehicle.data.MaxBreaking, vehicle.data.Traction)
-- CallScaleformMethod(vehicle.scaleform, 'SET_PLAYER_NAME', 'github.com/ihyajb')
-- CallScaleformMethod(vehicle.scaleform, 'SET_CREW_NAME', '...LoveU')
DrawScaleformMovie_3dSolid(vehicle.scaleform,
DisplayCoords.x, DisplayCoords.y, DisplayCoords.z + 2.5,
CamRot.x, 0.0, CamRot.z,
0.0, 1.0, 0.0,
6.0, 4.0, 5.0,
0
)
end
end
end
Wait(10)
end
--print('No longer displaying!')
UnloadAll(VehicleArray)
VehicleArray = {}
end)
end
RegisterCommand('RequestVehicleState', function()
DisplayingStats = not DisplayingStats
if not DisplayingStats then return end
DisplayVehicleStats(GetGamePool('CVehicle'))
local ClickedTime = GetGameTimer()
CreateThread(function()
while DisplayingStats do
if GetGameTimer() - ClickedTime >= 6000 then DisplayingStats = false end
Wait(50)
end
end)
end)
RegisterKeyMapping('RequestVehicleState', 'Requests Close by Vehicle Stats', 'keyboard', 'LMENU')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment