[HELP] fuel system
#1

Hello everyone. Is there a way to make working fuel system and the code for setting fuel is of course undeer ongamemodeinit and then to set fuel for bikes like nrg cars buses and trucks. I have main code and I was already trying to fix my problem with setting fuel for bikes but no one doesn't know and I despreeatly need that fuel system? Thanks
Reply
#2

I'll give you an example of a basic fuel system

first we create 2 variables, one that stores the current fuel of a vehicle and one that stores the maximum fuel of a vehicle
pawn Код:
enum VData
{
    mFuel,
    cFuel
}
new VehicleData[MAX_VEHICLES][VData];
whereas mFuel is the max fuel and cFuel the current fuel.
Now we set a value for these variables when creating a vehicle.
When doing it at OnGameModeInit it could look like this
pawn Код:
public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    return 1;
}
Optional you might want to refuel it automatically when it respawns
pawn Код:
public OnVehicleSpawn(vehicleid)
{
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel];
    return 1;
}
now there are different ways to change the fuel amount... What you need for each is a timer...
this will be started at "OnGameModeInit", too
pawn Код:
SetTimer("FuelChange", 1000, true);
all in all OnGameModeInit could look like this
pawn Код:
public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    SetTimer("FuelChange", 1000, true);
   
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    new vehicleid2 = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid2][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid2][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    return 1;
}
now the actual updating of the fuel...

you can run through all players or all vehicles inside of the FuelChange callback...
Looping through vehicles would be more realistic, as you would really get EACH vehicle, not only the USED vehicles. To keep it more simple, I'll now use version to loop through all players, as this does not require a working engine on/off system
pawn Код:
forward FuelChange();
public FuelChange()
{
    for(new playerid=0; playerid<GetMaxPlayers(); i++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                new vehicleid = GetPlayerVehicleID(playerid);
                if(GetPlayerVehicleSeat(playerid) == 0) //wouldn't make much sense if he was passenger
                {
                    VehicleData[vehicleid][cFuel]--; //reduce the fuel amount by 1 (EACH SECOND)
                }
            }
        }
    }
    return 1;
}
This version will reduce the vehicle fuel each second by 1. It would be more realistic to make it use different values for different speeds....
this could look similar to this
remember, this is just another version
pawn Код:
enum VData
{
    mFuel,
    cFuel,
    cuFuel
}
new VehicleData[MAX_VEHICLES][VData];

public OnGameModeInit() //it doesn't need to be at OnGameModeInit, depends where you create the vehicle
{
    SetTimer("FuelChange", 500, true);
   
    new vehicleid = CreateVehicle(400, 0.0, 0.0, 0.0, 0.0, -1, -1, 300); //works the same way with AddStaticVehicle, etc.
    VehicleData[vehicleid][mFuel] = 100; //100 is just an example, change it to the fuelamount you want
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel]; //we want to start with a full tank
    VehicleData[vehicleid][cuFuel] = 0;
    return 1;
}

public OnVehicleSpawn(vehicleid)
{
    VehicleData[vehicleid][cFuel] = VehicleData[vehicleid][mFuel];
    VehicleData[vehicleid][cuFuel] = 0;
    return 1;
}

forward FuelChange();
public FuelChange()
{
    for(new playerid=0; playerid<GetMaxPlayers(); i++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(IsPlayerInAnyVehicle(playerid))
            {
                new vehicleid = GetPlayerVehicleID(playerid);
                if(GetPlayerVehicleSeat(playerid) == 0) //wouldn't make much sense if he was passenger
                {
                    new speed = GetVehicleSpeed(vehicleid);
                    if(speed < 10)
                        VehicleData[vehicleid][cuFuel] += 1;
                    else if(speed < 30)
                        VehicleData[vehicleid][cuFuel] += 2;
                    else if(speed < 70)
                        VehicleData[vehicleid][cuFuel] += 3;
                    else if(speed < 130)
                        VehicleData[vehicleid][cuFuel] += 4;
                    else if(speed < 180)
                        VehicleData[vehicleid][cuFuel] += 5;
                    else if(speed < 220)
                        VehicleData[vehicleid][cuFuel] += 6;
                    else if(speed >= 220)
                        VehicleData[vehicleid][cuFuel] += 7;
                    if(VehicleData[vehicleid][cuFuel] >= 120)
                    {
                        VehicleData[vehicleid][cuFuel] = 0;
                        VehicleData[vehicleid][cFuel]--;
                    }
                }
            }
        }
    }
    return 1;
}

stock GetVehicleSpeed(vehicleid)
{
    new Float:Pos[3], rst;
    GetVehiclePos(vehicleid, Pos[0], Pos[1], Pos[2]);
    rst = floatround((250 * ((Pos[0] * Pos[0]) + (Pos[1] * Pos[1]) + (Pos[2] * Pos[2]))), floatround_round);
    return rst;
}
there are quite a lot ways to realise this.
Remember that this is just a basic layout that you could and should expand more and make it fit to your imaginations..
just that you get an idea how you could start it
(I did not compile this but just wrote it up in the browser, therefor I can't promise there won't be any errors due to typos)
Reply
#3

Thank you I have like this code which set fuel depend on type of vehicle but the only problem is for bikes like nrg etc I was make to set 15.0 L but it's set 40.0 L

pawn Код:
for(new i; i< MAX_VEHICLES; i++)
    {
        if(IsATruck(GetVehicleModel(i))) Fuel[i] = 100.0;
        else if(IsABus(GetVehicleModel(i))) Fuel[i] = 80.0;
        else if(IsABike(GetVehicleModel(i))) Fuel[i] = 15.0;
        else Fuel[i] = 40.0; // this set fuel for other vehicles which is not added above this code and I think that this code somehow setting bikes fuel to 40 and I don't have any codes anywhere in the script for setting the fuel this is the only one
    }
Reply
#4

show us the "IsABike" function
Reply
#5

I was posting and it's fine all you will see for your self. Thanks
Also I was trying to remove from code else Fuel[i] = 40.0; but when I remove this then when I enter in some vehicle the fuel is 0.0 I don't know why but the problem is here that else Fuel[i] = 40.0;

here

pawn Код:
stock IsABike(id)
{
    if(id == 462 || id == 448 || id == 581 || id == 522 || id == 461 || id == 521 || id == 523 || id == 463 || id == 468 || id == 471) return 1;
    return 0;
}
Reply
#6

yea your code really is fine..

what could although be the problem is, that all this is called before the vehicle is created..
where/when are you creating the bikes? 'cause if you call all that first, it will set it to 40.0 if you afterwards create the bike, it will still have 40.0 ofc.
Reply
#7

this is only for ownership bikes I have special public for that when is server started I from ongamemodeinit load those bikes maybe if I put that code from ongamemodeinit to that public maybe then it will set fuel to 15.0?
Reply
#8

yea that could work....

I personally wouldn't move it completely but, either just call the function again, or manually set it to 15 when creating the bike
if you don't know what vehicle is created, you could just create a nother function that just sets the fuel of the new vehicle and not of all (it's a little bit more effective, although you might not even notice the difference)
could be like
pawn Код:
stock SetVehicleFuel(vehicleid)
{
    new modelid = GetVehicleModel(vehicleid);
    if(IsATruck(modelid)) Fuel[vehicleid] = 100.0;
    else if(IsABus(modelid)) Fuel[vehicleid] = 80.0;
    else if(IsABike(modelid)) Fuel[vehicleid] = 15.0;
    else Fuel[vehicleid] = 40.0;
    return 1;
}
Reply
#9

I was try your way but the fuel is set to 40.0? Thanks
Reply
#10

Quote:
Originally Posted by Luca12
Посмотреть сообщение
I was try your way but the fuel is set to 40.0? Thanks
His code is correct its something to do with IsABike I guess. Maybe the bike you were using wasn't mentioned in the code.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)