Author Topic: [TUTORIAL] "while" loop  (Read 10848 times)

Stoku

  • lck.gudio.eu
  • Full Member
  • ***
  • Posts: 276
  • Karma: +26/-2
  • Liberty City Killers
    • View Profile
    • Liberty City Killers (GTA3/VC Clan)
[TUTORIAL] "while" loop
« 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.
« Last Edit: February 26, 2011, 03:37:15 pm by [LCK]Stoku »

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: [TUTORIAL] "while" loop
« Reply #1 on: February 26, 2011, 11:16:35 am »
I though vehicle IDs start with 1

Stoku

  • lck.gudio.eu
  • Full Member
  • ***
  • Posts: 276
  • Karma: +26/-2
  • Liberty City Killers
    • View Profile
    • Liberty City Killers (GTA3/VC Clan)
Re: [TUTORIAL] "while" loop
« Reply #2 on: February 26, 2011, 12:05:19 pm »
I though vehicle IDs start with 1
Thanks, fixed :).

Force

  • Developer
  • Full Member
  • *****
  • Posts: 204
  • Karma: +6/-2
    • View Profile
Re: [TUTORIAL] "while" loop
« Reply #3 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.
Quote
[Tue - 20:09:35] <&VRocker> CRAP!
[Tue - 20:09:43] <&VRocker> i think i just followed through...
Quote
[Sat - 22:11:56] <~Smapy> [R3V]breSt12 killed [R3V]Jack_Bauer. (Splat)

Stoku

  • lck.gudio.eu
  • Full Member
  • ***
  • Posts: 276
  • Karma: +26/-2
  • Liberty City Killers
    • View Profile
    • Liberty City Killers (GTA3/VC Clan)
Re: [TUTORIAL] "while" loop
« Reply #4 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.

Orpheus

  • Newbie
  • *
  • Posts: 49
  • Karma: +1/-0
  • Music is a way of life.
    • View Profile
    • Orphtown UK
Re: [TUTORIAL] "while" loop
« Reply #5 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 ;)

Windows: A 64-bit/32-bit extension to a 16-bit GUI on an 8-bit OS written for a 4-bit architecture by a 2-bit company who can not stand 1-bit of competition.

himselfe

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: [TUTORIAL] "while" loop
« Reply #6 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;
}

Windlord

  • Guest
Re: [TUTORIAL] "while" loop
« Reply #7 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++;
}

himselfe

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: [TUTORIAL] "while" loop
« Reply #8 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() 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;
}
}

Dominik

  • Newbie
  • *
  • Posts: 26
  • Karma: +1/-0
  • I like GTA3
    • View Profile
Re: [TUTORIAL] "while" loop
« Reply #9 on: April 06, 2011, 03:06:10 pm »
Nice. I try it...
Dominik

 

© Liberty Unleashed Team.