SA-MP Forums Archive
Random team spawns not working - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Random team spawns not working (/showthread.php?tid=650707)



Random team spawns not working - stormchaser206 - 04.03.2018

https://pastebin.com/cBkAnXuQ

The team spawn thing is at the very bottom.

For some reason it only randomly spawns at this one:
Код:
new Float:IndiaSpawns[3][4] =
{ 
		{2841.2942,1285.7273,11.3906,88.5804},
		{2794.3298,1256.1736,11.0299,4.2696},
		{2837.1394,1337.4557,11.0877,82.9403}
};
and none of the other ones, no matter which team you select.

Included the entire thing in case it's something outside that.


Re: Random team spawns not working - AdamsLT - 04.03.2018

I think the problem is that you didn't use any brackets in the code and just wrote it all in one line. That's not how checks are done.
After an IF statement, the code will execute lines up to the first semicolon and that will be considered your block of code for the statement. You can avoid this by using commas or brackets, to indicate blocks of code to execute.

You currently have:

PHP код:
if(PlayerInfo[playerid][pTeam] == TEAM_USArnd random(sizeof(USASpawns)); SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]); SetPlayerFacingAngle(playeridUSASpawns[rnd][3]); 
Which is executed as:
PHP код:
if(PlayerInfo[playerid][pTeam] == TEAM_USA
{
    
rnd random(sizeof(USASpawns)); 
}
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]); 
SetPlayerFacingAngle(playeridUSASpawns[rnd][3]); 
What happens is that your code checks the team, if the team is correct, generates the random number and then uses it for all the SetPlayerPos and FacingAngle lines. Eventually it reaches the last spawn point (India) and leaves the player there.


So two options:

Commas:

PHP код:
if(PlayerInfo[playerid][pTeam] == TEAM_USA
    
rnd random(sizeof(USASpawns)),
    
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]),
    
SetPlayerFacingAngle(playeridUSASpawns[rnd][3]); 
or brackets (recommended for clarity's sake):

PHP код:
if(PlayerInfo[playerid][pTeam] == TEAM_USA
{    
    
rnd random(sizeof(USASpawns)); 
    
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]); 
    
SetPlayerFacingAngle(playeridUSASpawns[rnd][3]);

Hope I didn't mess up and this helps


Re: Random team spawns not working - stormchaser206 - 04.03.2018

Could I still keep it all on the same line with the commas? I'm hoping to save some space here.


Re: Random team spawns not working - AdamsLT - 05.03.2018

Yes, you can.
Although this would barely take up less (if any at all; visually it's less space but I have yet to think of a reason that would ask for this) space than spacing it out and you would sacrifice quite a bit of clarity. I wouldn't recommend it but you do you.


You can also add: return 1 to every if statement so that your code doesn't have to keep checking the other teams to find the correct one after already finding one. Alternatively, if you want to take up less visual space, you can use a combination of if and else if and then you don't have to return inside every statement.
You can also ditch the ifs and just use a switch statement for this part.


Re: Random team spawns not working - Sew_Sumi - 05.03.2018

Random isn't as random as you'd think it would be...


Re: Random team spawns not working - stormchaser206 - 05.03.2018

Quote:
Originally Posted by Sew_Sumi
Посмотреть сообщение
Random isn't as random as you'd think it would be...
What do you mean?

Unrelated, but does anyone know why this might cause a tag mismatch warning:
Код:
GATE_ADMIN_HOUSE1 = CreateAutomaticGate(980, 1491.3000000,-699.9000200,96.5000000, 0.0000000,0.0000000,180.0000000, 1480.8000488281,-699.9000200,96.5000000, 0.000000, 0.0000000, 180.000000, 1491.3000000,-699.9000200,96.5000000, 20.0, 0.003, 1);
GATE_ADMIN_HOUSE2 = CreateAutomaticGate(980, 1502.8000000,-699.9000200,96.5000000, 0.000000, 0.0000000, 0.0000000, 1514.3000488281,-699.9000200,96.5000000, 0.000000, 10.000000, 90.000000, 1502.8000000,-699.9000200,96.5000000, 20.0, 0.003, 1);
command is from the mGates include

also this:
Код:
if(Iter_Contains(mGates, gateid) == 0) return 0; // Doesn't exist
they all cause a tag mismatch