Randomizer problem
#1

So what I wanna do is have a floor made out of objects and after a couple of seconds one object will go away, and it will keep doing this untill the floor is entirely gone, I know how to make it but I want to use a randomizer to select which object should get removed next, although at first I had the idea of putting the defined names of the object in the randomizer bit then a friend told me I could also use the object creation details to have my defined objects at all times scrambled.

But here is the problem, how will I be sure that all 15 objects that I put in the randomizer gets used and not just a few over and over again??

Example

pawn Код:
new String:RandomPlate[][1] =
{
    {"Plate1"},
    {"Plate2"},
    {"Plate3"}
};
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
The general way is to select an item, check if it has been removed, and if so repeat. However, this has undefined bounds in that it could in theory run forever. Look up y_iterate (foreach), it has ways to define lists of items and randomly select one with no repetition.
This is for an filterscript, you have any example of how to use that?
Reply
#3

Quote:
Originally Posted by ******
Посмотреть сообщение
Yes, lots, in multiple y_iterate tutorials and topics. It is probably my best documented library.
Ive fixed it but is not working a 100%


pawn Код:
new A = randomEx(-1,26);
    if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    else if(MappingObject1[A]+1 != 0)
    {
        if(A == 26)
        {
            A = -1;
        }
        DestroyObject(MappingObject1[A]);
    }
    return 1;
}

I think the problem is here, I got 25 objects and the randomizer gives A a random number, It checks if that object with that number is not 0, If it is it goes to the next one and adds 1, Am I doing this right?
Also copied it 25 times under each other so it will never not remove an object, But when im testing it, It works ofcourse but sometimes it skips so I think this whole idea doesnt work.

*Edit*
There is a timer that makes it go to this new function around every 7 seconds but it doesnt take away a new object every 7 seconds.
Reply
#4

that's the same thing repeated over and over again, what are you trying to by doing that?
Reply
#5

Quote:
Originally Posted by Crayder
Посмотреть сообщение
that's the same thing repeated over and over again, what are you trying to by doing that?
Im trying to check if the object with number A is not nothing if true delete it, if not add +1 will be added with the next else?

Thats the idea behind it..
Reply
#6

use a loop
Reply
#7

Quote:
Originally Posted by Crayder
Посмотреть сообщение
use a loop
I only want to do it 25 times but thats not the problem, The problem is is that I have 25 objects and around 7 seconds I want to remove 1 random, I set up so it select a random object and it removes it and it keeps on doing that around 7 seconds but sometimes the random number is the same as an object that is already removed, So thats why I made it 25 times to check if the object is already removed then add +1 to the object number.

And the problem is that sometimes it still does not take away an object around 7 seconds so my workaround to fixing the random number to select an object that is not yet removed is not working properly.
Reply
#8

I'm not sure if it's the most efficient way, but try this:
(Credits to Slice for the ShuffleArray-function).

//Top of script:
pawn Код:
new Plates[25];
new PlateOrder[25];
new PlateCount;
new Timer;
//Wherever you want to create the objects:
pawn Код:
Plates[0] = CreateObject(....
Plates[1] = CreateObject(...
Plates[2] = CreateObject(...
(...)
Plates[24] = CreateObject(...
When the game starts:
pawn Код:
PlateCount = 0;
for(new i; i<sizeof(Plates); i++) PlateOrder[i] = i;
ShuffleArray(PlateOrder, sizeof(PlateOrder));
Timer = SetTimer("DestroyPlates", 7000, 1);
pawn Код:
forward DestroyPlates();
public DestroyPlates()
{
    DestroyObject(Plates[PlateOrder[PlateCount]]);
    PlateCount++;
    if(PlateCount == 25)
    {
        //Game ends
        KillTimer(Timer);
    }
}

stock ShuffleArray(array[], size = sizeof(array)) {
    if (size < 2) return;
    for (new i = 0; i < size - 1; i++)  {
        new slot = random(size);
        if (slot == i) continue;
        new temp = array[slot];
        array[slot] = array[i];
        array[i] = temp;
    }
}
Reply
#9

Quote:
Originally Posted by Schneider
Посмотреть сообщение
I'm not sure if it's the most efficient way, but try this:
(Credits to Slice for the ShuffleArray-function).

//Top of script:
pawn Код:
new Plates[25];
new PlateOrder[25];
new PlateCount;
new Timer;
//Wherever you want to create the objects:
pawn Код:
Plates[0] = CreateObject(....
Plates[1] = CreateObject(...
Plates[2] = CreateObject(...
(...)
Plates[24] = CreateObject(...
When the game starts:
pawn Код:
PlateCount = 0;
for(new i; i<sizeof(Plates); i++) PlateOrder[i] = i;
ShuffleArray(PlateOrder, sizeof(PlateOrder));
Timer = SetTimer("DestroyPlates", 7000, 1);
pawn Код:
forward DestroyPlates();
public DestroyPlates()
{
    DestroyObject(Plates[PlateOrder[PlateCount]]);
    PlateCount++;
    if(PlateCount == 25)
    {
        //Game ends
        KillTimer(Timer);
    }
}

stock ShuffleArray(array[], size = sizeof(array)) {
    if (size < 2) return;
    for (new i = 0; i < size - 1; i++)  {
        new slot = random(size);
        if (slot == i) continue;
        new temp = array[slot];
        array[slot] = array[i];
        array[i] = temp;
    }
}
Actually I like your method, but y_iterator is better. Iter_Random is exactly what he needs.
Reply
#10

Quote:
Originally Posted by Schneider
Посмотреть сообщение
I'm not sure if it's the most efficient way, but try this:
(Credits to Slice for the ShuffleArray-function).

//Top of script:
pawn Код:
new Plates[25];
new PlateOrder[25];
new PlateCount;
new Timer;
//Wherever you want to create the objects:
pawn Код:
Plates[0] = CreateObject(....
Plates[1] = CreateObject(...
Plates[2] = CreateObject(...
(...)
Plates[24] = CreateObject(...
When the game starts:
pawn Код:
PlateCount = 0;
for(new i; i<sizeof(Plates); i++) PlateOrder[i] = i;
ShuffleArray(PlateOrder, sizeof(PlateOrder));
Timer = SetTimer("DestroyPlates", 7000, 1);
pawn Код:
forward DestroyPlates();
public DestroyPlates()
{
    DestroyObject(Plates[PlateOrder[PlateCount]]);
    PlateCount++;
    if(PlateCount == 25)
    {
        //Game ends
        KillTimer(Timer);
    }
}

stock ShuffleArray(array[], size = sizeof(array)) {
    if (size < 2) return;
    for (new i = 0; i < size - 1; i++)  {
        new slot = random(size);
        if (slot == i) continue;
        new temp = array[slot];
        array[slot] = array[i];
        array[i] = temp;
    }
}
Thanks so much, I changed it abit but it works now!!

Thank you everyone for helping me or even showing their faces on this topic,

Have a nice night everyone!!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)