SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Optimize this code (https://forum.sa-mp.com/showthread.php?t=655984)

Dice_ 04/07/2018 11:34 AM

Optimize this code
 
how do I optimize this code to reduce lines and makes more understandable?

PHP Code:

        if(GetVehicleEngineType(modelid) == 'H')
        {
            
format(typestr16"Human Powered");
        }
        else if(
GetVehicleEngineType(modelid) == 'D')
        {
            
format(typestr16"Diesel");
        }
        else if(
GetVehicleEngineType(modelid) == 'P')
        {
            
format(typestr16"Petrol");
        }
        else if(
GetVehicleEngineType(modelid) == 'E')
        {
            
format(typestr16"Electric");
        } 


Dice_ 04/07/2018 11:43 AM

Re: Optimize this code
 
Quote:

Originally Posted by ****** (Post 4034149)
Having said that, you can switch on character constants.

The wiki only provides number so I'm confused how to do it with strings.

Calisthenics 04/07/2018 03:33 PM

Re: Optimize this code
 
For a start, do not call GetVehicleEngineType function 4 times.
if/else if can be replaced with a switch.
format is useless when can set string directly.

pawn Code:
switch (GetVehicleEngineType(modelid))
{
    case 'H': typestr = "Human Powered";
    case 'D': typestr = "Diesel";
    case 'P': typestr = "Petrol";
    case 'E': typestr = "Electric";
}


All times are GMT. The time now is 04:53 AM.

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