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
+9 -22
View File
@@ -47,31 +47,18 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
SetEntityHeading(Ped, vector.w)
end
function self.updateCoords()
SetTimeout(1000,function()
local Ped = GetPlayerPed(self.source)
if DoesEntityExist(Ped) then
local coords = GetEntityCoords(Ped)
local distance = #(coords - vector3(self.coords.x, self.coords.y, self.coords.z))
if distance > 1.5 then
local heading = GetEntityHeading(Ped)
self.coords = {
x = coords.x,
y = coords.y,
z = coords.z,
heading = heading or 0.0
}
end
end
self.updateCoords()
end)
end
function self.getCoords(vector)
local ped = GetPlayerPed(self.source)
local coords = GetEntityCoords(ped)
if vector then
return vector3(self.coords.x, self.coords.y, self.coords.z)
return coords
else
return self.coords
return {
x = coords.x,
y = coords.y,
z = coords.z,
}
end
end
+2 -2
View File
@@ -309,7 +309,7 @@ function loadESXPlayer(identifier, playerId, isNew)
xPlayer.triggerEvent('esx:playerLoaded',
{
accounts = xPlayer.getAccounts(),
coords = xPlayer.getCoords(),
coords = userData.coords,
identifier = xPlayer.getIdentifier(),
inventory = xPlayer.getInventory(),
job = xPlayer.getJob(),
@@ -331,7 +331,7 @@ function loadESXPlayer(identifier, playerId, isNew)
else
exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory)
end
xPlayer.updateCoords()
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))
end