Skip to content

Commit

Permalink
Fix: Vehicle props not always being set
Browse files Browse the repository at this point in the history
* refactor: vehicle props as statebag

* fix: linter error

* refactor: add ped clearing

* refactor: move ped clearing

* refactor: zoo style baybay

Co-authored-by: Zoo <34508112+FjamZoo@users.noreply.github.com>

---------

Co-authored-by: Zoo <34508112+FjamZoo@users.noreply.github.com>
  • Loading branch information
solareon and FjamZoo committed Mar 13, 2024
1 parent 93be322 commit 4f09676
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
68 changes: 54 additions & 14 deletions client/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,6 @@ RegisterNetEvent('QBCore:Command:GoToMarker', function()
end)

-- Vehicle Commands
lib.callback.register('qbx_core:client:vehicleSpawned', function(netId, props)
local veh = NetworkGetEntityFromNetworkId(netId)

for i = -1, 0 do
local ped = GetPedInVehicleSeat(veh, i)
if ped ~= cache.ped and ped > 0 and NetworkGetEntityOwner(ped) == cache.playerId then
DeleteEntity(ped)
end
end

if props then
lib.setVehicleProperties(veh, props)
end
end)

lib.callback.register('qbx_core:client:getVehiclesInRadius', function(radius)
local vehicles = lib.getNearbyVehicles(GetEntityCoords(cache.ped), radius or 5, true)
Expand Down Expand Up @@ -208,3 +194,57 @@ RegisterNetEvent('QBCore:Client:OnSharedUpdateMultiple', function(tableName, val
end
TriggerEvent('QBCore:Client:UpdateObject')
end)

-- Set vehicle props
---@param vehicle number
---@param props table<any, any>
qbx.entityStateHandler('setVehicleProperties', function(vehicle, _, props)
if not props then return end

SetTimeout(0, function()
local state = Entity(vehicle).state

local timeOut = GetGameTimer() + 10000

while state.setVehicleProperties do
if NetworkGetEntityOwner(vehicle) == cache.playerId then
if lib.setVehicleProperties(vehicle, props) then
state:set('setVehicleProperties', nil, true)
end
end
if GetGameTimer() > timeOut then
break
end

Wait(50)
end
end)
end)

-- Clear vehicle peds
---@param vehicle number
---@param init boolean
qbx.entityStateHandler('initVehicle', function(vehicle, _, init)
if not init then return end

for i = -1, 0 do
local ped = GetPedInVehicleSeat(vehicle, i)
if ped ~= cache.ped and ped > 0 and NetworkGetEntityOwner(ped) == cache.playerId then
DeleteEntity(ped)
end
end

lib.waitFor(function()
return not IsEntityWaitingForWorldCollision(vehicle)
end)

if NetworkGetEntityOwner(vehicle) ~= cache.playerId then return end

local state = Entity(vehicle).state

SetVehicleOnGroundProperly(vehicle);

SetTimeout(0, function()
state:set('initVehicle', nil, true)
end)
end)
12 changes: 5 additions & 7 deletions modules/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ if isServer then
]]
---@param params LibSpawnVehicleParams
---@return integer netId
---@return number veh
function qbx.spawnVehicle(params)
local model = params.model
local source = params.spawnSource
Expand Down Expand Up @@ -260,14 +261,11 @@ if isServer then
SetPedIntoVehicle(ped, veh, -1)
end

local owner = lib.waitFor(function()
local owner = NetworkGetEntityOwner(veh)
if owner ~= -1 then return owner end
end, 5000)

Entity(veh).state:set('initVehicle', true, true)
Entity(veh).state:set('setVehicleProperties', props, true)
local netId = NetworkGetNetworkIdFromEntity(veh)
lib.callback.await('qbx_core:client:vehicleSpawned', owner, netId, props)
return netId

return netId, veh
end
else
---@class LibDrawTextParams
Expand Down

0 comments on commit 4f09676

Please sign in to comment.