-
Notifications
You must be signed in to change notification settings - Fork 182
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
feat(server): full vehicle persistence #621
base: main
Are you sure you want to change the base?
Changes from 2 commits
9d87f03
f00aa1c
f6e2391
50b3523
52605fd
a5189ae
72f0be4
0cdc565
4dc64af
8454ba5
c820fe8
2b1868c
d7f08cf
7318687
943513e
3bfedb8
7f5925c
6982f28
9366980
2996d23
c302631
7905660
5618df4
4fc475f
d2c326b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
local persistence = GetConvarInt('qbx:enableVehiclePersistence', 0) | ||
print('Vehicle persistence mode ' .. persistence) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete |
||
---A persisted vehicle will respawn when deleted. Only works for player owned vehicles. | ||
---Vehicles spawned using lib are automatically persisted | ||
---@param vehicle number | ||
|
@@ -15,7 +18,7 @@ end | |
|
||
exports('DisablePersistence', DisablePersistence) | ||
|
||
if GetConvar('qbx:enableVehiclePersistence', 'false') == 'false' then return end | ||
if persistence == 0 then return end | ||
|
||
assert(lib.checkDependency('qbx_vehicles', '1.4.1', true)) | ||
|
||
|
@@ -29,6 +32,7 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) | |
local vehicleId = getVehicleId(vehicle) | ||
if not vehicleId then return end | ||
|
||
local coords = nil | ||
local props = exports.qbx_vehicles:GetPlayerVehicle(vehicleId)?.props | ||
if not props then return end | ||
|
||
|
@@ -75,8 +79,15 @@ RegisterNetEvent('qbx_core:server:vehiclePropsChanged', function(netId, diff) | |
props.tyres = damage | ||
end | ||
|
||
if persistence == 2 then | ||
local entityCoords = GetEntityCoords(vehicle) | ||
local entityHeading = GetEntityHeading(vehicle) | ||
coords = vec4(entityCoords.x, entityCoords.y, entityCoords.z, entityHeading) | ||
end | ||
|
||
exports.qbx_vehicles:SaveVehicle(vehicle, { | ||
props = props, | ||
coords = coords | ||
}) | ||
end) | ||
|
||
|
@@ -128,4 +139,31 @@ AddEventHandler('entityRemoved', function(entity) | |
local passenger = passengers[i] | ||
SetPedIntoVehicle(passenger.ped, veh, passenger.seat) | ||
end | ||
end) | ||
end) | ||
|
||
if persistence == 1 then return end | ||
|
||
local spawned = false | ||
|
||
local function spawnVehicles() | ||
spawned = true | ||
local vehicles = exports.qbx_vehicles:GetPlayerVehicles({ states = 0 }) | ||
for _, vehicle in ipairs(vehicles) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vehicles is an array, use a numeric for loop instead of this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All done |
||
if not vehicle.coords then return end | ||
|
||
local coords = vector3(vehicle.coords.x, vehicle.coords.y, vehicle.coords.z) | ||
local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicle.id) | ||
if not playerVehicle then return end | ||
|
||
local _, veh = qbx.spawnVehicle({ spawnSource = coords, model = playerVehicle.props.model, props = playerVehicle.props}) | ||
exports.qbx_core:EnablePersistence(veh) | ||
Entity(veh).state:set('vehicleid', vehicle.id, false) | ||
SetVehicleDoorsLocked(veh, 2) | ||
SetEntityHeading(veh, vehicle.coords.w) | ||
end | ||
end | ||
|
||
RegisterNetEvent('QBCore:Server:OnPlayerLoaded', function() | ||
local players = exports.qbx_core:GetQBPlayers() | ||
if not spawned and #players == 1 then spawnVehicles() end | ||
end) | ||
Manason marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change. Either keep the convar as a string an overload the meaning (right now values are 'true' or 'false. We could add 'full' as well. OR add a new true/false convar which controls whether vehicles are persisted between restarts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, values
"true"
and"false"
are interpreted as1
and0
respectively byGetConvarInt
. But at this point this can actually be replaced by the newGetConvarBool
I believe, which is available since artifact 10543 — we require 10731.