SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 07/09/2019, 12:48 PM   #1
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 885
Reputation: 85
Default Fuel problem

I made a fuel system for all vehicles, it works but i have 2 problems:

pawn Code:
if(VehicleFuelLiters[vehicleid] <= 10.0)
{
    Beep(i);

    InformPlayer(i, 4000, "Your ~y~gas tank ~w~is almost ~r~empty~w~!~n~~n~You should find a nearest ~y~gas station ~w~to refill your vehicle.");
}
else if(VehicleFuelLiters[vehicleid] <= 0.0)
{
    RemovePlayerFromVehicle(i);

    VehicleFuelLiters[vehicleid] = 0.0;

    InformPlayer(i, 4000, "~r~You ran out of fuel.");
}

(This code is placed in a timer of 1 second )

First problem is related to "your gas tank is almost empty blabla", it keeps repeating every second.
Second problem is even if vehicle fuel reaches 0 or lower, it doesn't eject player from vehicle and the fuel starts to go negative.

Any help? Thanks.
Symon is offline   Reply With Quote
Old 07/09/2019, 01:22 PM   #2
Jonny
Huge Clucker
 
Jonny's Avatar
 
Join Date: Nov 2009
Location: Italy
Posts: 280
Reputation: 38
Default Re: Fuel problem

Code:
if(VehicleFuelLiters[vehicleid] <= 10.0 && VehicleFuelLiters[vehicleid] > 0.0)
{
    Beep(i);

    InformPlayer(i, 4000, "Your ~y~gas tank ~w~is almost ~r~empty~w~!~n~~n~You should find a nearest ~y~gas station ~w~to refill your vehicle.");
}
Can you show me the part where you go -1 on VehicleFuelLiters[vehicleid]?
__________________
Jonny is offline   Reply With Quote
Old 07/09/2019, 01:36 PM   #3
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 885
Reputation: 85
Default Re: Fuel problem

It's the second "if", where i use RemovePlayerFromVehicle.
Symon is offline   Reply With Quote
Old 07/09/2019, 01:49 PM   #4
Jonny
Huge Clucker
 
Jonny's Avatar
 
Join Date: Nov 2009
Location: Italy
Posts: 280
Reputation: 38
Default Re: Fuel problem

There you set it to 0 if it goes to negative values, i mean where you just go as example from 100 to 99, full timer would be better
__________________
Jonny is offline   Reply With Quote
Old 07/09/2019, 03:34 PM   #5
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 272
Reputation: 27
Default Re: Fuel problem

The message keeps repeating, because it is in a 1 second timer and where is no check, if the warning was sent already. There are multiple solutions to this.
Code:
//global player var
new bool:alreadyShown[MAX_PLAYERS];

//before sending message
if(!alreadyShown[i])
{
   //Show message
   alreadyShown[i]=true;
}
The second problem is the first condition. it is always true if fuel is <= 10.0, even when negative.
Because of this else if is not called.
Add && VehicleFuelLiters[vehicleid] > 0.0
Plastikmensch is offline   Reply With Quote
Old 07/09/2019, 04:05 PM   #6
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 885
Reputation: 85
Default Re: Fuel problem

Ok i fixed the eject from vehicle.

About the show message, what if i re-enter another vehicle and i run out of fuel? The message will not be shown again because it's already true.

It's ok if i set it to false after the player exit from vehicle?

Tell me if it's correct:

pawn Code:
if(VehicleFuelLiters[vehicleid] <= 10.0 && VehicleFuelLiters[vehicleid] > 0.0)
{
    if(!FuelMessageWarningShown[i])
    {
        Beep(i);

        InformPlayer(i, 4000, "Your ~y~gas tank ~w~is almost ~r~empty~w~!~n~~n~You should find a nearest ~y~gas station ~w~to refill your vehicle.");

        FuelMessageWarningShown[i] = true;                         
    }
}
    else if(VehicleFuelLiters[vehicleid]  < 0.1)
    {
        if(VehicleFuelLiters[vehicleid] < 0) VehicleFuelLiters[vehicleid] = 0.0;

        FuelMessageWarningShown[i] = false;
                       
        RemovePlayerFromVehicle(i);
        InformPlayer(i, 4000, "~r~You ran out of fuel.");
}
Symon is offline   Reply With Quote
Old 07/09/2019, 04:25 PM   #7
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 272
Reputation: 27
Default Re: Fuel problem

Yes, looks right.
You should probably also set it to false in OnPlayerExitVehicle, if the message was shown and the player leaves the vehicle voluntarily.
Plastikmensch is offline   Reply With Quote
Old 07/09/2019, 05:07 PM   #8
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 885
Reputation: 85
Default Re: Fuel problem

You're right. Another question. This is the fuel consumption based on speed. Is there a better way to accomplish this?

pawn Code:
if(GetVehSpeed(vehicleid) > 0 && GetVehSpeed(vehicleid) <= 30)
{
    VehicleFuelLiters[vehicleid] -= 0.1;
}
   else if(GetVehSpeed(vehicleid) > 30 && GetVehSpeed(vehicleid) <= 90)
{
    VehicleFuelLiters[vehicleid] -= 0.2;
}
else if(GetVehSpeed(vehicleid) > 90 && GetVehSpeed(vehicleid) <= 150)
{
    VehicleFuelLiters[vehicleid] -= 0.4;
}
else if(GetVehSpeed(vehicleid) > 150 && GetVehSpeed(vehicleid) <= 221)
{
    VehicleFuelLiters[vehicleid] -= 0.6;
}
else if(GetVehSpeed(vehicleid) > 221)
{
    VehicleFuelLiters[vehicleid] -= 0.9;
}
Symon is offline   Reply With Quote
Old 07/09/2019, 05:42 PM   #9
Plastikmensch
Huge Clucker
 
Join Date: Aug 2016
Posts: 272
Reputation: 27
Default Re: Fuel problem

You could use a formula for fuel consumption that fits your needs, instead of using if-else or switch statements.
Also in things like this, it is advised to create a variable, instead of calling a function repeatedly.
Code:
new speed=GetVehSpeed(vehicleid);
Formula example:
Code:
VehicleFuelLiters[vehicleid]-=0.003*GetVehSpeed(vehicleid);//You can edit this to your liking
//consumption in steps of 10 from 0 to 290 with 0.003:
speed: 0, fuel decrease: 0.000000
speed: 10, fuel decrease: 0.030000
speed: 20, fuel decrease: 0.060000
speed: 30, fuel decrease: 0.090000
speed: 40, fuel decrease: 0.120000
speed: 50, fuel decrease: 0.150000
speed: 60, fuel decrease: 0.180000
speed: 70, fuel decrease: 0.210000
speed: 80, fuel decrease: 0.240000
speed: 90, fuel decrease: 0.270000
speed: 100, fuel decrease: 0.300000
speed: 110, fuel decrease: 0.330000
speed: 120, fuel decrease: 0.360000
speed: 130, fuel decrease: 0.390000
speed: 140, fuel decrease: 0.420000
speed: 150, fuel decrease: 0.450000
speed: 160, fuel decrease: 0.480000
speed: 170, fuel decrease: 0.509999
speed: 180, fuel decrease: 0.540000
speed: 190, fuel decrease: 0.569999
speed: 200, fuel decrease: 0.600000
speed: 210, fuel decrease: 0.629999
speed: 220, fuel decrease: 0.660000
speed: 230, fuel decrease: 0.689999
speed: 240, fuel decrease: 0.720000
speed: 250, fuel decrease: 0.750000
speed: 260, fuel decrease: 0.780000
speed: 270, fuel decrease: 0.810000
speed: 280, fuel decrease: 0.840000
speed: 290, fuel decrease: 0.870000

//for testing, you can use this:
for(new i;i<300;i+=10)
{
   printf("speed: %i, fuel decrease: %f",i,0.003*i);
}
Plastikmensch is offline   Reply With Quote
Old 08/09/2019, 12:18 AM   #10
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 885
Reputation: 85
Default Re: Fuel problem

I think i dont get it. Where should i place the fuel decrease code and that speed formula?
Symon is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fuel system problem all vehicles use same fuel(enum variables, timers) grymtn Scripting Help 7 09/04/2017 08:16 PM
[FilterScript] Fuel System [Fuel/Speed/Vehicle Name/Health] reddromero Filterscripts 5 10/05/2014 02:50 AM
Custom Fuel system taking 2 fuel instead of 1. ricardo178 Scripting Help 6 27/04/2013 10:16 AM
Problem's with fuel ivanVU Scripting Help 10 30/09/2012 08:53 PM
Fuel[i] -= 1; (Insted of taking 1, takes all fuel) HondaCBR Scripting Help 2 15/04/2012 01:03 PM


All times are GMT. The time now is 03:09 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.