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

feat: entity session id #582

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ QBX.UsableItems = {}
local vehicleClasses
local vehicleClassesPromise

local currentSessionId = 0

---Sets a unique sessionId statebag on the entity.
---@param entity number
---@return integer sessionId
local function createSessionId(entity)
currentSessionId += 1
local sessionId = currentSessionId
Entity(entity).state:set('sessionId', sessionId, true)
return sessionId
end

exports('CreateSessionId', createSessionId)

---Caches the vehicle classes the first time this is called by getting the data from a random client.
---Throws an error if there is no cache and no client is connected to get the data from.
---@param model number
Expand Down
5 changes: 5 additions & 0 deletions server/vehicle-persistence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ AddEventHandler('entityRemoved', function(entity)

local vehicleId = getVehicleId(entity)
if not vehicleId then return end

local sessionId = Entity(entity).state.sessionId
local playerVehicle = exports.qbx_vehicles:GetPlayerVehicle(vehicleId)

if DoesEntityExist(entity) then
Expand All @@ -119,6 +121,9 @@ AddEventHandler('entityRemoved', function(entity)
props = playerVehicle.props
})

Entity(veh).state:set('sessionId', sessionId, true)
Entity(veh).state:set('vehicleid', vehicleId, false)

for i = 1, #passengers do
local passenger = passengers[i]
SetPedIntoVehicle(passenger.ped, veh, passenger.seat)
Expand Down