16
Script Releases / Re: [Snippet]Player Fps
« on: September 20, 2014, 12:14:28 am »
There seems to be a bug in the script module where the player instance is not the same in onPlayerPart the as the one used in onPlayerJoin and causes en error when the player leaves. I'll report this issue and see why this happens. The following works but there's an error when you disconnect and the previous player instance isn't removed:
BTW: The FPS value updates once every 10 seconds internaly, so there's no reason to draw something every second. I've set the draw function to run every 5 seconds instead of 10 to not risk printing an outdated value because the start point of our timer doesn't match with the internal timer on the FPS update function. This should make the draw function have on foot in the previous time lapse and another in the next time lapse but never have both feet in the same time lapse (so to speak).
EDIT: Updated the code to comply with the limitation specified above. Certainly looks more awkward but does the job.
Code: [Select]
local g_FPS = [];
function DrawFPS()
{
foreach (val in g_FPS)
{
if (val.Text != null) val.Text.Delete();
val.Text = CreateTextdraw("FPS: " + val.Player.FPS, 0, 0, 0xFFB0B0B0);
if (val.Text != null) val.Text.ShowForPlayer(val.Player);
}
}
function onServerStart()
{
NewTimer("DrawFPS", 5000, 0);
}
function onPlayerJoin(i_player)
{
g_FPS.push({Player = i_player, Text = null});
}
function onPlayerPart(i_player, reason)
{
for (local i = g_FPS.len()-1; i >= 0; i--)
{
if (g_FPS[i].Player.ID == i_player.ID)
{
if (g_FPS[i].Text != null) g_FPS[i].Text.Delete();
g_FPS.remove(i);
break;
}
}
}
BTW: The FPS value updates once every 10 seconds internaly, so there's no reason to draw something every second. I've set the draw function to run every 5 seconds instead of 10 to not risk printing an outdated value because the start point of our timer doesn't match with the internal timer on the FPS update function. This should make the draw function have on foot in the previous time lapse and another in the next time lapse but never have both feet in the same time lapse (so to speak).
EDIT: Updated the code to comply with the limitation specified above. Certainly looks more awkward but does the job.