[Tutorial] How to make an basic Fuel system
#1

Making a basic fuel management.

Beginning with the script, Declarations:
You will need the basic declarations, like where it must store all the fuel values of the car, etc:

pawn Code:
#include <a_samp>
new fuel[MAX_VEHICLES]; //fuel per vehicle
forward timer_fuel_lower(); //timer for lowering the fuel value
forward timer_refuel(playerid); //timer to refuel vehicle
new isrefuelling[MAX_PLAYERS] = 0; //bool to check if player is already refuelling
new Text:td_fuel[MAX_PLAYERS]; //textdraw with fuel
Setting the standard fuel value:
I just used 100 as standard value, you can always use randomize to randomize the fuels, but in my case il use this:

pawn Code:
public OnFilterScriptInit()//when the filterscript loads
{
    for(new i=0;i<MAX_VEHICLES;i++) {
        fuel[i] = 100; //sets every car's fuel to 100 in a loop
    }
    SetTimer("timer_fuel_lower",4200,true); //sets the timer to drop the fuel
    return 1;
}
Prepairing the textdraws when a player spawns:
Setting up the textdraw positions/effects for player.

pawn Code:
public OnPlayerSpawn(playerid)
{
    td_fuel[playerid] = TextDrawCreate(45,324,"Fuel: 100"); //create the textdraw at position
    TextDrawBackgroundColor(td_fuel[playerid],0x00000033); //setting an nice backgroundcolor
    TextDrawFont(td_fuel[playerid],3); //font type of textdraw
    TextDrawLetterSize(td_fuel[playerid],0.699999,1.700000); //size...
    TextDrawColor(td_fuel[playerid],0x000000ff); //color
    TextDrawSetShadow(td_fuel[playerid],3); //dropping the shadow
    return 1;
}
Showing/hiding fuel when player enters/leaves car:
Hiding the fuel textdraw when a player isnt in a car

pawn Code:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if (newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
    {
        new vid = GetPlayerVehicleID(playerid);
        new string[125];format(string,sizeof string,"Fuel:%i",fuel[vid]); //quickly doing a small update on fuel (so it wont jump from 100 to its real value)
        TextDrawSetString(td_fuel[playerid],string);
        TextDrawShowForPlayer(playerid,td_fuel[playerid]); //showing if an player is a driver or passenger of the ar
    } else {
        TextDrawHideForPlayer(playerid,td_fuel[playerid]); //hiding if a player isnt driving/or an passenger
    }
    return 1;
}
Making a /refuel command:
Making a refuelcommand that checks everything out and starts your vehicle refuelling.

pawn Code:
public OnPlayerCommandText(playerid,cmdtext[]) {

    if (!strcmp("/refuel",cmdtext,true,7)) {
        if (!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid,0xFFC800FF,"You are not in an vehicle!"); //if a player isnt in a vehicle, it stops here
        if (isrefuelling[playerid]) return SendClientMessage(playerid,0xFFC800FF,"You are already refuelling!"); //if a player is already refuelling, it stops here
        if (GetPlayerMoney(playerid) - 80 <0) return SendClientMessage(playerid,0xFFC800FF,"You dont have enough money!"); //if a player doesnt have $80 anymore, it stops here
        GivePlayerMoney(playerid,-80); //Sets the player's cash -$80
        SetCameraBehindPlayer(playerid); //Sets the camera behind the player (looks better because the player will be frozen for a few secs)
        TogglePlayerControllable(playerid,0); //freezes the player so he cant drive and refuel at the same time
        isrefuelling[playerid] = 1; //setting isrefuelling to 1 so the player cant spam /refuel
        TextDrawSetString(td_fuel[playerid],"Refuelling..."); //changing textdraw to /refuel
        SetTimerEx("timer_refuel",4500,false,"i",playerid); //setting refueltimer
        return 1;
    }
   
    return 0;
}
Setting up the timers:
This will setup what your timers need to do everytime you call them (so dropping down fuel, and refuelling)

pawn Code:
public timer_fuel_lower()
{
    for(new i=0;i<MAX_PLAYERS;i++) { //loop for all players
        if (isrefuelling[i]) continue; //stop when a player is already refuelling
        new vid = GetPlayerVehicleID(i); //getting vehicle ID
        if (GetPlayerVehicleSeat(i) == 0) { //if the player is a driver (it should only lower the fuel when theres an driver!)
            fuel[vid] = fuel[vid] -1; //lowering fuel value
            if (fuel[vid]<1) //if fuel is empty
            {
                fuel[vid] = 0; //setting fuel to 0 (else the timer will set it to -1 -2 -3 etc before removing player)
                RemovePlayerFromVehicle(i); //remove player out of vehicle
                GameTextForPlayer(i,"~r~You are out of ~w~fuel~r~!",5000,4); //show text
            }
        }
        new string[125];format(string,sizeof string,"Fuel:%i",fuel[vid]); //preparing string with next fuel value
        TextDrawSetString(td_fuel[i],string); //updating textdraw
    }
    return 1;
}

public timer_refuel(playerid)
{
    new vid = GetPlayerVehicleID(playerid);
    fuel[vid] = fuel[vid] = 100; //restoring fuel to 100
    isrefuelling[playerid] = 0;//resetting anti-spam thingy :3
    TextDrawSetString(td_fuel[playerid],"Fuel:100"); //small update on textdraw
    TogglePlayerControllable(playerid,1); //unfreeze player
}
This was my first fuel script, AND first tutorial so dont be hard on me. If you got any tips/ideas tell me!
Reply
#2

This is not a tutorial.
Reply
#3

Quote:
Originally Posted by Carlton
View Post
This is not a tutorial.
I know it looks more like a example how it looks like then a tutorial, but everything is explained step-by-step what it does and why it does. Which is categorisized as "Tutorial", so thats why i posted there.

I thaight it was nice to post, to help some beginners creating there own fuel system.
Reply
#4

nice dude!
funny thing is i released a fs to turn the engine on and was gona add a fuel system for the next version then this came up lol
again very nice easy to follow as well
Reply
#5

Good work, useful for RP servers
Reply
#6

yea nice job
Reply
#7

Thanks guys, made an more advanceder system with speedo here:
https://sampforum.blast.hk/showthread.php?tid=169469
Reply
#8

The script is commented enough to be classed as a tutorial :P Nice job
Reply
#9

Quote:
Originally Posted by Carlton
View Post
This is not a tutorial.
Nope, definitely not!

Quote:
Originally Posted by gamer931215
View Post
I know it looks more like a example how it looks like then a tutorial, but everything is explained step-by-step what it does and why it does. Which is categorisized as "Tutorial", so thats why i posted there.

I thaight it was nice to post, to help some beginners creating there own fuel system.
Like you said, more of an example script. Why don't you check out this thread?
Reply
#10

Well ok, this is my first & last tut then
Reply
#11

i think its sweat dude
Reply
#12

Quote:
Originally Posted by gamer931215
View Post
Well ok, this is my first & last tut then
When you post something, expect people to express their own opinion. Why don't you learn how to create a tutorial correctly and try again?


Quote:
Originally Posted by Lookin
View Post
i think its sweat dude
I believe you mean "sweet"?
Reply
#13

Quote:
Originally Posted by RealCop228
View Post
When you post something, expect people to express their own opinion. Why don't you learn how to create a tutorial correctly and try again??
Well i didnt expect i needed to follow an tutorial to create an tutorial....
I made the script for myself experimenting how to make it , and it worked so i thaight this is helpfull for others too so thats why i posted this non-tut and added explainations on every line what it does and why.

Anyways its still helpfull, people can see how it works, which functions they need and why so they can create there own.
Reply
#14

Ill use that
Reply
#15

useful but when i get into car theres no fuel ( so i get money and refuel it right away ) then i drive it for like an minute and textdraw still shows fuel:100
Reply
#16

Quote:
Originally Posted by gamer931215
View Post
Well i didnt expect i needed to follow an tutorial to create an tutorial....
I made the script for myself experimenting how to make it , and it worked so i thaight this is helpfull for others too so thats why i posted this non-tut and added explainations on every line what it does and why.

Anyways its still helpfull, people can see how it works, which functions they need and why so they can create there own.
I wasn't criticizing you, I was sharing my opinion. You didn't really create a tutorial, so I pointed out what a tutorial would actually be. I apologize if I offended you!
Reply
#17

Quote:
Originally Posted by RealCop228
View Post
I wasn't criticizing you, I was sharing my opinion. You didn't really create a tutorial, so I pointed out what a tutorial would actually be. I apologize if I offended you!
No problem im not mad or anything, only its weard to follow an tutorial to make an tutorial becuase i thaight it was OK this way.
Reply
#18

i posted before that it dont uses up but it does ... it don't uptade but when i got out of car and get back in it was 71 for a sec then it gone back up to 100 (just textdraw)
Reply
#19

Quote:
Originally Posted by Nekrus2
View Post
i posted before that it dont uses up but it does ... it don't uptade but when i got out of car and get back in it was 71 for a sec then it gone back up to 100 (just textdraw)
Do you have other textdraws ? here it was bugged with grandlarc's spawn texts (San Fierro, Las Venturas etc)
try to define it if you have (so instead of "TextDrawCreate(..." use new "td_name = TextDrawCreate...)"

And if you reload the filterscript it can be buggedtoo, you can fix that with a loop killing the textdraw for everyone on filterscript exit.

I used the tutorial script for my speedometer which you can find here:
https://sampforum.blast.hk/showthread.php?tid=169469
Reply
#20

Quote:
Originally Posted by RealCop228
View Post
When you post something, expect people to express their own opinion. Why don't you learn how to create a tutorial correctly and try again?




I believe you mean "sweet"?
lol yeah my bad xD thanks for picking that up
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)