Iter_Random, Spectate. - 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: Iter_Random, Spectate. (
/showthread.php?tid=593069)
Iter_Random, Spectate. -
JaKe Elite - 01.11.2015
I am doing a Spectate System with the Derby. It sometimes work, and it sometimes don't.
It also spectates a player which is not on the Derby Round (even though i coded it in the way that the Iter_Random would repeat itself if the return value is a playerid which is not in the Derby Round.)
How can i fix this?
PHP код:
new specplayer = Iter_Random(Player);
if(specplayer == playerid)
{
specplayer = Iter_Random(Player);
}
else
{
if(OnDerby[specplayer] == false)
{
specplayer = Iter_Random(Player);
}
else
{
StartSpectate(playerid, specplayer);
}
}
Note; Sorry if i coded it bad or in a way. I am just trying to do the stuffs on my way. <RIP my grammar>
Re: Iter_Random, Spectate. -
prineside - 01.11.2015
For example, there are 3 players on your server:
0 - You
1 - Player on derby round
2 - Someone else (we don't want to spectate him)
Iter_Random works with whole iterable and doesn't pop items from it. And that means that one item may appear two or more times in a row when we call Iter_Random
Let's run it. For example:
PHP код:
new specplayer = Iter_Random(Player); // "specplayer" now contains 0 (chance 33.3%)
if(specplayer == playerid) // 0 is you, so this condition works
{
specplayer = Iter_Random(Player); // We get another random player and get 0 again (yes, it's possible)
}
else // this part of code doesn't execute because specplayer == playerid
{
if(OnDerby[specplayer] == false)
{
specplayer = Iter_Random(Player);
}
else
{
StartSpectate(playerid, specplayer);
}
}
// ...and we have specplayer = 0, which is you (it's an error)
The second example:
PHP код:
new specplayer = Iter_Random(Player); // now we got 2 - the player we don't want to spectate
if(specplayer == playerid) // this is false because 2 != 0
{
specplayer = Iter_Random(Player);
}
else // this part of code is executing
{
if(OnDerby[specplayer] == false) // We are checking if player 2 is on derby and getting false
{
specplayer = Iter_Random(Player); // ...so we are asking for a new player... and get 2 again
}
else // this part of code doesn't run
{
StartSpectate(playerid, specplayer);
}
}
// ...and now we got "specplayer" = 2 whick is not a player on derby
You shold use recursion (warning! may caise an infinite loop if there are no players on derby) / create separate iterable for players on derby / simply use "for" loop / use another algorythm for better performance (no time to explain it)
Re: Iter_Random, Spectate. -
JaKe Elite - 01.11.2015
I am testing it with 3 players in the hosted server, yet the server spectates the player which is not on the derby.
Thank you for the tip doe using a separated Iter.