SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 04/07/2018, 11:34 AM   #1
Dice_
Huge Clucker
 
Dice_'s Avatar
 
Join Date: Sep 2015
Location: Indonesia
Posts: 450
Reputation: 57
Default 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_ is offline   Reply With Quote
Old 04/07/2018, 11:43 AM   #2
Dice_
Huge Clucker
 
Dice_'s Avatar
 
Join Date: Sep 2015
Location: Indonesia
Posts: 450
Reputation: 57
Default Re: Optimize this code

Quote:
Originally Posted by ****** View Post
Having said that, you can switch on character constants.
The wiki only provides number so I'm confused how to do it with strings.
Dice_ is offline   Reply With Quote
Old 04/07/2018, 03:33 PM   #3
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 875
Reputation: 156
Default 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";
}
Calisthenics 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
Help me optimize the code skiplovebra Scripting Help 12 14/09/2017 09:25 AM
You can optimize this code? wharlos Scripting Help 5 03/07/2016 08:49 PM
optimize code JoshNudock Scripting Help 4 22/06/2015 08:38 PM
Optimize this code... Baltimore Scripting Help 3 15/11/2014 08:45 PM
help me to optimize my code armyoftwo Help Archive 3 14/11/2010 11:19 AM


All times are GMT. The time now is 05:47 PM.


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