Array w/ Strings Loop - 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: Array w/ Strings Loop (
/showthread.php?tid=474314)
Array w/ Strings Loop -
MsgtHill - 07.11.2013
If I have an array, such as:
Код:
new myArray[MAX_PLAYERS][10][50];
, which is supposed to hold 10 strings. How do I loop through it to check for an open slot? I know how to loop through an array that contains number values, but not string values. I have tried using strcmp and strlen, but without success.
Re: Array w/ Strings Loop -
Konstantinos - 07.11.2013
pawn Код:
stock GetFreeSlot( playerid )
{
for( new i; i != sizeof( myArray[ ] ); i++ )
{
if( !myArray[ playerid ][ i ][ 0 ] ) return i;
}
return -1;
}
It returns the free slot or -1 if there is not any. Note that if you're going to use the value returned as an index, you must check if it's not -1 first to prevent runtime error: Index out of bounds.
Re: Array w/ Strings Loop -
MsgtHill - 07.11.2013
Thanks for the response! So if I wanted to add a string to a free slot, I would do something like:
Код:
myArray[playerid][GetFreeSlot(playerid)] = "My string here";
Re: Array w/ Strings Loop -
Konstantinos - 07.11.2013
Yes, but check the returned value first to avoid runtime errors.
pawn Код:
new slot = GetFreeSlot(playerid);
if(slot != -1) myArray[playerid][slot] = "My string here";
So if it returned 1 because the 0 was not null, then the myArray[playerid][1] will be "My string here".
Re: Array w/ Strings Loop -
MsgtHill - 07.11.2013
Nice, thanks for your help!