Run time error 4: "Array index out of bounds"
#1

Hello,

I am making my own inventory system but there is a problem when I try to load more than 4 objects.
So I have 2 tables in mysql.
one is called objects and contains all the information about the object (name,displayid,maxuses...)
and the other one is called inventories with all the objects owned by players and its properties (fingerprints, uses)

This is my code:

PHP код:
public OnGameModeInit()
{
    new 
queryy[512];
    
mysql_format(mysqlqueryysizeof(queryy), "SELECT ObjectID FROM `objects` ORDER BY ObjectID DESC LIMIT 1");
    
mysql_tquery(mysqlqueryy"countobjects");
    return 
1;
}
forward countobjects();
public 
countobjects()
{
    
objamount cache_get_field_content_int(0"ObjectID") + 1;
    for(new 
iobjamounti++)
    {
        
GetObjectInfo(i);
    }

I load everything into an array with this code:
PHP код:
stock GetObjectInfo(id)
{
    new 
queryy[100];
    
mysql_format(mysqlqueryysizeof(queryy), "SELECT * FROM `objects` WHERE `ObjectID` = '%i'"id);
    new 
Cache:result mysql_query(mysqlqueryy);
    
    new 
name[126];
    
cache_get_field_content(0"Name"name);
    
InventoryInfo[Objid][Name] = name;
    
    
InventoryInfo[Objid][MaxUses] = cache_get_field_content_int(0"MaxUses"mysql);
    
    
InventoryInfo[Objid][DisplayID] = cache_get_field_content_int(0"DisplayID"mysql);
    
    
InventoryInfo[Objid][Action] = cache_get_field_content_int(0"ActionID"mysql);
    
    
InventoryInfo[Objid][Type] = cache_get_field_content_int(0"Type"mysql);
    
    
InventoryInfo[Objid][BodyX] = cache_get_field_content_float(0"BodyX");
    
    
InventoryInfo[Objid][BodyY] = cache_get_field_content_float(0"BodyY");
    
    
InventoryInfo[Objid][BodyZ] = cache_get_field_content_float(0"BodyZ");
    
    
printf("Object loaded: %s | ObjectID: %i | MaxUses: %i | Action: %i | Type: %i"InventoryInfo[Objid][Name], idInventoryInfo[Objid][MaxUses],    InventoryInfo[Objid][Action], InventoryInfo[Objid][Type]);
    
cache_delete(result);
    
    return 
name;

It worked until I added a fifth object 'Empty Bottle'
I get this error when starting the server:

PHP код:
Object loadedWater ObjectIDMaxUsesAction21 Type14
Object loaded
Sprunk ObjectIDMaxUsesAction21 Type14
Object loaded
M&Ms ObjectIDMaxUsesActionType5
Object loaded
Chips ObjectIDMaxUsesActionType5
//there should be a 5th object here called 'Empty bottle'
[debugRun time error 4"Array index out of bounds"
[debug]  Accessing element at index 4 past array upper bound 3
[debugAMX backtrace:
[
debug#0 00013548 in GetObjectInfo (id=4) at C:\...\prp-gamemode.pwn:1011
[debug#1 0000d970 in public countobjects () at C:\...\prp-gamemode.pwn:158 
Reply
#2

You're doing two things wrong:
Database id's can and will be non consequential. It's common to have 5 objects with id's 1235, 1267, 1289, 1319. That's why you never should use database id as an array index. Second thing: you're calling 6 queries for 5 objects. And 5 of those are unthreaded. That's terrible.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)