28.08.2014, 14:08
How to give cash, scores, etc to Random Player when player spawn or something else
forward RandomMoney();
new Players;
public OnGameModeInit()
{
SetTimer("RandomMoney,100000,1); Timer to give the random money
return 1;
}
public OnPlayerConnect(playerid)
{
Players++; // Each time player joins the server
return 1;
}
public OnPlayerDisconnect(playerid)
{
Players--; // Each time player leaves the server
return 1;
}
public RandMoney()
{
GivePlayerMoney(random(Players),50); // Giver random player 50$
}
pawn Код:
pawn Код:
pawn Код:
pawn Код:
pawn Код:
|
stock RandomPlayer()
{
new
ConnectedPlayers[MAX_PLAYERS] = {INVALID_PLAYER_ID, ...},
idx;
for(new i = 0; i != MAX_PLAYERS; ++i)
{
if(IsPlayerConnected(i))
{
ConnectedPlayers[idx] = i;
idx++;
}
}
if(!idx)
return INVALID_PLAYER_ID;
return ConnectedPlayers[random(idx)];
}
pawn Код:
pawn Код:
pawn Код:
pawn Код:
pawn Код:
|
This way is bad, since player IDs are not sorted.
You can see player IDs are like this: 0, 3, 4, 5, 7, 15, 19, 41. In the above example, total connected players is : 8 So random ( 8 ) can't return the correct 'random' player ID. I Have a better way: pawn Код:
|
public RandMoney()
{
GivePlayerMoney(RandomPlayer(), 50); // Giver random player 50$
}