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 3 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
41 changes: 27 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,30 @@ 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

while state.setVehicleProperties do
if NetworkGetEntityOwner(vehicle) == cache.playerId then
for i = -1, 0 do
local ped = GetPedInVehicleSeat(vehicle, i)
if ped ~= cache.ped and ped > 0 then
DeleteEntity(ped)
end
end
solareon marked this conversation as resolved.
Show resolved Hide resolved
if lib.setVehicleProperties(vehicle, props) then
state:set('setVehicleProperties', nil, true)
end
end

Wait(0)
end
end)
end)
4 changes: 3 additions & 1 deletion modules/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,15 @@ if isServer then
SetPedIntoVehicle(ped, veh, -1)
end

---@diagnostic disable-next-line: unused-local
local owner = lib.waitFor(function()
local owner = NetworkGetEntityOwner(veh)
if owner ~= -1 then return owner end
end, 5000)
solareon marked this conversation as resolved.
Show resolved Hide resolved

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

return netId
end
else
Expand Down
Loading