Need help with my Vehicle System
#1

Hi,

I'm starting to create a RP script, but I'm stuck for months now with the vehicle system.
I want it to check what next file is available (for example: Vehicle_1 and 2 are used, it should go to Vehicle_3)
I tried to make a code for that:

pawn Код:
stock GetFreeVehicleSlot()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        new string[1000];
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
        if(fexist(string)) return i+1;
    }
    return -1;
}
But it doesn't work properly

My second problem,
I have added "CarID" to the vehicle files and when It spawns it sets the CarID in the file to the ID the car has IG (that all works fine).
When I wanna change my vehicle color it has to run through all the vehicle files and search in what file the CarID is the same as the VehicleID In game.
Code of the command
pawn Код:
command(setcarcolor, playerid, params[])
{
    new col1, col2;
    if(sscanf(params, "dd", col1, col2))
    {
        if(Player[playerid][AdminLevel] >= 4)
        {
            SendClientMessage(playerid, WHITE, "SYNTAX: /setcarcolor [color1] [color2]");
        }
    }
    else
    {
        if(Player[playerid][AdminLevel] >= 4)
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                for(new i = 0; i < MAX_VEHICLES; i++)
                {
                    if(Vehicles[i][CarID] == GetPlayerVehicleID(playerid))
                    {
                        new string[128];
                        {
                            format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
                        }

                        if(fexist(string))
                        {
                            Vehicles[i][Col1] = col1;
                            Vehicles[i][Col2] = col2;
                            ChangeVehicleColor(i, col1, col2);
                            SaveExistingVehicle(i);

                        }
                        else
                        {
                            SendClientMessage(playerid, WHITE, "This is not a saved car!");
                        }
                    }
                }
            }
        }
    }
    return 1;
}
I am doing something wrong but I don't know what :S

Please help me out so I can continue scripting.
Reply
#2

I think you need to put } and {'s between the if(fexists).

Not sure though
Reply
#3

About the first code, i think this will work:

pawn Код:
stock GetFreeVehicleSlot()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        new string[1000];
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
        if(fexist(string)) continue;
        else if(!fexist(string)) return i;
    }
    return -1;
}
about the second , can you show me the stock of SaveExistingVehicle ?
Reply
#4

Let's just have a quick look over your code.

pawn Код:
stock GetFreeVehicleSlot() //You will use GetFreeVehicleSlot() to get available IDs
{
    for(new i = 0; i < MAX_VEHICLES; i++) //You start the loop correctly
    {        
        new string[1000]; //You have an EXTREMELY oversized string which will take up alot of usage.
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i); //You format the file to see if it exists
        if(fexist(string)) return i+1; //This is where you are going wrong (and the string size), basically, if the file exists, you are saying the next available file is the file with a value of 1 more than this. So if file 1 exists, it will return file 2, but file 2 already exists...
    }    
    return -1;
}
Now let's begin to correct it:
pawn Код:
stock GetFreeVehicleSlot()
{    
    new string[40];
    for(new i = 0; i < MAX_VEHICLES; i++)    
    {                
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i); //format the file      
        if(fexist(string)) continue; //If the file exists, go to the next file
        return i; //If the file doesn't exist, then we have an available slot, and we return the number/value of 'i'
    }    
    return -1; //return -1 or INVALID_VEHICLE_ID for no available files
}
Hope I've helped you understand it at least a little bit.
Reply
#5

Lets try it out
Reply
#6

Quote:
Originally Posted by BenzoAMG
Посмотреть сообщение
Let's just have a quick look over your code.

pawn Код:
stock GetFreeVehicleSlot() //You will use GetFreeVehicleSlot() to get available IDs
{
    for(new i = 0; i < MAX_VEHICLES; i++) //You start the loop correctly
    {        
        new string[1000]; //You have an EXTREMELY oversized string which will take up alot of usage.
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i); //You format the file to see if it exists
        if(fexist(string)) return i+1; //This is where you are going wrong (and the string size), basically, if the file exists, you are saying the next available file is the file with a value of 1 more than this. So if file 1 exists, it will return file 2, but file 2 already exists...
    }    
    return -1;
}
Now let's begin to correct it:
pawn Код:
stock GetFreeVehicleSlot()
{    
    new string[40];
    for(new i = 0; i < MAX_VEHICLES; i++)    
    {                
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i); //format the file      
        if(fexist(string)) continue; //If the file exists, go to the next file
        return i; //If the file doesn't exist, then we have an available slot, and we return the number/value of 'i'
    }    
    return -1; //return -1 or INVALID_VEHICLE_ID for no available files
}
Hope I've helped you understand it at least a little bit.
Fixed thank you very much, now my second problem, about that setcarcolor
Reply
#7

I tried something, now it keeps saying it isnt a saved vehicle

pawn Код:
command(setcarcolor, playerid, params[])
{
    new col1, col2;
    if(sscanf(params, "dd", col1, col2))
    {
        if(Player[playerid][AdminLevel] >= 4)
        {
            SendClientMessage(playerid, WHITE, "SYNTAX: /setcarcolor [color1] [color2]");
        }
    }
    else
    {
        if(Player[playerid][AdminLevel] >= 4)
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                for(new i = 0; i < MAX_VEHICLES; i++)
                {
                    new string[128];
                    {
                        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
                    }
                    if(fexist(string))
                    {
                        if(Vehicles[i][CarID] == GetPlayerVehicleID(playerid))
                        {
                            Vehicles[i][Col1] = col1;
                            Vehicles[i][Col2] = col2;
                            ChangeVehicleColor(i, col1, col2);
                            SaveExistingVehicle(i);
                        }
                        else
                        {
                            SCM(playerid, RED, "WTFFFFFFFFFFFFFF");
                        }
                    }
                    else
                    {
                        SendClientMessage(playerid, WHITE, "This is not a saved car!");
                    }
                }
            }
        }
    }
    return 1;
}
Reply
#8

Quote:
Originally Posted by kirollos
Посмотреть сообщение
About the first code, i think this will work:

pawn Код:
stock GetFreeVehicleSlot()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        new string[1000];
        format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
        if(fexist(string)) continue;
        else if(!fexist(string)) return i;
    }
    return -1;
}
about the second , can you show me the stock of SaveExistingVehicle ?
pawn Код:
stock SaveExistingVehicle(carid)
{
    new string[36];
    format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", carid);

    if(fexist(string))
    {
        dini_IntSet(string, "FactionCar", 0);
        dini_IntSet(string, "Model", Vehicles[carid][Model]);
        dini_FloatSet(string, "XSpawn", Vehicles[carid][xspawn]);
        dini_FloatSet(string, "YSpawn", Vehicles[carid][yspawn]);
        dini_FloatSet(string, "ZSpawn", Vehicles[carid][zspawn]);
        dini_FloatSet(string, "AngleSpawn", Vehicles[carid][anglespawn]);
        dini_IntSet(string, "VirtualWorld", Vehicles[carid][VirtualWorld]);
        dini_IntSet(string, "InteriorID", Vehicles[carid][InteriorID]);
        dini_IntSet(string, "Color1", Vehicles[carid][Col1]);
        dini_IntSet(string, "Color2", Vehicles[carid][Col2]);
        dini_IntSet(string, "CarGroup", Vehicles[carid][CarGroup]);
        dini_IntSet(string, "RentalCar", -1);
        dini_Set(string, "vOwner", Vehicles[carid][vOwner]);
        dini_Set(string, "Plate", Vehicles[carid][Plate]);
    }
    return 1;
}
Reply
#9

Try:
pawn Код:
for(new i = 0; i < MAX_VEHICLES; i++)
                {
                    new string[40];
                    format(string, sizeof(string), "Vehicles/Vehicle_%d.ini", i);
                   
                    if(!fexist(string)) continue;
                    if(Vehicles[i][CarID] == GetPlayerVehicleID(playerid))
                    {
                        Vehicles[i][Col1] = col1;
                        Vehicles[i][Col2] = col2;
                        ChangeVehicleColor(i, col1, col2);
                        SaveExistingVehicle(i);
                    }
                }
Should work...
Reply
#10

SaveExistingVehicle bugs also... Dont know what wrong is with it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)