Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: vehicle props as statebag #412

Merged
merged 5 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
solareon marked this conversation as resolved.
Show resolved Hide resolved
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('setVehicleProperties', props, true)
Entity(veh).state:set('initVehicle', true, true)
solareon marked this conversation as resolved.
Show resolved Hide resolved
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
Loading