Spawn a Random Vehicle by a Command?
#1

So Im new at this scripting thing, and was looking for a way to:

Spawn a car with a command. But the car must NOT be pre defined, ie, random.

Now my first idea was to simple create a command /spawncar and in it, put something like this:
(note: I DONT REMEMBER THE COMMANDS SO IM JUST GONNA PUT SOME RANDOM EASY ENG TEXT)


{
CreateVehicle('method to get random id',infront of player x,y,z );

}

But we dont just want a rendom ID, since there are a LOT of useless trailers and stairs and planes and helicopters and what not, here: https://sampwiki.blast.hk/wiki/Vehicle_Model_ID_List

According to that, the vehicle IDs are between 400 and 611, which include a lot of useless junk, is there any way to like, exclude them from spawning?

here a scenario:

*Player goes to a random lot*
*Player types: "/spawncar"
*A Turismo spawns
*He types again
*a NRG spawns

Thats what I want. No Planes, boats, aircraft, or trailers and junk.

Thanks.
Reply
#2

Код:
new Float:RandomVehicles[][] =
{
	{400},
	{401},
	{402},
	{403},
	{404},
	{405},
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{} << when all is added dont add ","
};

public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/randomcar", cmdtext, true, 10) == 0) //randomcar is the command they'll be using to get car
	{
 		new Random = random(sizeof(RandomVehicles));
 		new Float:x, Float:y, Float:z;
	    GetPlayerPos(playerid, x, y, z);
		CreateVehicle(Random, x, y, z, 90, -1, -1, 60); //CreateVehicle([vehicle id], [x position], [y position], [z position], [rotation], [car color 1 (-1 = random color)], [car color 2], [respawn time(seconds)]
		return 1;
	}
	return 0;
}
Reply
#3

Hey budy. I've done this script for you

pawn Код:
#include    a_samp
#include    zcmd

#define     MAX_RANDOM_VEHICLES ( 144 )

new AllCars[ MAX_RANDOM_VEHICLES ] = {
    445,602,416,485,568,429,433,499,424,536,496,
    504,422,609,498,401,575,518,402,541,482,431,
    438,457,527,483,524,415,542,589,437,532,480,
    596,599,597,598,578,486,507,562,585,427,419,
    587,490,528,533,544,407,565,455,530,526,466,
    604,492,474,588,434,502,503,494,579,545,411,
    546,559,508,571,400,403,517,410,551,500,418,
    572,423,414,516,582,467,443,470,404,514,603,
    600,413,426,436,547,489,515,479,
    534,505,442,440,475,543,605,495,567,428,
    405,535,458,580,439,561,409,560,550,506,
    574,566,549,420,459,576,525,531,406,583,451,
    558,552,540,491,412,478,421,529,555,456,554,
    477,406,556,444,573,539
    },
    PlayerVehicle[ MAX_PLAYERS ] = -1
;

CMD:spawncar( playerid, params[ ] )
{
    new rand_car = random( MAX_RANDOM_VEHICLES - 1 ), s_car = AllCars[ rand_car ], interior = GetPlayerInterior( playerid ), Float:cPos[ 4 ];

    // Get Player Position
    GetPlayerPos( playerid, cPos[ 0 ], cPos[ 1 ], cPos[ 2 ] );
    GetPlayerFacingAngle( playerid, cPos[ 3 ] );
   
    // Destroy Last Vehicle
    DestroyVehicle( PlayerVehicle[ playerid ] );
   
    // Create Vehicle
    PlayerVehicle[ playerid ] = AddStaticVehicleEx( s_car, cPos[ 0 ]+3, cPos[ 1 ], cPos[ 2 ], cPos[ 3 ], -1, -1, -1 );

    // Set Vehicle Parameters
    LinkVehicleToInterior( PlayerVehicle[ playerid ], interior );
    SetVehicleNumberPlate( PlayerVehicle[ playerid ], "My Car" );
    SetVehicleVirtualWorld( PlayerVehicle[ playerid ], GetPlayerVirtualWorld( playerid ) );
   
    // Put Player in Vehicle
    PutPlayerInVehicle( playerid, PlayerVehicle[ playerid ], 0 );
    return ( 1 );
}
Reply
#4

Quote:
Originally Posted by ic3cr3am
Посмотреть сообщение
Код:
new Float:RandomVehicles[][] =
{
	{400},
	{401},
	{402},
	{403},
	{404},
	{405},
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{}, << all car's id you want here
	{} << when all is added dont add ","
};

public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/randomcar", cmdtext, true, 10) == 0) //randomcar is the command they'll be using to get car
	{
 		new Random = random(sizeof(RandomVehicles));
 		new Float:x, Float:y, Float:z;
	    GetPlayerPos(playerid, x, y, z);
		CreateVehicle(Random, x, y, z, 90, -1, -1, 60); //CreateVehicle([vehicle id], [x position], [y position], [z position], [rotation], [car color 1 (-1 = random color)], [car color 2], [respawn time(seconds)]
		return 1;
	}
	return 0;
}
A 2D array with Float tag? Did you even try to compile (not that it's going to be compiled..) before posting this?

---

Quote:
Originally Posted by nGen.SoNNy
Посмотреть сообщение
pawn Код:
...
new rand_car = random( MAX_RANDOM_VEHICLES - 1 ), s_car = AllCars[ rand_car ], interior = GetPlayerInterior( playerid ), Float:cPos[ 4 ];...
Why -1, the last one is never going to be returned like that.
Reply
#5

Well, I understood some of it, Thanks both of you, later today Im going to try this. Im just gonna remove the put player in vehicle line since I don't want that, but Thanks a million! (I haven't tried it yet, so I may come back )
Reply
#6

Well, it doesn't work, I dont really know where to place what. I've just placed everything before any "Public"s, along with the initial #includes

Make me understand like newb!
Reply
#7

pawn Код:
//CMD:randomcar(playerid) ...
new rand = 399 + random(211);
CreateVehicle(rand, ........)
Reply
#8

Oh, so I put that under my command? I'll try...
Reply
#9

Creates all vehs, will make a code to make the one u asked later.
pawn Код:
CMD:CreateCar(playerid, params[])
{
    new rand = random(211) + 1 + 400;
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid, x, y, z);
    CreateVehicle(rand, x +5 , y + 5, z, 0, random(230), random(230), 0);
    return 1;
}
EDIT:
Quote:
Originally Posted by newbienoob
Посмотреть сообщение
pawn Код:
//CMD:randomcar(playerid) ...
new rand = 399 + random(211);
CreateVehicle(rand, ........)
Possiblity of getting 399, which isnt a vehicle id. So, may crash.
Reply
#10

An array (nGen.SoNNy's way) would be the best option.

@nabeelfarooqui98: Would you mind to post the code that didn't work so we can help you on that.

@newbie scripter: He doesn't want totally random because he wants to get rid of the trailers and such as vehicles.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)