megaman54 Posted January 28, 2011 Share Posted January 28, 2011 What functions i should use to make server uptime counter that is viewable in-game? Link to comment
SDK Posted January 28, 2011 Share Posted January 28, 2011 setTimer to count the uptime in a variable, viewable in-game depends on your needs. A command or a real counter on the screen? A command is easily done with outputChatBox. If a real counter is wanted, send the variable to the client using triggerClientEvent, and use another setTimer to update it there. Display it using dxDrawText in the onClientRender event. local uptime = 0 setTimer(function() uptime = uptime + 1 end, 1000, 0) -- command addCommandHandler outputChatBox -- on screen triggerServerEvent addEvent addEventHandler setTimer dxDrawText ( in "onClientRender" ) Link to comment
megaman54 Posted January 28, 2011 Author Share Posted January 28, 2011 Both, command and real counter are OK. But i dont really understand your code EDIT: Lol now i get it! Thanks for the fast reply I thought this would be more complicated than this but it wasn't Nice! Thanks again ! Link to comment
SDK Posted January 28, 2011 Share Posted January 28, 2011 Ah well, it wasn't really a script. I gave you the functions you needed ( like you asked ) This could be the uptime command: local uptime = 0 setTimer(function() uptime = uptime + 1 end, 1000, 0) addCommandHandler('uptime', function(player) outputChatBox ( "Current server uptime: " .. tostring (uptime) .. " seconds", player) end) Link to comment
megaman54 Posted January 28, 2011 Author Share Posted January 28, 2011 Well, i already made a simple command but yours looks only a bit different. My code: local uptime = 0 setTimer(function() uptime = uptime + 1 end, 1000, 0) function showUptime(player) outputChatBox("Server uptime: " ..uptime.. " seconds", player, 0, 255, 0) end addCommandHandler("uptime", showUptime) EDIT: How to convert the seconds into days, months, years... etc ? Link to comment
SDK Posted January 28, 2011 Share Posted January 28, 2011 Your script is the same as mine, ignore that You'll have to calc the amount of days, hours, minutes and so on out the seconds counter yourself, shouldnt be that hard when you think about it. Link to comment
MCvarial Posted January 28, 2011 Share Posted January 28, 2011 You could also use getTickCount(), current tick - start tick should do the trick. Link to comment
eAi Posted January 29, 2011 Share Posted January 29, 2011 Using a timer like that is really bad for two reasons: - setTimer doesn't guarantee that it'll fire after exactly 1000ms. It says that it'll fire after 1000ms, but it could be 1030ms or so, depending how fast your server is running. As a consequence, you're going to overestimate your uptime fairly significantly, adding roughly 12 minutes per day if your server runs at 60fps. - It's also very wasteful triggering a function every second, and completely unnecessary. As MCvarial says, you should use getTickCount(). Record the value of it when you start the server then just subtract that from the current value when you want to show it. Link to comment
megaman54 Posted January 29, 2011 Author Share Posted January 29, 2011 When i first started thinking about this, i thought i should use getTickCount() but now, i know it can also done with a timer. As eAi said, timer is unaccurate in this kind of use, then i will try to use getTickCount() as a replacement. Thanks. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now