A Function Improvement Snippet

Started by !, Oct 02, 2017, 08:30 AM

Previous topic - Next topic

!

I've seen some users using this function
for( local i = 0; i <= GetMaxPlayers(); i++ )
{
   local plr = FindPlayer( i );
   if ( plr ) print("found");
}
Suppose there are 10 player online while the max players is 100 so this function is using the memory for no work.
for( local i = 0; i <= GetMaxPlayers(); i++ )
{
   local plr = FindPlayer( i );
   if ( plr ) print("found");
   else print("wasting :P");
}

This simple snippet helps to remove those useless calling.This snippet is very usefull for those who are using global timers for players eg. antibank killing( almost everyone uses a timer for it but looking at published scripts GetMaxPlayers() has been used in it which according to me is a bad idea )

[spoiler=Installation]
This goes at the top of main.nut file
/* A global constant/array which stores IDs of online players */
PlrsOnline <- [ ];
/* A global constant/array which handels the functions */
Players <- { };

This goes at the top of onPlayerJoin( player ) function
/* Lets adds the ID of player to the online players list */
Players.Inc( player.ID );

This goes at the top of onPlayerPart( player, reason ) function
/* Remove the ID of player from the online players list */
Players.Dec( player.ID );

These functions can be pasted at any place in the script
/* This functions push the ID to the array */
Players.Inc <- function( Plr_ID ) {
   /* Check is id already available in array, return if it's available */
   if ( PlrsOnline.find( Plr_ID ) != null ) return 0; // Just for precautionary measurement if not removed due to any reason
   /* If it's not available push the ID to the array */
   PlrsOnline.push( Plr_ID );
}
/* This functions removes the ID from the array */
Players.Dec <- function( Plr_ID ) {
   /* Check is id already available in array, return if it's available */
   if ( PlrsOnline.find( Plr_ID ) == null ) return 0; // once again it's for precautionary measurement
/* If it's available remove the ID from array */
PlrsOnline.remove( PlrsOnline.find( Plr_ID ) );
}
[/spoiler]

[spoiler=Usage]
foraceh( iPlrID in PlrsOnline )
{
   local plr = FindPlayer( iPlrID );
   if ( plr ) print("found");
}

[spoiler=Usage(healing_all)]
foraceh( iPlrID in PlrsOnline )
{
   local plr = FindPlayer( iPlrID );
   if ( plr ) plr.Health = -1;
}
[/spoiler]

[spoiler=Usage(getting_player_by_specific_data/stats)]
Lets suppose we want to get all players having hp less then 50%
foraceh( iPlrID in PlrsOnline )
{
   local plr = FindPlayer( iPlrID );
   if ( plr && plr.Health <= 50 ) print( "Found player "+plr.Name+" with ID: "+plr.ID+" having HP: "+plr.Health+"" );
}
[/spoiler]

[/spoiler]

Discord: zeus#5155

DizzasTeR

#1
Uh

g_Players <- {};

function onPlayerJoin( player ) {
    g_Players.rawset( player.ID, player );
}

function onPlayerPart( player, reason ) {
    g_Players.rawdelete( player.ID );
}

foreach( pID, pInstance in g_Players ) {
    print( "Player: " + pInstance.Name + " - ID: " + pID );
}