Author Topic: [Snippet]Player Fps  (Read 5872 times)

0 Members and 2 Guests are viewing this topic.

Offline Flockshot

  • Street Thug
  • *
  • Posts: 29
    • View Profile
[Snippet]Player Fps
« on: September 19, 2014, 09:48:03 pm »
Many People view there Fps during the game without using any other software so this is a very simple function to do so

Code: [Select]
//Global Variable
local fps = [];

//Fps Function
function PlayerFps()
{
foreach (val in fps)
{
if (val.Text != null) val.Text.Delete();
val.Text = CreateTextdraw("FPS: " + val.Player.FPS.tointeger(), 0, 0, 0xFFB0B0B0);
if (val.Text != null) val.Text.ShowForPlayer(val.Player);
}

}

//On Server Start
function onServerStart()
{
NewTimer("PlayerFps", 5000, 0);
}

function onPlayerJoin(player)
{
fps.push({Player = player, Text = null});
}

function onPlayerPart(player, reason)
{
for (local i = fps.len()-1; i >= 0; i--)
{
if (fps[i].Player.ID == player.ID)
{
if (fps[i].Text != null) fps[i].Text.Delete();
fps.remove(i);
break;
}
}
}


Note:
Global Variable have to be outside any function.
Global variable have to be in the same file in which it is declared.


Simple Option to raise fps will be to turn of frame limiter in display options of gta vc settings.

Credits:S.L.C as he gave simpler function than me.

Screenshots







« Last Edit: September 20, 2014, 04:38:33 am by Flockshot »

Offline S.L.C

  • Street Thug
  • *
  • Posts: 42
  • Sorry if you weren't impressed!
    • View Profile
Re: [Snippet]Player Fps
« Reply #1 on: September 19, 2014, 10:00:47 pm »
You really need to revise the current implementation. If you require an array element for each second that the server is running then you seriously need to take a look at your code from a different perspective:
Code: [Select]
fps <- array(86400,null);//86400 stand for seconds in a day so if u r going to run server continuously for more than 1 day than change it.

Offline Flockshot

  • Street Thug
  • *
  • Posts: 29
    • View Profile
Re: [Snippet]Player Fps
« Reply #2 on: September 19, 2014, 10:18:31 pm »
You really need to revise the current implementation. If you require an array element for each second that the server is running then you seriously need to take a look at your code from a different perspective:
Code: [Select]
fps <- array(86400,null);//86400 stand for seconds in a day so if u r going to run server continuously for more than 1 day than change it.

Cant find another way to delete the previous textdraw.Can u suggest any.

Offline S.L.C

  • Street Thug
  • *
  • Posts: 42
  • Sorry if you weren't impressed!
    • View Profile
Re: [Snippet]Player Fps
« Reply #3 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:
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.
« Last Edit: September 20, 2014, 02:13:51 am by S.L.C »

Offline Flockshot

  • Street Thug
  • *
  • Posts: 29
    • View Profile
Re: [Snippet]Player Fps
« Reply #4 on: September 20, 2014, 04:21:03 am »
Well then i will update the snippet as u tell.

BTW:What does the foreach loop and the arr.push do.

I dont know about them as i learned c++ and it dont contain them.
« Last Edit: September 20, 2014, 04:39:54 am by Flockshot »

Offline S.L.C

  • Street Thug
  • *
  • Posts: 42
  • Sorry if you weren't impressed!
    • View Profile
Re: [Snippet]Player Fps
« Reply #5 on: September 20, 2014, 04:55:31 am »
foreach

Code: [Select]
'foreach' '(' [index_id','] value_id 'in' exp ')' stat
Executes a statement for every element contained in an array, table, class, string or generator. If exp is a generator it will be resumed every iteration as long as it is alive; the value will be the result of 'resume' and the index the sequence number of the iteration starting from 0.
   
Code: [Select]
local a=[10,23,33,41,589,56]
foreach(idx,val in a)
    print("index="+idx+" value="+val+"\n");
//or
foreach(val in a)
    print("value="+val+"\n");

Similar to iterators in standard C++ or the new range based for loop in C++11.


Array

push(val)

appends the value ‘val’ at the end of the array

Similar to push_back() in std::vector or related containers in C++.


References taken from Squirrel 3.0 Reference Manual
« Last Edit: September 20, 2014, 04:59:07 am by S.L.C »

Offline Flockshot

  • Street Thug
  • *
  • Posts: 29
    • View Profile
Re: [Snippet]Player Fps
« Reply #6 on: September 20, 2014, 06:13:18 am »
foreach

Code: [Select]
'foreach' '(' [index_id','] value_id 'in' exp ')' stat
Executes a statement for every element contained in an array, table, class, string or generator. If exp is a generator it will be resumed every iteration as long as it is alive; the value will be the result of 'resume' and the index the sequence number of the iteration starting from 0.
   
Code: [Select]
local a=[10,23,33,41,589,56]
foreach(idx,val in a)
    print("index="+idx+" value="+val+"\n");
//or
foreach(val in a)
    print("value="+val+"\n");

Similar to iterators in standard C++ or the new range based for loop in C++11.


Array

push(val)

appends the value ‘val’ at the end of the array

Similar to push_back() in std::vector or related containers in C++.


References taken from Squirrel 3.0 Reference Manual
Thanks