Author Topic: Player loop, new method  (Read 2818 times)

Shadow.

  • Tester
  • Full Member
  • ****
  • Posts: 144
  • Karma: +16/-35
    • View Profile
Player loop, new method
« on: November 14, 2012, 03:41:25 pm »
This is a loop-through method using arrays tested by me, ( with myself... duh.. )! The good thing is that this method loops only through the valid player instances, and not some unfilled slot ( like for( local i = 0; i < GetMaxPlayers(); i++ ) ).

Also, morphine shown me a method using constructor and classes, but I thought this would be easier to teach, so here it is.

I will show it in an example:

Code: [Select]
playerson <- [];

function onPlayerJoin( player )
{
playerson.push( player.ID );
}

function onPlayerPart( player, reason )
{
playerson.remove( player.ID );
}


function HealAll()
{
foreach(playerid, val in playerson )
{
print( "CLIENT NAME: " + FindPlayer(playerid).Name );
}
}

function onPlayerCommand( player, cmd, params )
{
if( cmd == "printall" )
{
HealAll();
}
}

Output:

Quote
CLIENT NAME: yournamegoeshere
CLIENT NAME: yournamegoeshere
etc..

What happens if there is no player in the server ?

Nothing will happen, no message from the server.

Can be modified at your will, some credits won't hurt anyone.

Vortrex

  • Full Member
  • ***
  • Posts: 267
  • Karma: +54/-73
    • View Profile
Re: Player loop, new method
« Reply #1 on: November 14, 2012, 05:30:43 pm »
Nice! This will definitely help new scripters with proper looping methods.

 

© Liberty Unleashed Team.