SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 18/05/2018, 01:02 PM   #1
Hunud
Gangsta
 
Join Date: Jun 2016
Posts: 680
Reputation: 21
Default Optimizing Code

Hey.

I'm creating something and i made something like this.

Code:
DV[0] = AddStaticVehicleEx(560, 2880.3086, 505.3661, 2.2880, 181.0076, -1, -1, 100);
	DV[1] = AddStaticVehicleEx(560, 2923.7188, 502.2674, 21.5422, 270.2661, -1, -1, 100);
	DV[2] = AddStaticVehicleEx(560, 2921.6636, 526.5562, 21.6482, 181.7665, -1, -1, 100);
	DV[3] = AddStaticVehicleEx(560, 2946.2083, 388.2856, 23.0324, 325.0709, -1, -1, 100);
	DV[4] = AddStaticVehicleEx(560, 2991.2759, 416.1230, 38.6820, 0.0000, -1, -1, 100);
	DV[5] = AddStaticVehicleEx(560, 3060.6064, 516.6152, 40.4406, 55.8450, -1, -1, 100);
	DV[6] = AddStaticVehicleEx(560, 3086.6035, 554.4461, 40.4406, 55.8450, -1, -1, 100);
	DV[7] = AddStaticVehicleEx(560, 3061.0850, 547.1687, 40.4406, 145.0495, -1, -1, 100);
	DV[8] = AddStaticVehicleEx(560, 3058.3582, 572.7344, 40.4406, 145.0495, -1, -1, 100);
	DV[9] = AddStaticVehicleEx(560, 3028.5815, 501.0857, 18.0132, 91.5284, -1, -1, 100);
	DV[10] = AddStaticVehicleEx(560, 3033.1167, 523.1443, 18.0132, 178.7994, -1, -1, 100);
	DV[11] = AddStaticVehicleEx(560, 2988.3513, 430.0921, 10.4093, 89.1893, -1, -1, 100);
	DV[12] = AddStaticVehicleEx(560, 2988.3689, 465.5955, 10.4093, 89.1893, -1, -1, 100);
	DV[13] = AddStaticVehicleEx(560, 2945.1116, 465.8649, 10.4093, 269.3174, -1, -1, 100);
	DV[14] = AddStaticVehicleEx(560, 2946.5115, 448.1189, 10.4093, 269.3174, -1, -1, 100);
	DV[15] = AddStaticVehicleEx(560, 2888.0315, 528.2197, 2.2880, 270.4115, -1, -1, 100);
	DV[16] = AddStaticVehicleEx(560, 3036.1333, 523.9805, 3.8455, 88.2404, -1, -1, 100);
	DV[17] = AddStaticVehicleEx(560, 3035.3584, 501.6172, 3.8455, 88.2404, -1, -1, 100);
	DV[18] = AddStaticVehicleEx(560, 3035.0259, 479.1101, 3.8455, 88.2404, -1, -1, 100);
	DV[19] = AddStaticVehicleEx(560, 3001.9460, 478.8606, 3.8455, 268.2070, -1, -1, 100);
And when player join the event.

Code:
	if(DVPlayers == 0)
	{
	PutPlayerInVehicle(playerid,DV[0], 0);
	}
	else if(DVPlayers == 1)
	{
	PutPlayerInVehicle(playerid,DV[1], 0);
	}
	else if(DVPlayers == 2)
	{
	PutPlayerInVehicle(playerid,DV[2], 0);
	}
	else if(DVPlayers == 3)
	{
	PutPlayerInVehicle(playerid,DV[3], 0);
	}
	else if(DVPlayers == 4)
	{
	PutPlayerInVehicle(playerid,DV[4], 0);
	}
	else if(DVPlayers == 5)
	{
	PutPlayerInVehicle(playerid,DV[5], 0);
	}
	else if(DVPlayers == 6)
	{
	PutPlayerInVehicle(playerid,DV[6], 0);
	}
	else if(DVPlayers == 7)
	{
	PutPlayerInVehicle(playerid,DV[7], 0);
	}
	else if(DVPlayers == 8)
	{
	PutPlayerInVehicle(playerid,DV[8], 0);
	}
	else if(DVPlayers == 9)
	{
	PutPlayerInVehicle(playerid,DV[9], 0);
	}
	else if(DVPlayers == 10)
	{
	PutPlayerInVehicle(playerid,DV[10], 0);
	}
	else if(DVPlayers == 11)
	{
	PutPlayerInVehicle(playerid,DV[11], 0);
	}
	else if(DVPlayers == 12)
	{
	PutPlayerInVehicle(playerid,DV[12], 0);
	}
	else if(DVPlayers == 13)
	{
	PutPlayerInVehicle(playerid,DV[13], 0);
	}
	else if(DVPlayers == 14)
	{
	PutPlayerInVehicle(playerid,DV[14], 0);
	}
	else if(DVPlayers == 15)
	{
	PutPlayerInVehicle(playerid,DV[15], 0);
	}
	else if(DVPlayers == 16)
	{
	PutPlayerInVehicle(playerid,DV[16], 0);
	}
	else if(DVPlayers == 17)
	{
	PutPlayerInVehicle(playerid,DV[17], 0);
	}
	else if(DVPlayers == 18)
	{
	PutPlayerInVehicle(playerid,DV[18], 0);
	}
	else if(DVPlayers == 19)
	{
	PutPlayerInVehicle(playerid,DV[19], 0);
	}
Is there any way to make it better/shorter ?
__________________
I need a hero:
Quote:
Originally Posted by Meller View Post
At least my ass is smart, so I guess that makes me a smartass?
[/I]
Hunud is offline   Reply With Quote
Old 18/05/2018, 01:16 PM   #2
jlalt
High-roller
 
Join Date: Oct 2014
Posts: 1,370
Reputation: 338
Default Re: Optimizing Code

For the second:
PHP Code:
for(new 0DVPlayerssizeof DVi++) 
{
        if(
== j)
    {
        
PutPlayerInVehicle(playerid,DV[i], 0);
    }

Or also just:
PHP Code:
if(<= DVPlayers sizeof DV
{
    
PutPlayerInVehicle(playerid,DV[DVPlayers], 0);

For the first you can make new function for ex
AddStaticVehicleEx2 which assigns the var value to the AddStaticVehicleEx returned id....
jlalt is offline   Reply With Quote
Old 18/05/2018, 02:16 PM   #3
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,473
Reputation: 291
Default Re: Optimizing Code

I see the first one fine, the second one on the other hand can be done in one line like what jlalt suggested
Code:
PutPlayerInVehicle(playerid,DV[DVPlayers], 0);
__________________
Code:
I'm available to script stuff for cash PM me here or on discord @ Andy[RDZ]#0040
[Tutorial]:Anti-cheat guide ||Mini-game 101||VC/LC maps install||
Array index out of bounds


[Github]:Link [Pastebin]:Link [Forum]:Link [Server]:Link [Discord]:Link




RogueDrifter 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
Optimizing code maximthepain Scripting Help 5 31/08/2017 11:21 AM
optimizing code Bondz Scripting Help 4 18/09/2016 05:19 AM
optimizing rothhear Help Archive 0 19/03/2010 02:21 AM


All times are GMT. The time now is 08:05 AM.


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