SA-MP Forums Archive
Choose random speedcamera - 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: Choose random speedcamera (/showthread.php?tid=454509)



Choose random speedcamera - wumpyc - 29.07.2013

What I would like to do is to make a loop for MAX_CAMERAS and then randomly choose 1 cameraid so I could set it for a mission
under this :P
Код:
stock M_Job(playerid) //mechanic job
{
	SendClientMessage(playerid,-1,"Mechanic /work not finished yet :D Sorraaay!");
	return 1;
}



Re: Choose random speedcamera - Jstylezzz - 29.07.2013

I'd make sure there's a variable holding the amount of existing speedcams, so the script doesn't take a non existing one. Then I'd use the random function to randomly choose one of the existing cam id's.
pawn Код:
new camid = random(ExistingCamsVar);



Re: Choose random speedcamera - Threshold - 29.07.2013

pawn Код:
stock GetRandomCamera()
{
    new number = random(GetCameraCount());
    new camcount = 0;
    for(new i = 0; i < MAX_CAMERAS; i++)
    {
        format(file, sizeof(file), CAMERA_PATH, i);
        if(!fexist(file)) continue;
        camcount++;
        if(camcount == number) return i;
    }
    return -1;
}

stock GetCameraCount()
{
    new file[40];
    new count = 0;
    for(new i = 0; i < MAX_CAMERAS; i++)
    {
        format(file, sizeof(file), CAMERA_PATH, i);
        if(fexist(file))
        {
            count++;
        }
    }
    return count;
}
This should work. I have split them into stock functions just in case they may be useful in other situations.

Example of it's use:
pawn Код:
CMD:getrandomcameraid(playerid, params[])
{
    new fstr[50];
    format(fstr, sizeof(fstr), "There are a total of %d cameras.", GetCameraCount());
    SendClientMessage(playerid, 0xFFFF00FF, fstr);
    format(fstr, sizeof(fstr), "Random Camera ID: %d", GetRandomCamera());
    SendClientMessage(playerid, 0xFFFF00FF, fstr);
    return 1;
}



Re: Choose random speedcamera - wumpyc - 29.07.2013

Thanks Benzo man. I appreciate it