-
Notifications
You must be signed in to change notification settings - Fork 169
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
Open
CodexisPhantom
wants to merge
12
commits into
Qbox-project:main
Choose a base branch
from
CodexisPhantom:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+80
−1
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9d87f03
feat: full vehicle persistence
CodexisPhantom f00aa1c
fix: vehicles spawning multiple times if 1 player
CodexisPhantom f6e2391
refactor: use onResourceStart event
CodexisPhantom 50b3523
fix: players never refreshed
CodexisPhantom 52605fd
Merge branch 'Qbox-project:main' into main
CodexisPhantom a5189ae
Merge branch 'Qbox-project:main' into main
CodexisPhantom 72f0be4
Merge branch 'Qbox-project:main' into main
CodexisPhantom 0cdc565
Merge branch 'Qbox-project:main' into main
CodexisPhantom 4dc64af
Merge branch 'Qbox-project:main' into main
CodexisPhantom 8454ba5
refactor: better persistence handling
CodexisPhantom c820fe8
fix: vehicles duplication
CodexisPhantom 2b1868c
Merge branch 'Qbox-project:main' into main
CodexisPhantom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
Comment on lines
+2
to
+3
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,32 @@ AddEventHandler('entityRemoved', function(entity) | |
local passenger = passengers[i] | ||
SetPedIntoVehicle(passenger.ped, veh, passenger.seat) | ||
end | ||
end) | ||
|
||
if persistence == 1 then return end | ||
|
||
local spawned = false | ||
|
||
local function spawnVehicles() | ||
local vehicles = exports.qbx_vehicles:GetPlayerVehicles({ states = 0 }) | ||
for _, vehicle in ipairs(vehicles) do | ||
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 spawned and #players ~= 1 then return end | ||
spawnVehicles() | ||
spawned = true | ||
Manason marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.