Optimized saving of player position

The changes I applied to the code regarding self.getCoords(), changing the conditions when joining the server at coordinates to userData.coords and getting rid of the self.updateCoords() function. I believe that my changes when saving are more efficient, because the player's position is not overwritten unnecessarily every second if he changes his position, but the position is saved when leaving the server to the database, and then loaded when entering the server.
This commit is contained in:
Hubert
2023-06-07 21:53:45 +02:00
parent fca0756c65
commit f2e0441538
2 changed files with 11 additions and 24 deletions
+8 -21
View File
@@ -47,33 +47,20 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
SetEntityHeading(Ped, vector.w) SetEntityHeading(Ped, vector.w)
end end
function self.updateCoords() function self.getCoords(vector)
SetTimeout(1000,function() local ped = GetPlayerPed(self.source)
local Ped = GetPlayerPed(self.source) local coords = GetEntityCoords(ped)
if DoesEntityExist(Ped) then
local coords = GetEntityCoords(Ped) if vector then
local distance = #(coords - vector3(self.coords.x, self.coords.y, self.coords.z)) return coords
if distance > 1.5 then else
local heading = GetEntityHeading(Ped) return {
self.coords = {
x = coords.x, x = coords.x,
y = coords.y, y = coords.y,
z = coords.z, z = coords.z,
heading = heading or 0.0
} }
end end
end end
self.updateCoords()
end)
end
function self.getCoords(vector)
if vector then
return vector3(self.coords.x, self.coords.y, self.coords.z)
else
return self.coords
end
end
function self.kick(reason) function self.kick(reason)
DropPlayer(self.source, reason) DropPlayer(self.source, reason)
+2 -2
View File
@@ -309,7 +309,7 @@ function loadESXPlayer(identifier, playerId, isNew)
xPlayer.triggerEvent('esx:playerLoaded', xPlayer.triggerEvent('esx:playerLoaded',
{ {
accounts = xPlayer.getAccounts(), accounts = xPlayer.getAccounts(),
coords = xPlayer.getCoords(), coords = userData.coords,
identifier = xPlayer.getIdentifier(), identifier = xPlayer.getIdentifier(),
inventory = xPlayer.getInventory(), inventory = xPlayer.getInventory(),
job = xPlayer.getJob(), job = xPlayer.getJob(),
@@ -331,7 +331,7 @@ function loadESXPlayer(identifier, playerId, isNew)
else else
exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory) exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory)
end end
xPlayer.updateCoords()
xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands) xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands)
print(('[^2INFO^0] Player ^5"%s"^0 has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId)) print(('[^2INFO^0] Player ^5"%s"^0 has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId))
end end