SA-MP Forums Archive
Array index out of bounds - 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 index out of bounds (/showthread.php?tid=665425)



Array index out of bounds - SaiyanZ - 03.04.2019

commands were working find until i complied with ysi v4 now they just send SERVER:Unknown command msg and creates the object,
the console:
Code:
[18:18:49] [debug] Run time error 4: "Array index out of bounds"
[18:18:49] [debug]  Attempted to read/write array element at index 1032 in array of size 996
[18:18:49] [debug] AMX backtrace:
[18:18:49] [debug] #0 0001de20 in public cmd_ocreate (2, 347436) in AdminSys.amx
[18:18:49] [debug] #1 native CallLocalFunction () in sampsvr-port_177
[18:18:49] [debug] #2 000006d8 in public OnPlayerCommandText (2, 347380) in AdminSys.amx
Code:
CMD:ocreate(playerid, params[])
{
    if(PlayerInfo[playerid][Level]<3) return SendClientMessage(playerid, COLOR_RED, "SERVER: You're not allowed to use this command");
	new objectid;
    if(sscanf(params,"d", objectid)) return SendClientMessage(playerid,COLOR_RED,"Usage:/ocreate [object id]");
    if(objectid < 321 || objectid > 19999) return SendClientMessage(playerid,COLOR_RED,"SERVER: Object ID not found.");
    {
        objects++;
        objectsid[objects]=objectid;
        new Float:X, Float:Y, Float:Z, str[128];
        GetPlayerPos(playerid, X, Y, Z);
        SpawnedObject[objects] = CreateDynamicObject(objectid, X, Y, Z+1, 0, 0, 0);
		ObjectSpawned[SpawnedObject[objects]] = 1;
		format(str, 128, "SERVER: Object ID: %d - Created at your location - Index: %d", objectid, objects);
		SendClientMessage(playerid, COLOR_RED, str);
    }
    return 1;
}



Re: Array index out of bounds - raydx - 03.04.2019

Has to be related with ObjectSpawned[SpawnedObject[objects]] or SpawnedObject[objects]. Check array sizes.


Re: Array index out of bounds - Viggo - 03.04.2019

Write ObjectSpawned[SpawnedObject][objects] instead.


Re: Array index out of bounds - v1k1nG - 03.04.2019

PHP Code:
[18:18:49] [debug]  Attempted to read/write array element at index 1032 in array of size 996 
You tried to read/write at position 1032 in an array sized 996.


Re: Array index out of bounds - SaiyanZ - 04.04.2019

Code:
new SpawnedObject[999];
new ObjectSpawned[999];
still cant find it it was working fine until i got ysi4


Re: Array index out of bounds - raydx - 04.04.2019

Just increase 999 to higher value. You need at least 1032. It's not even ysi related.


Re: Array index out of bounds - Pottus - 06.04.2019

Quote:
Originally Posted by raydx
View Post
Just increase 999 to higher value. You need at least 1032. It's not even ysi related.
That is not how you fix this type of error.


Re: Array index out of bounds - Markski - 06.04.2019

Does the script compile fine w/o YSIv4?

Sounds to me like you should try and place some checkpoint prints/clientmessages with the variables in that command, see why you seem to be attempting to read from that inexistent array element.


Re: Array index out of bounds - SaiyanZ - 06.04.2019

I found the line if i remove the line the command works
any ideas what should i do to replace it with something else?
PHP Code:
CMD:ocreate(playeridparams[])
{
    if(
PlayerInfo[playerid][Level]<3) return SendClientMessage(playeridCOLOR_RED"SERVER: You're not allowed to use this command");
    new 
objectid;
    if(
sscanf(params,"d"objectid)) return SendClientMessage(playerid,COLOR_RED,"Usage:/ocreate [object id]");
    if(
objectid 321 || objectid 19999) return SendClientMessage(playerid,COLOR_RED,"SERVER: Object ID not found.");
    {
        
objects++;
        
objectsid[objects]=objectid;
        new 
Float:XFloat:YFloat:Zstr[128];
        
GetPlayerPos(playeridXYZ);
        
SpawnedObject[objects] = CreateDynamicObject(objectidXYZ+1000);
    
ObjectSpawned[SpawnedObject[objects]] = 1//  <-----  this  line
    
format(str128"SERVER: Object ID: %d - Created at your location - Index: %d"objectidobjects);
    
SendClientMessage(playeridCOLOR_REDstr);
    }
    return 
1;




Re: Array index out of bounds - Markski - 06.04.2019

Quote:
Originally Posted by SaiyanZ
View Post
I found the line if i remove the line the command works
any ideas what should i do to replace it with something else?
PHP Code:
CMD:ocreate(playeridparams[])
{
    if(
PlayerInfo[playerid][Level]<3) return SendClientMessage(playeridCOLOR_RED"SERVER: You're not allowed to use this command");
    new 
objectid;
    if(
sscanf(params,"d"objectid)) return SendClientMessage(playerid,COLOR_RED,"Usage:/ocreate [object id]");
    if(
objectid 321 || objectid 19999) return SendClientMessage(playerid,COLOR_RED,"SERVER: Object ID not found.");
    {
        
objects++;
        
objectsid[objects]=objectid;
        new 
Float:XFloat:YFloat:Zstr[128];
        
GetPlayerPos(playeridXYZ);
        
SpawnedObject[objects] = CreateDynamicObject(objectidXYZ+1000);
    
ObjectSpawned[SpawnedObject[objects]] = 1//  <-----  this  line
    
format(str128"SERVER: Object ID: %d - Created at your location - Index: %d"objectidobjects);
    
SendClientMessage(playeridCOLOR_REDstr);
    }
    return 
1;

Why not try printing the contents of SpawnedObject[objects] into chat or something so you can see what value it has, and then from there figure out how it reaches said value?

The way I understand it, it seems you're giving that variable a value larger than 998, which is why you break out of the array's bounds.


Re: Array index out of bounds - Y_Less - 06.04.2019

It will be these two together:

Код:
SpawnedObject[objects] = CreateDynamicObject(objectid, X, Y, Z+1, 0, 0, 0);


    ObjectSpawned[SpawnedObject[objects]] = 1; //  <-----  this  line
CreateDynamicObject returns something over 998, leading to the OOB Markski mentioned.