Choose random speedcamera
#1

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;
}
Reply
#2

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);
Reply
#3

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;
}
Reply
#4

Thanks Benzo man. I appreciate it
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)