Liberty Unleashed

Scripting => Script Tutorials => Topic started by: Stoku on February 26, 2011, 02:55:12 am

Title: [TUTORIAL] "while" loop
Post by: Stoku on February 26, 2011, 02:55:12 am
Hello,
I got bored so I decided to write this short "tutorial" about "while" loop.

The while loop will be repeating as long as it's condition is false (eg. 1 > 0 = true, stop; 1 < 0 = false, continue executing).

The main syntax of the while loop is:
Code: (squirrel) [Select]
while ( condition )
{
code to execute
}


And below are some samples of use:

- Setting all players health to 100:
Code: (squirrel) [Select]
local x = 0;

while ( x < GetPlayers() )
{
local plr = FindPlayer( x );

if ( plr )
{
plr.Health = 100;
}
x++;
}

- Teleporting all players somewhere:
Code: (squirrel) [Select]
local x = 0;

while ( x < GetPlayers() )
{
local plr = FindPlayer( x );

if ( plr )
{
plr.Pos = Vector( 0, 0, 0 );
}
x++;
}

- Fixing every vehicle on the server:
Code: (squirrel) [Select]
local x = 0;

while ( x < GetVehicleCount() )
{
local veh = FindVehicle( x );

if ( veh )
{
veh.Fix();
}
x++;
}

Good luck with looping :).

PS. Samples weren't tested, so if there's something wrong, please inform me.
Title: Re: [TUTORIAL] "while" loop
Post by: Thijn on February 26, 2011, 11:16:35 am
I though vehicle IDs start with 1
Title: Re: [TUTORIAL] "while" loop
Post by: Stoku on February 26, 2011, 12:05:19 pm
I though vehicle IDs start with 1
Thanks, fixed :).
Title: Re: [TUTORIAL] "while" loop
Post by: Force on February 26, 2011, 03:30:27 pm
I though vehicle IDs start with 1
Thanks, fixed :).

Last I checked they start with 0, not 1.
Title: Re: [TUTORIAL] "while" loop
Post by: Stoku on February 26, 2011, 03:37:56 pm
I though vehicle IDs start with 1
Thanks, fixed :).

Last I checked they start with 0, not 1.
Lol, so I changed it again.
Title: Re: [TUTORIAL] "while" loop
Post by: Orpheus on February 26, 2011, 06:11:47 pm
I though vehicle IDs start with 1
Thanks, fixed :).

Last I checked they start with 0, not 1.
Lol, so I changed it again.
even if it HAD started at 1, you may aswell start the loop at 0, it'll just skip past the vehicle pointer and go straight to X++; so it would work even if the vehicle ID's started at 1 ;)
Title: Re: [TUTORIAL] "while" loop
Post by: himselfe on February 28, 2011, 09:54:53 pm
And here's a loop for people who aren't batshit crazy:

Code: (squirrel) [Select]
for (local p, i = 0, count = GetPlayers(); i < count; i++) {
  if (!(p = FindPlayer(i))) continue;
 
  p.Health = 100;
}
Title: Re: [TUTORIAL] "while" loop
Post by: Windlord on March 01, 2011, 02:20:52 am
And here's a loop for people who aren't batshit crazy:

Code: (squirrel) [Select]
for (local p, i = 0, count = GetPlayers(); i < count; i++) {
  if (!(p = FindPlayer(i))) continue;
 
  p.Health = 100;
}


Slight problem would be that not all player IDs are populated in sequence if someone in the middle leaves.
A solution is:
Code: (squirrel) [Select]
for (local p, i = 0, c = 0, count = GetPlayers(); c < count; i++) {
  if (!(p = FindPlayer(i))) continue;
 
  p.Health = 100;
  c++;
}
Title: Re: [TUTORIAL] "while" loop
Post by: himselfe on March 01, 2011, 02:57:48 am
Slight problem would be that not all player IDs are populated in sequence if someone in the middle leaves.


Good point! A simpler solution would be to use GetMaxPlayers (http://liberty-unleashed.co.uk/LUWiki/Squirrel/Server/Functions/Server/GetMaxPlayers)() instead of GetPlayers(), such as:

Code: (squirrel) [Select]
for (local p, i = 0, count = GetMaxPlayers(); i < count; i++) {
  if (!(p = FindPlayer(i))) continue;
 
  p.Health = 100;
}

Although this could be inefficient if it's done lots of times on large servers. A better and more efficient alternative would be to keep an active table of players and use foreach to iterate that table:

Code: (squirrel) [Select]
players <- {};

function onPlayerJoin( player )
{
players.rawset(player.Name,player);
}

function onPlayerPart( player, reason )
{
players.rawdelete(player.Name);
}

function HealAllPlayers()
{
foreach(player in players) {
player.Health = 100;
}
}
Title: Re: [TUTORIAL] "while" loop
Post by: Dominik on April 06, 2011, 03:06:10 pm
Nice. I try it...