PDA

View Full Version : Random team spawns not working


stormchaser206
04/03/2018, 10:57 PM
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.

AdamsLT
04/03/2018, 11:53 PM
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:

if(PlayerInfo[playerid][pTeam] == TEAM_USA) rnd = random(sizeof(USASpawns)); SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]); SetPlayerFacingAngle(playerid, USASpawns[rnd][3]);

Which is executed as:
if(PlayerInfo[playerid][pTeam] == TEAM_USA)
{
rnd = random(sizeof(USASpawns));
}
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]);
SetPlayerFacingAngle(playerid, USASpawns[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:

if(PlayerInfo[playerid][pTeam] == TEAM_USA)
rnd = random(sizeof(USASpawns)),
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]),
SetPlayerFacingAngle(playerid, USASpawns[rnd][3]);

or brackets (recommended for clarity's sake):

if(PlayerInfo[playerid][pTeam] == TEAM_USA)
{
rnd = random(sizeof(USASpawns));
SetPlayerPos(playerid,USASpawns[rnd][0],USASpawns[rnd][1],USASpawns[rnd][2]);
SetPlayerFacingAngle(playerid, USASpawns[rnd][3]);
}

Hope I didn't mess up and this helps :)

stormchaser206
04/03/2018, 11:58 PM
Could I still keep it all on the same line with the commas? I'm hoping to save some space here.

AdamsLT
05/03/2018, 12:03 AM
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.

Sew_Sumi
05/03/2018, 12:21 AM
Random isn't as random as you'd think it would be...

stormchaser206
05/03/2018, 12:57 AM
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