Liberty Unleashed
Scripting => Script Tutorials => Topic started 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:
while ( condition )
{
code to execute
}
And below are some samples of use:
- Setting all players health to 100:
local x = 0;
while ( x < GetPlayers() )
{
local plr = FindPlayer( x );
if ( plr )
{
plr.Health = 100;
}
x++;
}
- Teleporting all players somewhere:
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:
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.
-
I though vehicle IDs start with 1
-
I though vehicle IDs start with 1
Thanks, fixed :).
-
I though vehicle IDs start with 1
Thanks, fixed :).
Last I checked they start with 0, not 1.
-
I though vehicle IDs start with 1
Thanks, fixed :).
Last I checked they start with 0, not 1.
Lol, so I changed it again.
-
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 ;)
-
And here's a loop for people who aren't batshit crazy:
for (local p, i = 0, count = GetPlayers(); i < count; i++) {
if (!(p = FindPlayer(i))) continue;
p.Health = 100;
}
-
And here's a loop for people who aren't batshit crazy:
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:
for (local p, i = 0, c = 0, count = GetPlayers(); c < count; i++) {
if (!(p = FindPlayer(i))) continue;
p.Health = 100;
c++;
}
-
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:
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:
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;
}
}
-
Nice. I try it...