Vice City Multiplayer

VC:MP 0.4 (Beta) => Script Discussion => Script Releases => Topic started by: Flockshot on September 19, 2014, 09:48:03 pm

Title: [Snippet]Player Fps
Post by: Flockshot 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


(http://i1348.photobucket.com/albums/p730/msomaan/Screenshot1_zps685c6a27.png) (http://s1348.photobucket.com/user/msomaan/media/Screenshot1_zps685c6a27.png.html)


(http://i1348.photobucket.com/albums/p730/msomaan/Screenshot2_zpsdcb18eca.png) (http://s1348.photobucket.com/user/msomaan/media/Screenshot2_zpsdcb18eca.png.html)

Title: Re: [Snippet]Player Fps
Post by: S.L.C 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.
Title: Re: [Snippet]Player Fps
Post by: Flockshot 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.
Title: Re: [Snippet]Player Fps
Post by: S.L.C 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 (http://forum.vicecitymultiplayer.com/index.php?topic=6942.0) 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.
Title: Re: [Snippet]Player Fps
Post by: Flockshot 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.
Title: Re: [Snippet]Player Fps
Post by: S.L.C 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 (http://www.squirrel-lang.org/doc/squirrel3.html)
Title: Re: [Snippet]Player Fps
Post by: Flockshot 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 (http://www.squirrel-lang.org/doc/squirrel3.html)
Thanks