• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Help with a loop
#1
Star 
So I feel very stupid right now but can somebody help me with this problem.

I'm making a loop which loads the mysql database data for my admin system.

Code:
#define MAX_ADMINS 25

public OnGameModeInit() {
handle = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);

new query[128];
for(new i = 0; i < MAX_ADMINS; i++) {
mysql_format(handle, query, sizeof(query), "SELECT * FROM admini WHERE Slot='%d'", i);
mysql_tquery(handle, query, "OnAdminLoad", "i", i);
}

return 1;
}

forward OnAdminLoad(i);
public OnAdminLoad(i) {
if(cache_num_rows() > 0) {
cache_get_value(0, "Ime", AdminInfo[i+1][aIme], 25);     // THIS IS WHERE THE ERROR OCCURS
cache_get_value_int(0, "MinNeaktivnost", AdminInfo[i+1][aMinNeaktivnost]);
cache_get_value_int(0, "Neaktivnost", AdminInfo[i+1][aNeaktivnost]);
cache_get_value_int(0, "Zauzeto", AdminInfo[i+1][aZauzeto]);
cache_get_value_int(0, "Level", AdminInfo[i+1][aLevel]);
cache_get_value(0, "ImeLevel", AdminInfo[i+1][aImeLevel], 32);
} else {
print("Ucitavanje Admin Databaze nije uspelo.");
}
return true;
}

The error I get is:

[debug] Run time error 4: "Array index out of bounds"
[2025-01-05T16:46:11+0100] [Info] [debug]  Attempted to read/write array element at index 25 in array of size 25
[2025-01-05T16:46:11+0100] [Info] [debug] AMX backtrace:
[2025-01-05T16:46:11+0100] [Info] [debug] #0 000243c8 in public OnAdminLoad (i=24) at C:\Users\neman\Desktop\openmp server\gamemodes\skripta.pwn:120

Even though the loop loops from 0 to 24 and then I load it "i+1" because in my database, the Admin Slots are from 1 to 25?

Can somebody explain where the problem is?
  Reply
#2
I couldn't understand very well. Why are you loading 25 admins at once?
without them being connected
  Reply
#3
(2025-01-06, 12:11 AM)samuelmatheus0502 Wrote: I couldn't understand very well. Why are you loading 25 admins at once?
without them being connected

It just loads every admin data and puts those values into variables when the server starts.
  Reply
#4
I believe the problem is with MAX_ADMINS, as the maximum is 25, but you started the loop with 0 and from 0 to 25 gives 26 spaces.


change this:

"for(new i = 0; i < MAX_ADMINS; i++)"

that's why:

for(new i = 1; i < MAX_ADMINS; i++)

and also remove the "+1" from variable i in the OnAdminLoad callback
  Reply


Forum Jump: