Accessing element at negative index -400
#1

This is another problem that I been trying to solve. Crashdetect plugin spams these so much sometimes

Код:
[debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:46] [debug] Run time error 4: "Array index out of bounds"
[22:20:46] [debug]  Accessing element at negative index -400
[22:20:46] [debug] AMX backtrace:
[22:20:46] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:46] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:47] [debug] Run time error 4: "Array index out of bounds"
[22:20:47] [debug]  Accessing element at negative index -400
[22:20:47] [debug] AMX backtrace:
[22:20:47] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:47] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:48] [debug] Run time error 4: "Array index out of bounds"
[22:20:48] [debug]  Accessing element at negative index -400
[22:20:48] [debug] AMX backtrace:
[22:20:48] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:48] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:49] [debug] Run time error 4: "Array index out of bounds"
[22:20:49] [debug]  Accessing element at negative index -400
[22:20:49] [debug] AMX backtrace:
[22:20:49] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:49] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:50] [debug] Run time error 4: "Array index out of bounds"
[22:20:50] [debug]  Accessing element at negative index -400
[22:20:50] [debug] AMX backtrace:
[22:20:50] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:50] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:51] [debug] Run time error 4: "Array index out of bounds"
[22:20:51] [debug]  Accessing element at negative index -400
[22:20:51] [debug] AMX backtrace:
[22:20:51] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHelvetti.amx
[22:20:51] [debug] #1 00228eb0 in public automon () from NytLoppuHelvetti.amx
[22:20:52] [debug] Run time error 4: "Array index out of bounds"
[22:20:52] [debug]  Accessing element at negative index -400
[22:20:52] [debug] AMX backtrace:
[22:20:52] [debug] #0 002da010 in ?? (0x00000000) from NytLoppuHe
My script is this kind of
pawn Код:
public automon(playerid)
{
new vehicleid;
    vehicleid = GetPlayerVehicleID(playerid);
    new model = GetVehicleModel(vehicleid);
format(string,sizeof(string),"%s",GetVehicleName(model));
SendClientMessage(playerid,COLOR_RED,string);
return 1;
}
pawn Код:
GetVehicleName(vehicleid)
{
    new answer[256];

    format(answer,sizeof(answer),"%s",VehicleNames[vehicleid-400]);
    return answer;
}
pawn Код:
new VehicleNames[212][] = {
{"Landstalker"},{"Bravura"},{"Buffalo"},{"Linerunner"},{"Perrenial"},{"Sentinel"},{"Dumper"},
{"Firetruck"},{"Trashmaster"},{"Stretch"},{"Manana"},{"Infernus"},{"Voodoo"},{"Pony"},{"Mule"},
{"Cheetah"},{"Ambulance"},{"Leviathan"},{"Moonbeam"},{"Esperanto"},{"Taxi"},{"Washington"},
{"Bobcat"},{"Mr Whoopee"},{"BF Injection"},{"Hunter"},{"Premier"},{"Enforcer"},{"Securicar"},
{"Banshee"},{"Predator"},{"Bus"},{"Rhino"},{"Barracks"},{"Hotknife"},{"Trailer 1"},{"Previon"},
{"Coach"},{"Cabbie"},{"Stallion"},{"Rumpo"},{"RC Bandit"},{"Romero"},{"Packer"},{"Monster"},
{"Admiral"},{"Squalo"},{"Seasparrow"},{"Pizzaboy"},{"Tram"},{"Trailer 2"},{"Turismo"},
{"Speeder"},{"Reefer"},{"Tropic"},{"Flatbed"},{"Yankee"},{"Caddy"},{"Solair"},{"Berkley's RC Van"},
{"Skimmer"},{"PCJ-600"},{"Faggio"},{"Freeway"},{"RC Baron"},{"RC Raider"},{"Glendale"},{"Oceanic"},
{"Sanchez"},{"Sparrow"},{"Patriot"},{"Quad"},{"Coastguard"},{"Dinghy"},{"Hermes"},{"Sabre"},
{"Rustler"},{"ZR-350"},{"Walton"},{"Regina"},{"Comet"},{"BMX"},{"Burrito"},{"Camper"},{"Marquis"},
{"Baggage"},{"Dozer"},{"Maverick"},{"News Chopper"},{"Rancher"},{"FBI Rancher"},{"Virgo"},{"Greenwood"},
{"Jetmax"},{"Hotring"},{"Sandking"},{"Blista Compact"},{"Police Maverick"},{"Boxville"},{"Benson"},
{"Mesa"},{"RC Goblin"},{"Hotring Racer A"},{"Hotring Racer B"},{"Bloodring Banger"},{"Rancher"},
{"Super GT"},{"Elegant"},{"Journey"},{"Bike"},{"Mountain Bike"},{"Beagle"},{"Cropdust"},{"Stunt"},
{"Tanker"}, {"Roadtrain"},{"Nebula"},{"Majestic"},{"Buccaneer"},{"Shamal"},{"Hydra"},{"FCR-900"},
{"NRG-500"},{"HPV1000"},{"Cement Truck"},{"Tow Truck"},{"Fortune"},{"Cadrona"},{"FBI Truck"},
{"Willard"},{"Forklift"},{"Tractor"},{"Combine"},{"Feltzer"},{"Remington"},{"Slamvan"},
{"Blade"},{"Freight"},{"Streak"},{"Vortex"},{"Vincent"},{"Bullet"},{"Clover"},{"Sadler"},
{"Firetruck LA"},{"Hustler"},{"Intruder"},{"Primo"},{"Cargobob"},{"Tampa"},{"Sunrise"},{"Merit"},
{"Utility"},{"Nevada"},{"Yosemite"},{"Windsor"},{"Monster A"},{"Monster B"},{"Uranus"},{"Jester"},
{"Sultan"},{"Stratum"},{"Elegy"},{"Raindance"},{"RC Tiger"},{"Flash"},{"Tahoma"},{"Savanna"},
{"Bandito"},{"Freight Flat"},{"Streak Carriage"},{"Kart"},{"Mower"},{"Duneride"},{"Sweeper"},
{"Broadway"},{"Tornado"},{"AT-400"},{"DFT-30"},{"Huntley"},{"Stafford"},{"BF-400"},{"Newsvan"},
{"Tug"},{"Trailer 3"},{"Emperor"},{"Wayfarer"},{"Euros"},{"Hotdog"},{"Club"},{"Freight Carriage"},
{"Trailer 3"},{"Andromada"},{"Dodo"},{"RC Cam"},{"Launch"},{"Police Car (LSPD)"},{"Police Car (SFPD)"},
{"Police Car (LVPD)"},{"Police Ranger"},{"Picador"},{"S.W.A.T. Van"},{"Alpha"},{"Phoenix"},{"Glendale"},
{"Sadler"},{"Luggage Trailer A"},{"Luggage Trailer B"},{"Stair Trailer"},{"Boxville"},{"Farm Plow"},
{"Utility Trailer"}
};
Thanks so much if you help me with this issue too.
Reply
#2

Wrong section.

Anyhow, take a look at https://sampwiki.blast.hk/wiki/GetPlayerVehicleID.
Specifically :
Quote:

This function gets the ID of the vehicle the player is currently in. Note: NOT the model id of the vehicle. See GetVehicleModel for that.

Reply
#3

Quote:
Originally Posted by Infinity
Посмотреть сообщение
Wrong section.

Anyhow, take a look at https://sampwiki.blast.hk/wiki/GetPlayerVehicleID.
Specifically :
Why wrong section? everyone else are doing trouble threads too to here and I made first one too and got help. And btw Take a look at my script.
Reply
#4

That's because your "automon" timer is probably being called whilst the player is not inside a vehicle, thus returning "-400" since "GetVehicleModel" equals to 0 when an invalid vehicle ID is passed.

pawn Код:
GetVehicleName(vehicleid) {

    // No need for 256 cells.
    new
        name[32];

    if (!(400 <= vehicleid <= 611))
        return name;

    vehicleid -= 400;

    strcat(name, VehicleNames[vehicleid]);

    return name;
}
Reply
#5

^ No need for this function too, it's 100% redundant code


Код:
public automon(playerid)
{
    if (IsPlayerInAnyVehicle(playerid) == 1)
    {
        new vehicleid = GetPlayerVehicleID(playerid);
        new model = GetVehicleModel(vehicleid);
        format(string,sizeof(string),"%s", VehicleNames[model-400]);
        SendClientMessage(playerid,COLOR_RED,string);
    }
    return 1;
}
or even better:

Код:
public automon(playerid)
{
    if (IsPlayerInAnyVehicle(playerid) == 1)
    {
        format(string,sizeof(string),"%s", VehicleNames[GetVehicleModel(GetPlayerVehicleID(playerid))-400]);
        SendClientMessage(playerid,COLOR_RED,string);
    }
    return 1;
}
Reply
#6

Quote:
Originally Posted by liinor
Посмотреть сообщение
Why wrong section? everyone else are doing trouble threads too to here and I made first one too and got help.
"Everyone else is jumping in a pit of lava, so should I". Everyone else breaking the rules does not justify your actions.

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
pawn Код:
GetVehicleName(vehicleid) {

    // No need for 256 cells.
    new
        name[32];

    if (!(400 <= vehicleid <= 611))
        return name;

    vehicleid -= 400;

    strcat(name, VehicleNames[vehicleid]);

    return name;
}
Can be simplified:
pawn Код:
GetVehicleName(modelid)
{
    new name[] = "On Foot"; // can't return a string directly, so have to use a variable
    return (400 <= modelid<= 611) ? VehicleNames[modelid - 400] : name;
}
Reply
#7

^ Or more simplify.. (the simpliest, GetVehicleName separated, probably you also used it somewhere else..)
pawn Код:
public automon(playerid)
{
    if(IsPlayerInAnyVehicle(playerid)) {
        SendClientMessage(playerid,COLOR_RED,GetVehicleName(GetVehicleModel(GetPlayerVehicleID(playerid))));
    }
    return 1;
}
pawn Код:
GetVehicleName(modelid)
{
    return ((400 <= modelid<= 611) ? (VehicleNames[modelid-400]) : ("On Foot"));
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)