[Tutorial] How to make a fast engine system!
#1

How to make a fast engine system!

Hello! In this tutorial I'll be teaching you how to make a fast and easy to use engine activation command and stock!

How to make a fast engine system!

What this does is allow you to activate an engine whenever and not only in a command.

How to make a fast engine system!

You will need the following includes!
  • zcmd
  • a_samp
** Includes are given full credits to there rightful owner. **

To begin, place these at the top of your script!
pawn Код:
#include <a_samp> // THIS IS THE MOST IMPORTANT INCLUDE BY THE SA-MP TEAM
#include <zcmd> // THIS IS USED TO PROCESS COMMANDS FASTER!

Next, you will need to place some variables, these variables will tell if the engine is off or on!
pawn Код:
new IsEngineOn[MAX_PLAYERS];// THIS IS A VARIABLE!
Once you have placed this in, you will need to create a section for your stocks, these will be used to turn on and off your engine!
pawn Код:
stock TurnEngineOn(playerid); //THE START OF THE STOCK
{
    new vehicleid = GetPlayerVehicleID(playerid), engine, lights, alarm, doors, bonnet, boot, objective; //THIS GETS THE VEHICLE ID
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective); //THIS GETS THE PARAMS FOR THE VEHICLE!
    SetVehicleParamsEx(vehicleid, 1, lights, alarm, doors, bonnet, boot, objective); //THIS UPDATES THE PARAMS OF THE VEHICLE TO TURN THE ENGINE ON!
    IsEngineOn[playerid] = 1; //THIS TELLS THE VARIABLE THAT IT'S ON!
}
stock TurnEngineOff(playerid);
{
    new vehicleid = GetPlayerVehicleID(playerid), engine, lights, alarm, doors, bonnet, boot, objective; //THIS GETS THE VEHICLE ID
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective); //THIS GETS THE PARAMS FOR THE VEHICLE!
    SetVehicleParamsEx(vehicleid, 0, lights, alarm, doors, bonnet, boot, objective); //THIS UPDATES THE PARAMS OF THE VEHICLE TO TURN THE ENGINE OFF!
    IsEngineOn[playerid] = 0; //THIS TELLS THE VARIABLE THAT IT'S OFF!
}
Now, you have the stocks, the variables, and the includes, you will then need to create the command itself, to do this, we'll be using zcmd as listed above. Using ZCMD will process the command tons faster then the usual command processor by SA-MP.

pawn Код:
CMD:engine(playerid, params[]) //THE COMMAND THE PLAYER TYPES
{
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "You must be in a vehicle to use this command."); //IT TELLS THEM IF THEY'RE ARE NOT IN A VEHICLE!
    if(IsEngineOn[playerid] == 1) // WE'RE USING THE VARIABLE WE STORED AND TELL IT IF IT'S (1 == ON) OR (0 == OFF)
    {
      TurnEngineOn(playerid);
    }
    else
    {
      TurnEngineOff(playerid);
    }
    return 1;
}
You can use this for other purposes too!
Below, you will find OnPlayerEnterVehicle and OnPlayerExitVehicle, these public functions detect if the player is entering or exiting a vehicle.

pawn Код:
public OnPlayerEnterVehicle(playerid) //THE PUBLIC FUNCTION
{
     TurnEngineOn(playerid); //TURNS THE ENGINE ON
     return 1;
}

public OnPlayerExitVehicle(playerid)// THE PUBLIC FUNCTION
{
     TurnEngineOff(playerid); //TURNS THE ENGINE OFF
     return 1;
}
Or you may simply download/copy it from here.
PASTEBIN #1 - http://pastebin.com/0z4uQKkY
Reply
#2

How can it be "faster" than simply using SetVehicleParamsEx on the engine command instead of the stock function. Just adds more unnecessary code to your gamemode.

Also, https://sampforum.blast.hk/showthread.php?tid=570635
Reply
#3

Quote:
Originally Posted by Luis-
Посмотреть сообщение
How can it be "faster" then simply using SetVehicleParamsEx on the engine command instead of the stock function. Just adds more unnecessary code to your gamemode.

Also, https://sampforum.blast.hk/showthread.php?tid=570635
It can be faster when activating and de-activating the engine on different occasions.
Reply
#4

Quote:
Originally Posted by Luis-
Посмотреть сообщение
^ That, and if I may be honest, you haven't explained much either.

Just for the sake of convenience, you should rename the IsEngineOn variable to something like: 'EngineStatus' and declare it as a bool:
PHP код:
new bool:EngineStatus[MAX_PLAYERS]; 
By declaring it as a boolean, you will no longer have to use the '==' in if statements that use this variable.
PHP код:
if(EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 1
and
PHP код:
if(!EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 0
That is just for the sake of convenience though.

You declare all the variables that will be put to use in the function's body. Okay, but why? I know why but I am 100 percent sure that other people that don't know will question that. In that case I prefer to go to the wiki and figure it out myself or seek a better tutorial. I am talking about the variables used for the SetVehicleParamsEx function.
Reply
#5

It'll be the same speed no matter what way you do it.
Reply
#6

Quote:
Originally Posted by Overhaul
Посмотреть сообщение
^ That, and if I may be honest, you haven't explained much either.

Just for the sake of convenience, you should rename the IsEngineOn variable to something like: 'EngineStatus' and declare it as a bool:
PHP код:
new bool:EngineStatus[MAX_PLAYERS]; 
By declaring it as a boolean, you will no longer have to use the '==' in if statements that use this variable.
PHP код:
if(EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 1
and
PHP код:
if(!EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 0
That is just for the sake of convenience though.

You declare all the variables that will be put to use in the function's body. Okay, but why? I know why but I am 100 percent sure that other people that don't know will question that. In that case I prefer to go to the wiki and figure it out myself or seek a better tutorial. I am talking about the variables used for the SetVehicleParamsEx function.
Standard(non-boolean) variables can do this too, I don't see your point. And @OP, coding shouldn't be a "fast" process it should be done in an efficient pace but also it should be done so that it works as it should. Fast implies that you are somewhat rushing, something which is a very bad practice in coding.
Reply
#7

Quote:
Originally Posted by Abagail
Посмотреть сообщение
Standard(non-boolean) variables can do this too, I don't see your point.
I know.

Quote:
Originally Posted by myself
That is just for the sake of convenience though.
Reply
#8

Quote:
Originally Posted by Luis-
Посмотреть сообщение
How can it be "faster" than simply using SetVehicleParamsEx on the engine command instead of the stock function. Just adds more unnecessary code to your gamemode.

Also, https://sampforum.blast.hk/showthread.php?tid=570635
Using the keyword "stock" or not doesn't make a difference (as for performance), so why even bother?

Quote:
Originally Posted by Jonny5
Посмотреть сообщение
also the use of the "stock" modifier is pointless
you know the 2 functions are gonna be used in the CMD.
That's not really the reason why people use it. It's more accessible and can be used to goto functions when you want to apply changes to it.
Reply
#9

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Using the keyword "stock" or not doesn't make a difference (as for performance), so why even bother?

That's not really the reason why people use it. It's more accessible and can be used to goto functions when you want to apply changes to it.
Well for searching your code might be one reason...
but that's not likely to be the case with most people.
They do it because they don't know better.
Personally my code is split up in away i can find anything easy without the search/find


but in this case the 2 functions where pointless and not even needed to achieve the same result.
Using them actually would be slower, not that i think we would notice a difference in speed.

~J5
Reply
#10

Quote:
Originally Posted by Overhaul
Посмотреть сообщение
^ That, and if I may be honest, you haven't explained much either.

Just for the sake of convenience, you should rename the IsEngineOn variable to something like: 'EngineStatus' and declare it as a bool:
PHP код:
new bool:EngineStatus[MAX_PLAYERS]; 
By declaring it as a boolean, you will no longer have to use the '==' in if statements that use this variable.
PHP код:
if(EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 1
and
PHP код:
if(!EngineStatus[playerid]) 
which is equal to
PHP код:
if(EngineStatus[playerid] == 0
That is just for the sake of convenience though.

You declare all the variables that will be put to use in the function's body. Okay, but why? I know why but I am 100 percent sure that other people that don't know will question that. In that case I prefer to go to the wiki and figure it out myself or seek a better tutorial. I am talking about the variables used for the SetVehicleParamsEx function.
This would be a great point in another language (not PAWN), but remember PAWN is typeless.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)