No, it doesn't cause lags. (max 2 seconds in a sux dedicated server)
Tested by 5 or more servers with 74~500 players online
Ok, I agree, just explain now why my LUA is faster than C++ in this case
No no, I'm serious... If you were at least experienced and know any differences between Lua and C++ you would understand but you just don't know what you doing and saying.
I can simply explain in few sentences why:
1. Lua is way slower and uses more resources to count every player in-game and then to save player and after save player save his house.
2. TFS server engine is wrote in c++ language this means it is even faster than Lua and default server save going right to engine core and all counting and save is made in TFS engine.
You see we use Lua scripting language to avoid hardcore codings in c++. This should be simple and easy to understand for everybody now.
instead of having global saves every X time, why don't you have individual saves every X time?
when a player logs in, you give him an onThink event that after X time the player is saved, that way each player is individually saved independently and it wont cause any lag at all since the chance that 2 or more players will save at the exact same time is next to 0
function saveRepeat(cid)
if not isPlayer(cid) then return true end
doPlayerSave(cid)
doPlayerSendTextMessage(cid,19, 'Seu Char foi salvo automaticamente.')
doSendMagicEffect(getThingPos(cid), 65)
save = addEvent(saveRepeat, math.random(20,30) *60*1000, cid)
end
function onLogin(cid)
save = addEvent(saveRepeat, math.random(20,30)*60*1000, cid)
return true
end
function onLogout(cid)
stopEvent(save)
return true
end
-- Local config
local timetosave = 30000 -- It's time to save after broadcast, delay 30 seconds
local delay = 5000 -- It's time to save odd after pair, delay 5 seconds
local function save()
for _, cid in ipairs(getPlayersOnline()) do
if getPlayerGUID(cid) % 2 == 0 then
savePlayer(cid)
else
addEvent(savesavePlayer, delay, cid)
end
end
end
local function savePlayer(cid)
doPlayerSave(cid)
doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))})
return true
end
function onThink(interval, lastExecution, thinkInterval)
addEvent(save, timetosave)
doBroadcastMessage("Server save within 30 seconds, please mind it may freeze.")
end
Exactly what I mean, but using an onThink script instead of addEvent, that way you don't have to do that check if the player still exists nor the stopEvent if he logs out(if isPlayer(cid) ...)I use this creature script in my server :
Code:function saveRepeat(cid) if not isPlayer(cid) then return true end doPlayerSave(cid) doPlayerSendTextMessage(cid,19, 'Seu Char foi salvo automaticamente.') doSendMagicEffect(getThingPos(cid), 65) save = addEvent(saveRepeat, math.random(20,30) *60*1000, cid) end function onLogin(cid) save = addEvent(saveRepeat, math.random(20,30)*60*1000, cid) return true end function onLogout(cid) stopEvent(save) return true end
@lucas proposal
I was looking at ur code and I see it having 2 problems
first is that you're getting all players twice, which can be rendered unnecessary(see code below)
second, you're repeating code for the odd/pair function just because of the comparison, this bothers me so much
I think this code is much more elegantLua:-- Local config local timetosave = 30000 -- It's time to save after broadcast, delay 30 seconds local delay = 5000 -- It's time to save odd after pair, delay 5 seconds local function save() for _, cid in ipairs(getPlayersOnline()) do if getPlayerGUID(cid) % 2 == 0 then savePlayer(cid) else addEvent(savesavePlayer, delay, cid) end end end local function savePlayer(cid) doPlayerSave(cid) doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))}) return true end function onThink(interval, lastExecution, thinkInterval) addEvent(save, timetosave) doBroadcastMessage("Server save within 30 seconds, please mind it may freeze.") end
- - - Updated - - -
Exactly what I mean, but using an onThink script instead of addEvent, that way you don't have to do that check if the player still exists(if isPlayer(cid) ...)
@lucas proposal
I was looking at ur code and I see it having 2 problems
first is that you're getting all players twice, which can be rendered unnecessary(see code below)
second, you're repeating code for the odd/pair function just because of the comparison, this bothers me so much
I think this code is much more elegantLua:-- Local config local timetosave = 30000 -- It's time to save after broadcast, delay 30 seconds local delay = 5000 -- It's time to save odd after pair, delay 5 seconds local function save() for _, cid in ipairs(getPlayersOnline()) do if getPlayerGUID(cid) % 2 == 0 then savePlayer(cid) else addEvent(savesavePlayer, delay, cid) end end end local function savePlayer(cid) doPlayerSave(cid) doSaveHouse({getHouseByPlayerGUID(getPlayerGUID(cid))}) return true end function onThink(interval, lastExecution, thinkInterval) addEvent(save, timetosave) doBroadcastMessage("Server save within 30 seconds, please mind it may freeze.") end
- - - Updated - - -
Exactly what I mean, but using an onThink script instead of addEvent, that way you don't have to do that check if the player still exists nor the stopEvent if he logs out(if isPlayer(cid) ...)
I will write a code how I do imagine how should be with less lag. Just because I started to complicate this thread.