PDA

View Full Version : Help with a loop. [EASY]


$Marco$
04/03/2015, 01:02 PM
So I made a loop to count throguh all the ONLINE players on the server, but it keeps counting to 500 no matter what (I was the only one on the server).


new i;
i = 0;
loop:
if(i > MAX_PLAYERS) goto loop_stop;
if(something == 1)
{
//do something
i++;
goto loop;
}
else
{
i++;
goto loop;
}
loop_stop:
//do something
return 1;


WHAT'S WRONG WITH THIS CODE?!

Jimmy0wns
04/03/2015, 01:08 PM
Things that are wrong:
- Where the heck is the indentation? It's 2015 people.
- You already explained yourself what you did wrong in your code.
- The answer is in capital letters inside your code.
- Why are there random words there like "do something2;", "loop_stop" ?
- According to your signature, you appear to be a medium scripter, I don't know what medium means in your eyes. But whatever.

$Marco$
04/03/2015, 01:12 PM
Things that are wrong:
- Where the heck is the indentation? It's 2015 people.
- You already explained yourself what you did wrong in your code.
- The answer is in capital letters inside your code.
- Why are there random words there like "do something2;", "loop_stop" ?
- According to your signature, you appear to be a medium scripter, I don't know what medium means in your eyes. But whatever.

Dear Jummy0owns, do something2 is an example for what gonna happen next, I have no intention of showing my code so I made an example code of what happens if the loop breaks and it reaches the MAX_PLAYERS on the server.

How about you show me what the error instead of giving me hints, its not a riddle forums its Scripting Help, so please - if you can't help me or have no intentions of helping me avoid posting, because I'm seeking for answers and not riddles.

Schneider
04/03/2015, 01:15 PM
@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. http://wiki.sa-mp.com/wiki/Control_Structures#do-while

@Xysiaris:
You forgot to add the IsPlayerConnected(i) function.

Golimad
04/03/2015, 01:17 PM
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i)) continue;
count++;
}

Simple as this

$Marco$
04/03/2015, 01:18 PM
@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. http://wiki.sa-mp.com/wiki/Control_Structures#do-while

@Xysiaris:
You forgot to add the IsPlayerConnected(i) function.

I did add it on my real code it looks like this:


new i;
i = 0;
loop:
if(i > MAX_PLAYERS) goto loop_stop;
if(something == 1 && isPlayerConnected(i) == 1)
{
//do something
i++;
goto loop;
}
else
{
i++;
goto loop;
}
loop_stop:
//do something
return 1;


But it still counts to 500 and only then breaks instead of counting to 1 (or the number of players online on the server).

Golimad
04/03/2015, 01:20 PM
Your mistake is : if(i > MAX_PLAYERS)
should be if(i < MAX_PLAYERS) I guess

Use For(new i ... )
You have break; and continue.

$Marco$
04/03/2015, 01:22 PM
Your mistake is : if(i > MAX_PLAYERS)
should be if(i < MAX_PLAYERS) I guess

if(i > MAX_PLAYERS) goto loop_stop = if I is bigger than MAX PLAYERS go to label called loop stop.

if(i < MAX_PLAYERS) goto loop_stop = if I is smaller than MAX PLAYERS go to label called loop stop.

You are incorrect sadly.

I dont need to 'break' it as I send the code to somewhere else and it should STOP counting when it reaches MAX PLAYERS, but it don't.

Golimad
04/03/2015, 01:26 PM
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i)) continue;
count++;
}


Why don't you just use this to do the count and whatever function you wanna do to the players

You are making things complicated for nothing, What you are doing is pointless


new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i)) continue;
count++;
if(something) // code
else(something) // code

}

$Marco$
04/03/2015, 01:28 PM
Why don't you just use this to do the count and whatever function you wanna do to the players

Because in the original code I have done things that require me to use it the way I showed it, I need someone just to tell me why i > MAX_PLAYERS is not working.

Golimad
04/03/2015, 01:31 PM
After samp include:

#undef MAX_PLAYERS
#define MAX_PLAYERS (30) // or how many players you have


Sa-mp defines MAX_PLAYERS to 500 by default.


Edit : You can't stop the loop from counting if you are the only player in the server.
Because when 3 players login : id 0, id 1, id 2. if player id 1 leaves, server won't change ids. id0, id2;
if you break it when IsPlayerConnected = false, it won't reach other players.

Kaliber
04/03/2015, 01:33 PM
Because in the original code I have done things that require me to use it the way I showed it, I need someone just to tell me why i > MAX_PLAYERS is not working.
Dude, first of all...NEVER USE GOTO when you have another option for a loop!!!

This Code...counts to 500 because in both cases you do i++...

and it only ends if i > MAX_PLAYERS...so what do you expect?!

new i,count;
loop:
if(i == MAX_PLAYERS) goto loop_stop;
if(IsPlayerConnected(i))
{
//do something
count++;

}
else
{

}
i++;
goto loop;
loop_stop:
//in count is the online player value
//do something
return 1;

But as Golimad posted, you should use this code snippet.

$Marco$
04/03/2015, 01:50 PM
I have solved this on my own, none of you was right.

Schneider
04/03/2015, 01:54 PM
Right...

Vince
04/03/2015, 03:15 PM
Completely mental. This isn't Assembly, you know. Goto should never be used in a high level language because the problem at hand can always be solved with a for, while or do-while loop.

KingHual
04/03/2015, 03:24 PM
>2015
>using goto

Parallex
04/03/2015, 03:26 PM
You put [EASY] tag in the thread title when you don't know how to fix it?
http://img.pandawhale.com/37020-didnt-read-lol-eLU8.gif

maramizo
04/03/2015, 04:41 PM
Completely mental. This isn't Assembly, you know. Goto should never be used in a high level language because the problem at hand can always be solved with a for, while or do-while loop.

>2015
>using goto

You put [EASY] tag in the thread title when you don't know how to fix it?
http://img.pandawhale.com/37020-didnt-read-lol-eLU8.gif

Oh man this is like the /b/ of SAMP. Gold.

Anyway, @OP, don't use goto, pls, this isn't .net.

$Marco$
04/03/2015, 06:47 PM
I will use what ever I find fitting to script with, its not like that has any effect on you neither on SA-MP community.