spawning cars randomly; problem with getting script to work
#1

Hi
I have problem with making the following script to work :
(in OnPlayerCommandText)

Код:
if(strcmp(cmd, "/random", true) == 0){
		new counter = 0;
		new Float:xxx, Float:yyy, Float:zzz;
		for(new k=0;k<2000;k++)
		{
		DestroyVehicle(k); //Destroy all the cars on the server
		}
		for(new s=0;s<10;s=s+1)
		{
		SetPlayerPosFindZ(playerid,float(random(660)),float(random(500)),500.0); //Set random position and find Z
		new Float:xh, Float:yh, Float:zh;
		GetPlayerPos(playerid,xh,yh,zh);	// save coordinates after SetPlayerPosFindZ
		SetPlayerPosFindZ(playerid, 0,0,500); // move player somewhere else, so the car wouldn't spawn exactly where he is
		new idveh = CreateVehicle(411,xh,yh,zh,random(360),random(127),random(127),120); // create infernus with random angle and colors
		GetVehiclePos(idveh, xxx, yyy, zzz);	//get the position of car
		xh = 0.0;
		yh = 0.0;
		zh = 0.0;
		counter = counter + 1;
		}
		new msg[48];
		format(msg,sizeof(msg),"you made %i cars",counter );
		SendClientMessage(playerid,0xFFFFFFFF,msg);
		SetPlayerPos(playerid,xxx,yyy,zzz); // move player to the location of car (it should be the last one, right?)
		return 1;
		}
I wrote comments, so I hope you understand the code. So, after I write /random, instead of moving to random position ( SetPlayerPosFindZ(playerid,float(random(660)),floa t(random(500)),500.0); ) I keep standing in the same place, and after about 1 second, 10 cars spawn, just where I am.
It looks like this (I walked away a bit, so you can see the cars, I spawn "inside" them (because of SetPlayerPos(playerid,xxx,yyy,zzz); ):



I made a few changes in code, but never managed to make it work ... Maybe someone here can deal with this ?
Reply
#2

Im taking a shot at this. I made 2 commands. 1 for spawning one car at random. The other to delete all cars, and spawn 10 cars at random, then drop you at the last car created. These have not been tested, but theoretically should work.

Pastebin link for code
Код:
if(strcmp(cmd, "/randomcar", true) == 0) //Creates 1 car in random location.
{
	new Float:vehiclex, Float:vehicley, Float:vehiclez; //declare vehicle coordinates
	new Float:playerx, Float:playery, Float:playerz;  //declare player coordinates
	
	SetPlayerPosFindZ(playerid, float(random(660)), float(random(500)), 500.0); //place player in random location
	GetPlayerPos(playerid, playerx, playery, playerz); //detect where the player is
	new idveh = CreateVehicle(411, playerx, playery, playerz, float(random(360)), random(127), random(127), 60000); //create vehicle at that location
	
	SetPlayerPos(playerid, playerx, playery, playerz+1.0); //sets player above car so he doesnt spawn inside the car
	return 1;
}

if(strcmp(cmd, "/randomcars", true) == 0) //Destroys all cars and creates 10 in random locations, dropping you at the last one created.
{
	new Float:vehiclex, Float:vehicley, Float:vehiclez; //declare vehicle coordinates
	new Float:playerx, Float:playery, Float:playerz;  //declare player coordinates
	new CarCount;
	for(new c=0; c<=2000;)
	{
		DestroyVehicle©;
		c++;
	}
	
	for(new k=0; k<=10;)
	{
		SetPlayerPosFindZ(playerid, float(random(660)), float(random(500)), 500.0); //place player in random location
		GetPlayerPos(playerid, playerx, playery, playerz); //detect where the player is
		new idveh = CreateVehicle(411, playerx, playery, playerz, float(random(360)), random(127), random(127), 60000); //create vehicle at that location
	
		SetPlayerPos(playerid, playerx, playery, playerz+1.0); //sets player above car so he doesnt spawn inside the car
		CarCount++; //increases car count each time the loop passes
		k++;
	}
	new msg[48];
	format(msg,sizeof(msg),"you made %d cars", CarCount); //sets message string to tell you how many cars have been created.
	SendClientMessage(playerid,0xFFFFFFFF,msg); //sends string that was created
	return 1;	
}
Reply
#3

if you want to put the cars to spawn good then use "AddStaticVehicle" in OneGameModeInit
Reply
#4

That's not the problem Sfinx. I believe that a contributing factor is lag.. When using 'SetPlayerPosFindZ' there is going to be a lagging moment.

Here's an example:
Let's say you type this and there is a 50 millisecond ping. That's 50 milliseconds it takes for you to see server activity after it has happend, and vice versa (your activity to the server). If you use SetPlayerFindZ and then immediatly use GetPlayerPos, it's going to receive the player's current position that the server has stored in it's memory, but the SetPlayerFindZ still has 50 milliseconds before the player is moved, and then another 50 milliseconds before the server picks up on the player having been moved. So in essence, your 'find a random position and retrieve it's coordinate' isn't going to work.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)