Help with selecting random events

I'm trying to run random events that only trigger once using a combination of a RNG and a switch for the event.

e.g.

Label = Random Event

Generate Random number 1-25

If random number = 1
if common event 1 = on
run common event 1
switch common event 1 off
else
Jump to Label - Random Event

If random number = 2
if common event 2 = on
run common event 2
switch common event 2 off
else
Jump to Label - Random Event

And so on...


The problem I'm running into is that the RNG can sometimes take an incredibly long time to generate a number for the event that hasn't been switched off yet.

It seems like there should be an easier and more elegant way to accomplish this

I've read about arrays but I'm not sure how to use them yet. Can these events be stored in an array? Can I select an event from the array, and then remove the event from the array once it's been activated so that it never activates again and I don't run in to this problem with the RNG?


Sorry if this is kind of an easy question, I'm still very new to coding and I don't know JavaScript or any other coding languages.
Any advice or help is appreciated.
The problem with what you're trying to do here is the else you've specified.

What occurs:
  • <label thingy>
  • random number is generated
  • checks if this number is 1
  • if number is not 1
    • go back to <label thingy>
So, what you're doing is generating 1-25, checking if it's 1, and generating a new number. It takes so long because it loops this process every time the result is not 1.

The fix is fairly simple... Remove all the else branches, and place a label after all the checking if random number = # to jump to after your code so that when it hits one that it uses, it will not test the rest of the numbers.

So...

  • If random number = 1
    • if common event 1 = on
    • run common event 1
    • switch common event 1 off
    • jump to end random number check label

  • If random number = 2
    • if common event 2 = on
    • run common event 2
    • switch common event 2 off
    • jump to end random number check label
  • If random number = 1
    • if common event 1 = on
    • run common event 1
    • switch common event 1 off
    • jump to end random number check label

  • If random number = 2
    • if common event 2 = on
    • run common event 2
    • switch common event 2 off
    • jump to end random number check label

Thanks for the reply. This doesn't really accomplish what I'm trying to do though.

In your code if the random number generated is assigned to trigger an event that's been turned off then the generator does nothing.

e.g.

1st pass through the RNG generates the number 5

event 5 runs then gets turned off

If the number 5 is generated again during the next pass through or any other pass through then nothing happens. It's for that very reason I added the else branch so that it generates a number for an event that has not been turned off.

I'd like to figure out some way to generate an array, pick a random item out of that array, then once that item has been picked simply remove it from the array. This way the next time I go through the array to a pick a random item there is no chance that it will pick something that's already been turned off.
This is untested, but here is how I would do it:

In a new, unused game variable, store an array containing the numbers 1-25 with this script call:
Code:
// create a new array
var array = [];
// push numbers 1-25 into it
for (i = 1; i <= 25; i++) {
    array.push(i);
}
// save it in a game variable (otherwise it will be lost when the script call ends)
$gameVariables.setValue(17, array); //change the 17 to the id of the variable you want to use
The above code should only run once to initialize the array. Then on each iteration, you can take a random element out of the array and either save it in another game variable, or use it directly to call a common event (then you wouldn't need all the conditional branches):
Code:
// get the array you stored earlier. Change the 17 to the variable id you're using
var array = $gameVariables.value(17);
// get a random number
var index = Math.floor(Math.random() * array.length);
// save the element at that position in the array
var newId = array[index];
// remove the element from the array
array.splice(index, 1);
// now either store the element (a random number from 1-25 you did not use before) in a game variable, e.g.:
$gameVariables.setValue(18, newId);
// or call the common event directly (then you don't need the line above this):
$gameTemp.reserveCommonEvent(newId);
I hope this helps and I didn't make any mistakes typing this from my phone 😉
T
I hope this helps and I didn't make any mistakes typing this from my phone 😉

Ty, I'll give it a shot as soon as I get the chance. I appreciate the assistance.
It works! You are awesome


I had to do something slightly different at the beginning.

I used conditional statements to populate the array in the variable directly, instead of creating the array and storing it as a variable after.

This method works more dynamically for what I'm doing.
Code:
$gameVariable.setValue(17, []);
If event 1 = on
$gameVariable.value(17).push(1);
If event 2 = on
$gameVariable.value(17).push(2);
...
...
...
then your code works to pick a random index in the variable. Since I'm repopulating the array using conditions every time I run it then I don't need to splice the array.
Code:
script:  var array = $gameVariables.value(17);
         var index = Math.floor(Math.random() * array.length);
         var newId = array[index];
         $gameVariables.setValue(18, newId);

THANK YOU

Original thread: https://forums.rpgmakerweb.com/threads/help-with-selecting-random-events.81208/