/members out of bounds
#1

My command /members works only on few factions, IDK why one some doesn't...

Код:
CMD:members( playerid ){
	if( playerVariables[ playerid ][ pGroupRank ] < 6 ) return SendClientMessage( playerid, -1, "Faction: You can't acces this command");
	new szString[ 1500 ], szQuery[ 512 ], g_ID = playerVariables[ playerid ][ pGroup ], Cache: r, szName[ MAX_PLAYER_NAME ], g_Rank, g_FW, g_Days;
	format( szString, sizeof( szString ), "{FFB300}%s {FFFFFF}members: \n", groupVariables[ g_ID ][ gGroupName ] );
	format( szQuery, sizeof( szQuery ), "SELECT * FROM `playeraccounts` WHERE `playerGroup` = %d ORDER BY `playerGroupRank` DESC", g_ID );
	r = mysql_query( handle, szQuery );
	for( new i = 0; i < cache_get_row_count( handle ); i++ ){
	    cache_get_field_content( i, "playerName", szName, handle, MAX_PLAYER_NAME );
	    format( Selected[ i + 1 ][ snume ], MAX_PLAYER_NAME, szName );
		g_Rank = cache_get_field_content_int( i, "playerGroupRank", handle );
		g_FW = cache_get_field_content_int( i, "playerFWarns", handle );
		g_Days = cache_get_field_content_int( i, "playerDays", handle );
		format( szString, sizeof( szString ), "%s %s - Rank: %d - FW: %d/3 - %s - %d\n", szString, szName, g_Rank, g_FW, ( GetPlayerID( szName ) == INVALID_PLAYER_ID ) ? ( "offline" ) : ( "online" ), g_Days );
	}
	cache_delete( r );
	ShowPlayerDialog( playerid, DIALOG_MEMBERS, DIALOG_STYLE_LIST, "Members", szString, "Select", "Cancel" );
	return 1;
	
}
Код:
[09:38:04] [debug] Run time error 4: "Array index out of bounds"
[09:38:04] [debug]  Accessing element at index 10 past array upper bound 9
[09:38:04] [debug] AMX backtrace:
[09:38:04] [debug] #0 00179d54 in public cmd_members (playerid=0, ... <1 argument>) at E:\x\x\gamemodes\x.pwn:26238
[09:38:04] [debug] #1 native CallLocalFunction () from samp03svr
[09:38:04] [debug] #2 00000b1c in public OnPlayerCommandText (playerid=0, cmdtext[]=@0035f32c "/members") at E:\x\x\pawno\include\zcmd.inc:102
26238 is format( Selected[ i + 1 ][ snume ], MAX_PLAYER_NAME, szName );
Reply
#2

EDIT: Confused this with something else.

The second issue is, you're supposedly going out of bounds.
Let's say that 'Selected' only has an array size of 10 elements.
eg.
pawn Код:
new Selected[10][enumhere];
This means you can use the elements 0 - 9, but never more.

So if we say that 'cache_get_row_count(handle)' is equal to 9, you have a loop going from 0 up to 9. But because you add '1' to the value of i, this means that the loop will go from 1 - 10. But 10 is outside the range of the elements in 'Select'.

So you either have 2 options. Remove the '+ 1' from the format line, or change the element size of 'Select' from 10 to 11. Element start at 0, not 1.

References:
https://sampwiki.blast.hk/wiki/Format
Reply
#3

^I start at 1 because on 0 is the name of the faction.
Reply
#4

Quote:
Originally Posted by Threshold
Посмотреть сообщение
Well first of all, you're missing a parameter.
No, he can do this also like this:

PHP код:
formatSelected ][ snume ], MAX_PLAYER_NAMEszName ); 
It always the same.
Reply
#5

True, I am getting confused with one of the custom functions I made for printing variables. But like I said, if you plan on starting at '1', then you either need to reduce the maximum value of the loop, or increase the elements of the array by 1.
Reply
#6

Well, like the MySQL plugin says, unthreaded queries are always unnecessary in the latest edition.

Also, stop using sz as a prefix, cause my bets are you don't actually know what it means.

Should probably follow a template similar to this:

pawn Код:
CMD:members(playerid, params[])
{
    if(playerVariables[playerid][pGroupRank] < 6) return SendClientMessage(playerid, -1, "Faction: You can't access this command.");
    new string[512], query[256];
    mysql_format(handle, query, sizeof(query), "SELECT * FROM `playeraccounts` WHERE playerGroup=%d ORDER BY `playerGroupRank` DESC", playerVariables[playerid][pGroup]);
    mysql_tquery(handle, query, "LoadFactionMembers", "i", playerid);
    return 1;
}

forward LoadFactionMembers(playerid);
public LoadFactionMembers(playerid)
{
    new rows, fields,
        tmpName[25], tmpRank, tmpFW, tmpDays;
   
    cache_get_data(rows, fields);
       
    format(string, sizeof(string), "%s members:\n", group_name_variable_here);
   
    if(rows)
    {
    }
}
Reply


Forum Jump:


Users browsing this thread: