Code stops working when formating text.
#1

Hello SA-MP forums,

The following code stops when formating at the indicated point.

pawn Код:
forward CreateOwnableCar(playerid,vehicle);
public CreateOwnableCar(playerid,vehicle)
{
    if(DebugEnabled == 1) { SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar();"); }
    new string[128];
    if(DebugEnabled == 1) { SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - loop VehicleInfo."); }
    for(new i = 0; i < sizeof(VehicleInfo); i++)
    {
        if(VehicleInfo[i][vOwned] == 0 && IsValidVehicle(i) == 0)
        {
            if(DebugEnabled == 1) { SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - found a free car slot."); }
            if(PlayerInfo[playerid][pCarkey] == 999)
            {
                if(DebugEnabled == 1) { SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - Checking to see if price is good."); }
                if(GetMoney(playerid) >= GetVehiclePrice(vehicle))
                {
                    SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - Starting operation.");//IT STOPS WORKING AFTER THIS!!!
                    format(string, sizeof(string), "You have bought a %s for $%d.",GetVehicleName(vehicle), GetVehiclePrice(vehicle));
                    SendClientMessage(playerid,COLOR_LIGHTBLUE,string);
                    SendClientMessage(playerid,COLOR_WHITE,"Check out /vehiclehelp to see your new commands.");
                    SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - giving out vehicle.");
                    TakeMoney(playerid,GetVehiclePrice(vehicle));
                    BusinessInfo[BuyingVehicle[playerid]][bTill] += GetVehiclePrice(vehicle);
                    VehicleInfo[i][vOwned] = 1;
                    VehicleInfo[i][vModel] = vehicle;
                    VehicleInfo[i][vPrice] = GetVehiclePrice(vehicle);
                    VehicleInfo[i][vColour1] = 1;
                    VehicleInfo[i][vColour2] = 1;
                    VehicleInfo[i][vLocked] = 0;
                    VehicleInfo[i][vSpawnX] = BusinessInfo[BuyingVehicle[playerid]][vSpawnX];
                    VehicleInfo[i][vSpawnY] = BusinessInfo[BuyingVehicle[playerid]][vSpawnY];
                    VehicleInfo[i][vSpawnZ] = BusinessInfo[BuyingVehicle[playerid]][vSpawnZ];
                    VehicleInfo[i][vSpawnR] = BusinessInfo[BuyingVehicle[playerid]][vSpawnR];
                    VehicleInfo[i][vFuel] = 100;
                    VehicleInfo[i][vRadio] = 0;
                    VehicleInfo[i][vHealth] = 1000;
                    VehicleInfo[i][vInterior] = 0;
                    VehicleInfo[i][vSpawned] = 0;
                    format(string, sizeof(string), "%s", GetNameEx(playerid));
                    strmid(VehicleInfo[i][vOwner], string, 0, strlen(string), 255);
                    SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - spawning vehicle.");
                    new carid = AddStaticVehicleEx(VehicleInfo[i][vModel],VehicleInfo[i][vSpawnX],VehicleInfo[i][vSpawnY],VehicleInfo[i][vSpawnZ]+1,VehicleInfo[i][vSpawnR],VehicleInfo[i][vColour1],VehicleInfo[i][vColour2],3600);
                    VehicleOwned[carid] = i;
                    SaveAccountInfo(playerid);
                    SaveVehicle(carid);
                    PlayerInfo[playerid][pCarkey] = i;
                    SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - done.");
                }
                else
                {
                    format(string, sizeof(string), "* You do not have enough money to purchase this vehicle, you need $%d.", GetVehiclePrice(vehicle));
                    SendClientMessage(playerid,COLOR_LIGHTRED,string);
                }
                SendClientMessage(playerid,COLOR_WHITE,"DEBUG: CreateOwnableCar(); - return'ed 1;.");
                return 1;
            }
            else SendClientMessage(playerid,COLOR_LIGHTRED,"* You already own a vehicle.");
            return 1;
        }
    }
    if(DebugEnabled == 1) { printf("DEBUG: CreateOwnableCar(%d, %d) ended.", playerid, vehicle); }
    return 1;
}
Here is some related (but maybe not needed at SAMP forums) code:

pawn Код:
#define                         GetVehicleName(%0)                  VehicleName[GetVehicleModel(%0)-400]

forward GetVehiclePrice(vehicle);
public GetVehiclePrice(vehicle)
{
    new vehicleprice;
    if(vehicle == 481) { vehicleprice = 1200; }
    else if(vehicle == 531) { vehicleprice = 36000; }
    else if(vehicle == 462) { vehicleprice = 10000; }
    else if(vehicle == 463) { vehicleprice = 17000; }
    else if(vehicle == 468) { vehicleprice = 20000; }
    else if(vehicle == 461) { vehicleprice = 25000; }
    else if(vehicle == 517) { vehicleprice = 20500; }
    else if(vehicle == 566) { vehicleprice = 21000; }
    else if(vehicle == 600) { vehicleprice = 20750; }
    else if(vehicle == 412) { vehicleprice = 25000; }
    else if(vehicle == 475) { vehicleprice = 24925; }
    else if(vehicle == 518) { vehicleprice = 24500; }
    else if(vehicle == 422) { vehicleprice = 26000; }
    else if(vehicle == 500) { vehicleprice = 27000; }
    else if(vehicle == 576) { vehicleprice = 27500; }
    else if(vehicle == 474) { vehicleprice = 26500; }
    else if(vehicle == 492) { vehicleprice = 27000; }
    else if(vehicle == 536) { vehicleprice = 27900; }
    else if(vehicle == 551) { vehicleprice = 28000; }
    else if(vehicle == 533) { vehicleprice = 30000; }
    else if(vehicle == 405) { vehicleprice = 30500; }
    else if(vehicle == 561) { vehicleprice = 30000; }
    else if(vehicle == 421) { vehicleprice = 31000; }
    else if(vehicle == 575) { vehicleprice = 32000; }
    else if(vehicle == 534) { vehicleprice = 31500; }
    else if(vehicle == 550) { vehicleprice = 33000; }
    else if(vehicle == 482) { vehicleprice = 35000; }
    else if(vehicle == 466) { vehicleprice = 34500; }
    else if(vehicle == 567) { vehicleprice = 35000; }
    else if(vehicle == 400) { vehicleprice = 38000; }
    else if(vehicle == 535) { vehicleprice = 38500; }
    else if(vehicle == 554) { vehicleprice = 37000; }
    else if(vehicle == 445) { vehicleprice = 37250; }
    else if(vehicle == 491) { vehicleprice = 37000; }
    else if(vehicle == 580) { vehicleprice = 39000; }
    else if(vehicle == 604) { vehicleprice = 20000; }
    else if(vehicle == 483) { vehicleprice = 36000; }
    else if(vehicle == 521) { vehicleprice = 26000; }
    else if(vehicle == 540) { vehicleprice = 35000; }
    else if(vehicle == 545) { vehicleprice = 45000; }
    else if(vehicle == 496) { vehicleprice = 36000; }
    else if(vehicle == 479) { vehicleprice = 38500; }
    else if(vehicle == 603) { vehicleprice = 42000; }
    else if(vehicle == 585) { vehicleprice = 37000; }
    else if(vehicle == 549) { vehicleprice = 40000; }
    else if(vehicle == 547) { vehicleprice = 42000; }
    else if(vehicle == 546) { vehicleprice = 35000; }
    else if(vehicle == 529) { vehicleprice = 47000; }
    else if(vehicle == 527) { vehicleprice = 47000; }
    else if(vehicle == 526) { vehicleprice = 48000; }
    else if(vehicle == 516) { vehicleprice = 46000; }
    else if(vehicle == 509) { vehicleprice = 700; }
    else if(vehicle == 506) { vehicleprice = 50000; }
    else if(vehicle == 508) { vehicleprice = 90000; }
    else if(vehicle == 489) { vehicleprice = 52000; }
    else if(vehicle == 579) { vehicleprice = 53000; }
    else if(vehicle == 602) { vehicleprice = 45000; }
    else if(vehicle == 555) { vehicleprice = 53000; }
    else if(vehicle == 402) { vehicleprice = 52000; }
    else if(vehicle == 558) { vehicleprice = 57000; }
    else if(vehicle == 562) { vehicleprice = 59000; }
    else if(vehicle == 565) { vehicleprice = 59000; }
    else if(vehicle == 477) { vehicleprice = 60000; }
    else if(vehicle == 559) { vehicleprice = 60500; }
    else if(vehicle == 480) { vehicleprice = 68000; }
    else if(vehicle == 415) { vehicleprice = 70000; }
    else if(vehicle == 560) { vehicleprice = 100000; }
    else if(vehicle == 429) { vehicleprice = 75000; }
    else if(vehicle == 541) { vehicleprice = 120000; }
    else if(vehicle == 451) { vehicleprice = 150000; }
    else if(vehicle == 411) { vehicleprice = 160000; }
    else if(vehicle == 473) { vehicleprice = 2000; }
    else if(vehicle == 453) { vehicleprice = 30000; }
    else if(vehicle == 454) { vehicleprice = 80000; }
    else if(vehicle == 452) { vehicleprice = 82000; }
    else if(vehicle == 446) { vehicleprice = 83000; }
    else if(vehicle == 493) { vehicleprice = 90000; }
    else if(vehicle == 469) { vehicleprice = 750000; }
    else if(vehicle == 487) { vehicleprice = 900000; }
    else if(vehicle == 593) { vehicleprice = 1000000; }
    else if(vehicle == 519) { vehicleprice = 1500000; }
    else if(vehicle == 511) { vehicleprice = 1300000; }
    else if(vehicle == 513) { vehicleprice = 1100000; }
    else if(vehicle == 581) { vehicleprice = 29000; }
    else if(vehicle == 522) { vehicleprice = 23000; }
    else if(vehicle == 586) { vehicleprice = 36000; }
    else if(vehicle == 471) { vehicleprice = 100; }
    else { return 0; }
    return vehicleprice;
}
Reply
#2

well, the code is correct.

But this GetVehiclePrice looks rly ugly,
it might cause the code to stop (just guessin' this but it could be)

rewrite it, use switch instead of countless if statements
btw. the code will run faster too like this

Example:

Код:
forward GetVehiclePrice(vehicle);
public GetVehiclePrice(vehicle)
{
        new vehicleprice;
	switch(vehicle)
	{
		case 481: vehicleprice = 1200;
		case 531: vehicleprice = 36000;
		//and so on...
		default: vehicleprice = 0;// wwhen nothing matches
	}
    return vehicleprice;
}
also try leaving out one of the two functions in your format and try if it's working.
Like this, you might see what caused the code to stop
Reply
#3

I believe the problem is on GetVehicleName due to a possible runtime error: Index out of bounds.

Using crashdetect plugin can let you know what caused the code from stopping (most of the times).

If that is the problem, then check if the vehicleid is valid (the modelid between 400 and 611) and I'm sure it won't give any runtime errors. I've seen that many times happens.

I also agree with CutX - switch is MUCH faster!
Reply
#4

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
I believe the problem is on GetVehicleName due to a possible runtime error: Index out of bounds.

Using crashdetect plugin can let you know what caused the code from stopping (most of the times).

If that is the problem, then check if the vehicleid is valid (the modelid between 400 and 611) and I'm sure it won't give any runtime errors. I've seen that many times happens.

I also agree with CutX - switch is MUCH faster!
Quote:
Originally Posted by CutX
Посмотреть сообщение
well, the code is correct.

But this GetVehiclePrice looks rly ugly,
it might cause the code to stop (just guessin' this but it could be)

rewrite it, use switch instead of countless if statements
btw. the code will run faster too like this

Example:

Код:
forward GetVehiclePrice(vehicle);
public GetVehiclePrice(vehicle)
{
        new vehicleprice;
	switch(vehicle)
	{
		case 481: vehicleprice = 1200;
		case 531: vehicleprice = 36000;
		//and so on...
		default: vehicleprice = 0;// wwhen nothing matches
	}
    return vehicleprice;
}
also try leaving out one of the two functions in your format and try if it's working.
Like this, you might see what caused the code to stop
I do agree with both of you on the GetVehiclePrice function, but I was too lazy at the time to do it, but it ain't the problem, I remove GetVehicleName and changed to: "Test" and it worked this time, I need to look at GetVehicleName.
Reply
#5

That should work:
pawn Код:
stock GetVehicleName(vehicleid)
{
    return (400 <= GetVehicleModel(vehicleid) <= 611) ? (VehicleName[GetVehicleModel(vehicleid) - 400]) : ("N/A");
}
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
That should work:
pawn Код:
stock GetVehicleName(vehicleid)
{
    return (400 <= GetVehicleModel(vehicleid) <= 611) ? (VehicleName[GetVehicleModel(vehicleid) - 400]) : ("N/A");
}
Alright, I'll try it out, I'll edit this post when I get the results.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)