Selecting something from an array of data
#1

I am trying to select a random box that has not been selected already.

Well, I have an array of spawn positions for objects(a box). I want a random number to be chosen from 3 to 10, this will be the number of boxes that will spawn. then I want to select that many number of boxes from an array randomly but not a box that has already been chosen.

here is what i have so far:

array:
Код:
new Float:RandomBoxSpawn[][6] =
{
    // Positions, (X, Y, Z, rX, rY, rZ)
    {2783.76,-2450.73,14.596,0,0,0},
    {2783.67,-2449.26,13.797,0,0,0},
    {2791.95,-2461.6,13.796,0,0,0},
    {2791.87,-2487.78,14.593,0,0,0},
    {2797.43,-2487.67,13.793,0,0,0},
    {2783.74,-2499.64,14.659,0,0,0},
    {2783.75,-2407.97,13.827,0,0,0},
    {2791.9,-2426.13,14.633,0,0,0},
    {2783.83,-2424.59,13.055,0,0,0},
    {2791.93,-2447.75,13.044,0,0,0}
};
Code to run to select boxes and create them
Код:
public Createboxes(playerid)
{
	new ammountofboxes = randomEx(3,10);
        while (i <= ammountofboxes)
	{
	    for(new index=1; index<11; index++)
		{
		    if(!IsValidObject(PlayerInfo[playerid][TempoObject][index]))
		    {
                             new randbox = random(sizeof(RandomBoxSpawn));
                             //HERE IS WHERE I WANT A RANDOM BOX TO BE SELECTED BUT WHERE IT CANNOT BE SELECTED TWICE
		    
		             i++;
		    }
		}
	}
return 1;
}
Reply
#2

please help me, this is slowing me down
Reply
#3

Anyone here pro enough to even come up with a script of this type?
Reply
#4

pawn Код:
#define MAX_BOXES           10

new Float:RandomBoxSpawn[MAX_BOXES][6] =
{
    // Positions, (X, Y, Z, rX, rY, rZ)
    {2783.76,-2450.73,14.596,0,0,0},
    {2783.67,-2449.26,13.797,0,0,0},
    {2791.95,-2461.6,13.796,0,0,0},
    {2791.87,-2487.78,14.593,0,0,0},
    {2797.43,-2487.67,13.793,0,0,0},
    {2783.74,-2499.64,14.659,0,0,0},
    {2783.75,-2407.97,13.827,0,0,0},
    {2791.9,-2426.13,14.633,0,0,0},
    {2783.83,-2424.59,13.055,0,0,0},
    {2791.93,-2447.75,13.044,0,0,0}
};


public Createboxes(playerid)
{
    new BoxNums[MAX_BOXES], bool:NewBoxFound = false;

    // Fill the array with invalid numbers (-1), since blank arrays contain "0" and might be a box-index from RandomBoxSpawn
    for (new i; i < MAX_BOXES; i++)
        BoxNums[i] = -1;

    new ammountofboxes = randomEx(3,10);

    // For the first box, just choose a random one and store it's index in the array
    BoxNums[0] = random(sizeof(RandomBoxSpawn));

    // Loop through the remaining amount of boxes that has been chosen
    for (new i = 1; i < amountofboxes; i++)
    {
        // Reset the NewBoxFound to false
        NewBoxFound = false;

        // Continue searching until a spare box has been found
        while (NewBoxFound == false)
        {
            // Select a new random box
            NextBox = random(MAX_BOXES);

            // First set this to true before checking
            NewBoxFound = true;

            // Check if it's already chosen
            for (new j; j < MAX_BOXES; j++)
            {
                if (BoxNums[j] == NextBox)
                {
                    NewBoxFound = false;
                }
            }

            // If the next randomly chosen box isn't used yet, store it in the next index of the array
            if (NexBoxFound == true)
                BoxNums[i] = NewBox;
        }
    }

    new Float:x, Float:y, Float:z, BoxNum;

    // Now it's time to create them
    for (new i; i < amountofboxes; i++)
    {
        BoxNum = BoxNums[i];

        x = RandomBoxSpawn[BoxNum][0];
        y = RandomBoxSpawn[BoxNum][1];
        z = RandomBoxSpawn[BoxNum][2];
        CreateObject (boxmodel, x, y, z, ...
    }

    return 1;
}
Something like this?
This code should find a random number of unique boxes and stores them in the BoxNums array.
Then, after it, your code can come to create them.
Reply
#5

Check my answer here: https://sampforum.blast.hk/showthread.php?tid=560866
You can use same pattern...
Reply
#6

nice, just what i needed.

THANKS
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)