Problem with teleporting

Hello all,

So I am having a little problem,

some information:

I need 2 players that get randomly selected from a vector stand next to each other so they can battle, the vector saves the player guid whenever a player signs up with a command ( this is already done before the teleporting) I got this:


bool placement = false;

	 int max = players - 1;

	 for(uint32 i = 2; i < 1; i--)

	 {

	 uint32 random = urand (0,max);

	 if(!placement)

	 {


	 Contestors.push_back(Contestors[random]);

	 Contestors.erase(Contestors.begin()+random);

	 Player* selected = sObjectAccessor->FindPlayer(Contestors[max + 1]);

	 selected->TeleportTo(1, -3737.089600f, 1093.636475f, 131.969437f, 3.150863f);

	 max--;

	 placement = true;

	 }

	 else

	 {


	 Contestors.push_back(Contestors[random]);

	 Contestors.erase(Contestors.begin()+random);

	 Player* selected = sObjectAccessor->FindPlayer(Contestors[max + 1]);

	 selected->TeleportTo(1, -3774.096924f, 1096.924805f, 131.969269f, 6.176457f);

	 max--;

	 }

I want to loop 2 times cause I made an event of it that will be called everytime when a 1v1 battle must start, so thats why the i = 2.

the placement bool is just for other teleporting coordinates, I made a scenario if the random number was 0 and it should work, only its not, no crash but no teleport either.

It should work, I made my own scenario if the random number = 0

so when random = 0, there will be added a new element with the push.back, element number 1 since I tested it on my own and I am element 0, so the new element ( number 1) got the GUID of element 0 so element 0 and 1 are both me, then element number 0 gets erased with the erase cause Contestors.begin() + random would still be 0 since random is 0, then the objectaccessor should find the player with the guid that is in element max +1, which is players - 1, I only signed up so players = 1, so that will make the calculation 1 - 1 + 1 = 1, so it should find the player with the guid of element 1, which is me, and then teleport me, but like I said this doenst work.

Studying what to do:


	 // Select player 1 randomly and place to the end

	 uint32 rand = urand(0, Contestors.size()-1); // size returns the amount of values in the vector and since it starts from 0, it needs to be size -1

	 Contestors.push_back(Contestors[rand]);

	 Contestors.erase(Contestors.begin()+rand);


	 // Select player 2 randomly and place to the end

	 rand = urand(0, Contestors.size()-2); // The player 1 is now at the end of the vector so we need to avoid selecting him, -2

	 Contestors.push_back(Contestors[rand]);

	 Contestors.erase(Contestors.begin()+rand);


	 // Teleport player 2 if found

	 if(Player* selection = sObjectAccessor->FindPlayer(Contestors[Contestors.size()-1]))

		 selection->TeleportTo(1, -3737.089600f, 1093.636475f, 131.969437f, 3.150863f);


	 // Teleport player 1 if found

	 if(Player* selection = sObjectAccessor->FindPlayer(Contestors[Contestors.size()-2]))

		 selection->TeleportTo(1, -3774.096924f, 1096.924805f, 131.969269f, 6.176457f);

Doing it with a loop:


	 bool teleported = false;

	 uint32 max = Contestors.size();

	 for(uint32 i = 1; i <= 2; i++)

	 {

		 uint32 rand = urand(0, max-i);

		 Contestors.push_back(Contestors[rand]);

		 Contestors.erase(Contestors.begin()+rand);

// -1 instead of -i because -1 is the last player in the vector and the 2nd player is pushed to the back of the list, so he is then the last player

		 if(Player* selection = sObjectAccessor->FindPlayer(Contestors[max-1]))

			 if(!teleported)

			 {

				 selection->TeleportTo(1, -3774.096924f, 1096.924805f, 131.969269f, 6.176457f);

				 teleported = true;

			 }

			 else

				 selection->TeleportTo(1, -3737.089600f, 1093.636475f, 131.969437f, 3.150863f);

	 }

Something along those lines.

Wrong section?

Rewrote it:


uint32 max = Contestors.size();

for(uint32 i = 1; i <= 2; i++)

{

    uint32 rand = urand(0, max-i);

    Contestors.push_back(Contestors[rand]);

    Contestors.erase(Contestors.begin()+rand);

    // -1 instead of -i because -1 is the last player in the vector and the 2nd player is pushed to the back of the list, so he is then the last player

    if(!sObjectAccessor->FindPlayer(Contestors[max-1]))

	    return; // put code what you want to do if a player does not exist here

}

sObjectAccessor->FindPlayer(Contestors[max-1])->TeleportTo(1, -3737.089600f, 1093.636475f, 131.969437f, 3.150863f);

sObjectAccessor->FindPlayer(Contestors[max-2])->TeleportTo(1, -3774.096924f, 1096.924805f, 131.969269f, 6.176457f);

Thanks for answering though im more curious why

mine doesnt work

Btw. You should code what to do if a player is not found.

Atm it ignores that and just teleports the players.

for(uint32 i = 2; i < 1; i–)

If i is 2, it is not < 1, so the loop wont ever execute I guess.

Player* selected = sObjectAccessor->FindPlayer(Contestors[max + 1]);

If players was 20, that means that the vector has 20 player guids and it starts from 0, so to access last element, you need to get Contestors[19] (20th player guid)

This means that earlier you set max to be players -1 = 19 and now you are trying to get the contestor number 21 (in vector the key is 20) which does not exist… since Contestors ends at key 19.