PDA

View Full Version : Random with case (Variables)


Groc
18/03/2017, 07:17 PM
So i remember when i made random gift from pickups it had one problem that if i put checking with variable, random case will be always the same. I will explain it better
so for example

if(pInfo[playerid][IsPlayerInfected] == 0)
{
new slot, weap, ammo;
for ( slot = 0; slot < 14; slot++ )
{
GetPlayerWeaponData(playerid,slot,weap,ammo);
if(IsValidWeapon(weap))
{
new randomselect = random(5); // i put random 5 because i want give a chance win nothing.
switch(randomselect)
{
case 0:
{
new randomammo = random(15);
GivePlayerWeapon(playerid,weap,randomammo);
format(string,sizeof(string),"~g~Found~w~: %d ammo",randomammo);
GameTextForPlayer(playerid,string,4000,5);
DestroyPickup(drop[playerid]);
}
case 1:
{
new randomxp = random(7);
format(string,sizeof(string),"~g~Found~w~: %d XP",randomxp);
GameTextForPlayer(playerid,string,4000,5);
pInfo[playerid][pXP] += randomxp;
DestroyPickup(drop[playerid]);
}
So with this code i can get ammo, exp, and nothing. So if i do something like this

new slot, weap, ammo;
for ( slot = 0; slot < 14; slot++ )
{
GetPlayerWeaponData(playerid,slot,weap,ammo);
if(IsValidWeapon(weap))
{
new randomselect = random(5); // i put random 5 because i want give a chance win nothing.
switch(randomselect)
{
case 0:
{
new randomammo = random(15);
GivePlayerWeapon(playerid,weap,randomammo);
format(string,sizeof(string),"~g~Found~w~: %d ammo",randomammo);
GameTextForPlayer(playerid,string,4000,5);
DestroyPickup(drop[playerid]);
}
case 1:
{
new randomxp = random(7);
format(string,sizeof(string),"~g~Found~w~: %d XP",randomxp);
GameTextForPlayer(playerid,string,4000,5);
pInfo[playerid][pXP] += randomxp;
DestroyPickup(drop[playerid]);
}
case 2:
{
if(pInfo[playerid][IsPlayerInfected] == 1 || pInfo[playerid][IsPlayerBlind] == 1)
{
GameTextForPlayer(playerid, "Cure", 5000, 2);
CurePlayer(playerid);
DestroyPickup(drop[playerid]);
}
}
}
}
Now if player get "IsPlayerInfected", "IsPlayerBlind" case 2 will be always selected. How can i avoid this and make chance to get case 0, case 1?

Groc
18/03/2017, 10:20 PM
Up up up

Groc
19/03/2017, 09:02 AM
Bump

Toroi
19/03/2017, 11:10 AM
Are you sure your whole function needs to run in a loop?

for ( slot = 0; slot < 14; slot++ )

It does not make sense for me, can you explain it? Perhaps it is the problem