Efficiency of this command
#1

Can this command be improved? Some of my community say it lags for them when they type it.
pawn Код:
CMD:engine(playerid, params[])
{
    new vid = GetPlayerVehicleID(playerid);
    GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective);
    if(engine == 1) {
        SetVehicleParamsEx(vid, 0, lights, alarm, doors, bonnet, boot, objective);
        SendClientMessage(playerid, COLOR_ORANGE, "INFO: {FFFFFF}Engine off.");
    }
    else {
        SetVehicleParamsEx(vid, 1, lights, alarm, doors, bonnet, boot, objective);
        SendClientMessage(playerid, COLOR_ORANGE, "INFO: {FFFFFF}Engine on.");
    }
    return 1;
}
Also how can I actually improve things myself so they run better? I look at codes and think, could this be improved? But I have no idea how to. Thanks.
Reply
#2

check that it's a valid vehicle id
Reply
#3

I'll try that and hope for the best. Thanks for the tip. If anyone else has any information feel free to tell me!
Reply
#4

Would this work better?
pawn Код:
CMD:engine(playerid, params[])
{
    if(!IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, COLOR_ORANGE, "INFO: {FFFFFF} You're not in a vehicle.");
    new vid = GetPlayerVehicleID(playerid);
    GetVehicleParamsEx(vid, engine, lights, alarm, doors, bonnet, boot, objective);
    if(IsValidVehicleID(vid))
        return SendClientMessage(playerid, COLOR_LIGHTRED, "INFO: That is an invalid vehicle.");
       
    if(IsVehicleStreamedIn(vid, playerid))
    {
        if(engine == 1) {
            SetVehicleParamsEx(vid, 0, lights, alarm, doors, bonnet, boot, objective);
            SendClientMessage(playerid, COLOR_ORANGE, "INFO: {FFFFFF}Engine off.");
        }
        else {
            SetVehicleParamsEx(vid, 1, lights, alarm, doors, bonnet, boot, objective);
            SendClientMessage(playerid, COLOR_ORANGE, "INFO: {FFFFFF}Engine on.");
        }
    }
    else
    {
        SendClientMessage(playerid, COLOR_WHITE, "INFO: This vehicle doesn't seem to be streamed in. Contact an admin and tell them!");
    }
    return 1;
}
Reply
#5

pawn Код:
CMD:engine ( playerid , params [] )
{
    new vid = GetPlayerVehicleID ( playerid ) ;
    GetVehicleParamsEx ( vid , engine , lights , alarm , doors , bonnet , boot , objective ) ;
    if ( !IsPlayerInAnyVehicle ( playerid ) ) return SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF} You're not in a vehicle." ) ;
    if ( !IsValidVehicleID ( vid ) ) return SendClientMessage ( playerid , COLOR_LIGHTRED , "INFO: That is an invalid vehicle." ) ;
    if ( !IsVehicleStreamedIn ( vid , playerid ) ) return SendClientMessage ( playerid , COLOR_WHITE , "INFO: This vehicle doesn't seem to be streamed in. Contact an admin and tell them!" ) ;
    else
    {
        if ( engine == 1 )
        {
            SetVehicleParamsEx ( vid , 0 , lights , alarm , doors , bonnet , boot , objective ) ;
            SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF}Engine Off." ) ;
        }
        else
        {
            SetVehicleParamsEx ( vid , 1 , lights , alarm , doors , bonnect , boot , objective ) ;
            SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF}Engine On" ) ;
        }
    }
    return 1;
}
May have typo mistakes and silly too. I am out from my home, dont have my PC
Reply
#6

Quote:
Originally Posted by cessil
Посмотреть сообщение
check that it's a valid vehicle id
This is why I checked to see if it is valid. Thanks Ronaldo. I'll give that ago.
Reply
#7

Quote:
Originally Posted by -Luis
Посмотреть сообщение
This is why I checked to see if it is valid. Thanks Ronaldo. I'll give that ago.
Sure, just inform me the results
Reply
#8

pawn Код:
CMD:engine ( playerid , params [] )
{
       if ( !IsPlayerInAnyVehicle ( playerid ) ) return SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF} You're not in a vehicle." ) ;
    new vid = GetPlayerVehicleID ( playerid ) ;
    GetVehicleParamsEx ( vid , engine , lights , alarm , doors , bonnet , boot , objective ) ;
    if ( !IsValidVehicleID ( vid ) ) return SendClientMessage ( playerid , COLOR_LIGHTRED , "INFO: That is an invalid vehicle." ) ;
    if ( !IsVehicleStreamedIn ( vid , playerid ) ) return SendClientMessage ( playerid , COLOR_WHITE , "INFO: This vehicle doesn't seem to be streamed in. Contact an admin and tell them!" ) ;
    else
    {
            switch(engine){
            case 1:
        {
            SetVehicleParamsEx ( vid , 0 , lights , alarm , doors , bonnet , boot , objective ) ;
            SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF}Engine Off." ) ;
        }
        default:
        {
            SetVehicleParamsEx ( vid , 1 , lights , alarm , doors , bonnect , boot , objective ) ;
            SendClientMessage ( playerid , COLOR_ORANGE , "INFO: {FFFFFF}Engine On" ) ;
        }
    }
    return 1;
}
Thats a bit more efficient since the player could perform the command /engine without being inside a car so you dont need to check the vehicle id, aswell check what usually happens if the vehicle is a valid vehicle id or it tends to stream out more than be an invalid id or try to find a relation with both.

Aswell with the switch.. default you could fix an uncommon issue, for example if the engine takes as value 5 instead of 0 or 1, which probably will never happen, you could turn off the engine with no issues (use a boolean instead).
Reply
#9

Quote:
Originally Posted by kirk
Посмотреть сообщение
[pawn]
Aswell with the switch.. default you could fix an uncommon issue, for example if the engine takes as value 5 instead of 0 or 1, which probably will never happen, you could turn off the engine with no issues (use a boolean instead).
To avoid that, we can use variables. As i have used it.
Reply
#10

Quote:
Originally Posted by Ronaldo_raul™
Посмотреть сообщение
To avoid that, we can use variables. As i have used it.
You are still using a variable, i dont get what you mean, but with "switch" you just switch the value of that variable to multiple cases, the cases it could have, depending on the case it holds.. you do one thing and step the rest, the usage of a boolean which is a variable is more efficient if you got only two cases:

the engine is on(true) or its off (false), i doubt your engine system will be half on and half off, using an integer to hold the state of the engine makes no sense, since the engine will be always on or off.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)