What's wrong with my code (detection of near job id's) - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: What's wrong with my code (detection of near job id's) (
/showthread.php?tid=569583)
What's wrong with my code (detection of near job id's) -
WhoIsYourDaddy - 31.03.2015
Код:
YCMD:yakin(playerid, params[], help)
{
new sMeslek[128+1];
for(new i = 0; i < MAKSIMUM_MESLEK; i++) // Meslek ID - Job ID
{
if(IsPlayerInRangeOfPoint(playerid, 5.0, Meslek[i][meslekX], Meslek[i][meslekY], Meslek[i][meslekZ]))
{
format(sMeslek, sizeof(sMeslek), "Nearest job's SQLID: %d", Meslek[i][meslekID]);
SendClientMessage(playerid, -1, sMeslek);
break;
} else return Hata(playerid, "There isn't a job near you.");
}
return 1;
}
It works for Job SQLID 1 but not for 2. Why is that? Is it because of the 'break' ?
Re: What's wrong with my code (detection of near job id's) -
Mauzen - 31.03.2015
Its the "else"
pawn Код:
if(IsPlayerInRangeOfPoint(playerid, 5.0, Meslek[i][meslekX], Meslek[i][meslekY], Meslek[i][meslekZ]))
{
//...
} else return Hata(playerid, "There isn't a job near you.");
As soon as the first range check fails, it returns and the function ends.
Put the message and the return after the for-loop, and instead return when a job was found, so the "There isn't a job near you." message is only shown when the whole loop failed.
pawn Код:
YCMD:yakin(playerid, params[], help)
{
new sMeslek[128+1];
for(new i = 0; i < MAKSIMUM_MESLEK; i++) // Meslek ID - Job ID
{
if(IsPlayerInRangeOfPoint(playerid, 5.0, Meslek[i][meslekX], Meslek[i][meslekY], Meslek[i][meslekZ]))
{
format(sMeslek, sizeof(sMeslek), "Nearest job's SQLID: %d", Meslek[i][meslekID]);
SendClientMessage(playerid, -1, sMeslek);
return 1; // Stop the loop, no more need to check other jobs
}
}
// Loop finished, no job found
Hata(playerid, "There isn't a job near you.");
return 1;
}
Re: What's wrong with my code (detection of near job id's) -
WhoIsYourDaddy - 31.03.2015
Quote:
Originally Posted by Mauzen
Its the "else"
pawn Код:
if(IsPlayerInRangeOfPoint(playerid, 5.0, Meslek[i][meslekX], Meslek[i][meslekY], Meslek[i][meslekZ])) { //... } else return Hata(playerid, "There isn't a job near you.");
As soon as the first range check fails, it returns and the function ends.
Put the message and the return after the for-loop, and instead return when a job was found, so the "There isn't a job near you." message is only shown when the whole loop failed.
pawn Код:
YCMD:yakin(playerid, params[], help) { new sMeslek[128+1]; for(new i = 0; i < MAKSIMUM_MESLEK; i++) // Meslek ID - Job ID { if(IsPlayerInRangeOfPoint(playerid, 5.0, Meslek[i][meslekX], Meslek[i][meslekY], Meslek[i][meslekZ])) { format(sMeslek, sizeof(sMeslek), "Nearest job's SQLID: %d", Meslek[i][meslekID]); SendClientMessage(playerid, -1, sMeslek); return 1; // Stop the loop, no more need to check other jobs } } // Loop finished, no job found Hata(playerid, "There isn't a job near you."); return 1; }
|
Thanks for your answer. I'll try it tomorrow.