SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 21/02/2020, 02:12 PM   #1
masterart
Little Clucker
 
Join Date: Feb 2019
Posts: 30
Reputation: 0
Default array index out of bounds

PHP Code:
#define MAX_FACTIONS (11)

enum FACTIONDATA
{
    
id,
    
fname[25]
}
new 
fInfo[MAX_FACTIONS][FACTIONDATA];

CMD:createfaction(playeridparams[])
{
    new 
query[250];
    if(!
IsPlayerAdmin(playerid)) return SendClientMessage(playeridCOLOR_RED"ERROR: You cannot use this command");
    if(
sscanf(params"s"fInfo[MAX_FACTIONS][fname])) return SendClientMessage(playeridCOLOR_RED"USAGE: /createfaction [factionname]");
    
mysql_format(databasequerysizeof(query), "INSERT INTO `FACTIONS` (`FNAME`) VALUES ('%e')"fInfo[MAX_FACTIONS][fname]);
    return 
1;

why am i getting these errors?

Quote:
C:\Users\\Desktop\wcl\gamemodes\wcl.pwn(71) : error 032: array index out of bounds (variable "fInfo")
C:\Users\\Desktop\wcl\gamemodes\wcl.pwn(72) : error 032: array index out of bounds (variable "fInfo")
masterart is offline   Reply With Quote
Old 21/02/2020, 02:18 PM   #2
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,528
Reputation: 1642
Default Re: array index out of bounds

It literally means what it says: Index out of bounds.
fInfo's size is 11 (MAX_FACTIONS). You are trying to access index 11 (in the sscanf() part and in the mysql_format() part).
If you declare an array with size 11, you have access to index 0 up to 10. 11 contains the null terminator.

Aside from that, this is not the way to add new factions anyway. This script doesn't know what ID it should use.

Another note, when you #define something, it is translated to it's value by the pre-processor (that happens before it actually compiles the script).
So this code:
pawn Code:
#define ARRAY_SIZE 10
new array[ARRAY_SIZE];
Gets translated to this before it recompiles:
pawn Code:
new array[10];

So,
pawn Code:
//This (from your script)
if(sscanf(params, "s", fInfo[MAX_FACTIONS][fname]))
//Gets translated to:
if(sscanf(params, "s", fInfo[11][fname]))
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is offline   Reply With Quote
Old 21/02/2020, 02:32 PM   #3
masterart
Little Clucker
 
Join Date: Feb 2019
Posts: 30
Reputation: 0
Default Re: array index out of bounds

ah alright, so how do i let it know what id it should use?
masterart is offline   Reply With Quote
Old 21/02/2020, 02:39 PM   #4
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,528
Reputation: 1642
Default Re: array index out of bounds

Well, first of all, you're just formatting the string using mysql_format(). You're not actually sending the query to the database. Use mysql_tquery().
In the callback you're providing in mysql_tquery() you can use cache_insert_id() to tell what the new ID is.

Also, since you don't want to go over the limit of 11 factions, you can either loop through fInfo to see which indexes are already used (factions exist). If there are no free slots, block the function. Or you can use cache_insert_id() to tell if it's reached the limit and block the function there.
Keep in mind that array indexes start at 0, and IDs in a MySQL database start at 1.
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is offline   Reply With Quote
Old 21/02/2020, 05:02 PM   #5
masterart
Little Clucker
 
Join Date: Feb 2019
Posts: 30
Reputation: 0
Default Re: array index out of bounds

how do i loop through fInfo and check the indexes if i may ask? i can't find a clear tutorial
masterart is offline   Reply With Quote
Old 21/02/2020, 06:12 PM   #6
Kwarde
High-roller
 
Kwarde's Avatar
 
Join Date: Nov 2009
Location: The Netherlands
Posts: 2,528
Reputation: 1642
Default Re: array index out of bounds

A simple for() loop would do.
https://forum.sa-mp.com/showthread.php?p=4122459
You might want to read about scripting basics.
__________________
Do not click on this URL -It is secret information
RIP Slayer 1981-2019 \m/
Kwarde is offline   Reply With Quote
Old 21/02/2020, 06:21 PM   #7
Vizi10
Huge Clucker
 
Join Date: May 2017
Location: Iran
Posts: 331
Reputation: 8
Default Re: array index out of bounds

Fixed code:
pawn Code:
#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define MAX_FACTIONS (11)

enum FACTIONDATA
{
    fexist,
    id,
    fname[24]
}
new fInfo[MAX_FACTIONS][FACTIONDATA];

CMD:createfaction(playerid, params[])
{
    if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, COLOR_RED, "ERROR: You cannot use this command");
    new name[24];
    if(sscanf(params, "s[24]", name)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /createfaction [factionname]");
    new fID = GetFreeFactionID(), query[128];
    fInfo[fID][fexist] = 1;
    strcpy(fInfo[fID][fname], name, 24);
    mysql_format(database, query, sizeof(query), "INSERT INTO `FACTIONS` (`FNAME`) VALUES ('%e')", fInfo[fID][fname]);
    return 1;
}

GetFreeFactionID()
{
    for(new i; i<MAX_FACTIONS; i++)
    {
        if(!fInfo[i][fexist]) return i;
    }
    return -1;
}
Vizi10 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array index out of bounds Banditul18 Scripting Help 5 05/03/2017 12:25 PM
Array index out of bounds. iLearner Scripting Help 7 14/12/2016 06:44 PM
Array index out of bounds malackovgfx Scripting Help 3 12/06/2015 07:24 PM
Array index out of bounds thimo Scripting Help 1 11/08/2013 11:29 AM


All times are GMT. The time now is 01:36 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.