I know you can get the class of any vehicle with GetVehicleClass
, but electric cars don’t have their own class and belong to classes shared with gas cars.
I can’t find any native that does this for you, but you can easily create one yourself. Make an array and fill it with the models you know are electric, and check if the model of a specific vehicles is inside the array.
Hi !
After checking the handling.meta
files I noticed that vehicles with only one drive gear were electric, so you can know if a vehicle is electric from this native: GetVehicleHandlingInt(GetVehiclePedIsIn(PlayerPedId(), false), 'CHandlingData', 'nInitialDriveGears')
, if the result is 1, the vehicle is electric, otherwise it is not.
I was trying to avoid doing that and wondering if there happens to be a more “future-proof” alternative. One thing I could do is get the class of the vehicle, check if it’s a car and check if it only has 1 gear. But I think I’m gonna follow your suggestion instead.
Thanks for the suggestion, I thought about doing this and actually just mentioned it in my comment above, which I was writing while you posted yours
I could also just use the GetHighGear native which would be an easier way to get the highest gear of a vehicle.
Now that you mention that there are no other non-electric vehicles that only have 1 gear, I trust this method more.
However, if a server with custom vehicles included a Porsche Taycan, this check would fail because those have two-speed transmissions (assuming the server had the car properly configured to match the real life thing). I guess the best thing I can do is check if the highest gear of a vehicle is 1, and also have an array containing names of vehicles which may differ when it comes to number of drive gears and check if the name of the vehicle is in it.
There is also this property in the handling <fOilVolume value="0.000000" />
which appears on electric vehicles (also on bicycles but I imagine that you check that it is a car and not a bicycle, a boat or whatever). May be a property that is more reliable than the gears on imported vehicles.
PS: The native GetVehicleOilLevel()
is in my opinion not to be used (in case a non-electric vehicle is out of oil) if you prefer to check according to the oil tank.
Bicycle and boats planes etc, should only have 1 gear.
I use this method:
-- Electric List
config = {
electricVehicles = {
`Imorgon`,
`Neon`,
`Raiden`,
`Cyclone`,
`Voltic`,
`Voltic2`,
`Tezeract`,
`Dilettante`,
`Dilettante2`,
`Airtug`,
`Caddy`,
`Caddy2`,
`Caddy3`,
`Surge`,
`Khamelion`,
`RCBandito`
}
}
-- CHECK THREAD
inElectric = false
Citizen.CreateThread(function()
while true do
Wait(2000)
local ped = PlayerPedId()
local pedVeh = GetVehiclePedIsIn(ped)
local vehModel = GetEntityModel(pedVeh)
if pedVeh ~= 0 and CheckElectric(vehModel) then
inElectric = true
else
inElectric = false
end
end
end)
-- FUNCTION
function CheckElectric(vehicle)
for k, v in pairs(config.electricVehicles) do
if GetHashKey(vehicle) == GetHashKey(v) then
return true
end
end
return false
end
I didn’t think about that. Thanks for another great suggestion.
Bicycle and boats planes etc, should only have 1 gear.
Right, but I’d check the class of the vehicle as well.
You seem to be using the same method FRB_Jop suggested. Thanks for including some code.
Sorry to revive an old topic, but in b3258/mp2024_01, there is a native available for this, so I wanted to record this somewhere.
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
local isElectric = Citizen.InvokeNative(0x1fcb07fe230b6639, GetEntityModel(vehicle)) == 1
Edit November 2024:
This native is now available in the docs: https://docs.fivem.net/natives/?_0x1FCB07FE230B6639=
Don’t use Citizen.InvokeNative
and instead use GetIsVehicleElectric
as it makes your code more descriptive. Remember this native is only available in game build 3258 or newer!
Example:
if GetGameBuildNumber() < 3258 then error("Game build must be 3258 or newer") end
local vehicle = GetVehiclePedIsIn(PlayerPedId(), false)
local isElectric = GetIsVehicleElectric(GetEntityModel(vehicle)) == 1
where is this located at?