MV How do I trigger randomized non-repeating events?

I have a small dungeon that has 6 events in it (one is a trap, another grants an HP potion, another boosts MP, etc - nothing super fancy).

I can already make it so that I can trigger a random event; I created a variable that chooses a random number between 1 and 6 and then it runs the event corresponding to the number that was picked. So, for example, if the random variable picked the number 2, it gives the player a nice HP potion.

But then in another area of the dungeon the random variable sometimes chooses the same number as it chose the previous time. But what I want to happen is for the random variable to be "smart" enough to disregard any random numbers it already picked and therefore cycle through all 6 possible events without repeating a number.

For example in the first event the player got a potion and in the second event the player triggers a trap, and so on until all 6 events have played out and none repeat (thus preventing the player from having sprung 6 traps).

This is a repeatable dungeon by the way so that's why I'm trying to make this random so it's at least a little different each time through, otherwise I would just assign one event to one trigger. I'm trying to keep the player from knowing that "the potion is always on this tile" and "the trap is always on this other tile".

I hope I explained this well enough and I always appreciate everyone's help!

Have a great day!
you can either do this by eventing or by script, and six is barely doable as the limit on eventing - anything larger and you would have to go by script.

with eventing, you'll need five or six variables to store previous amounts and to count the numbers rolled, rerolling the random value until getting a new value (and comparing it to every previous value every roll).
the logic to do that can get rather complicated the more numbers and tries you want.

the scripted way is to have an array with every number possible, roll for random on the array size to get an element and then remove that element index from the array, thereby making sure it is not repeated.
I can't tell you the javascript needed for that at the moment, but there are special functions able to handle arrays that way.
I'm still early in my learning of scripting. I've taken the advice of the folks on here and started learning, and I did learn that what I'm trying to do will involve scripting, but it seems to beyond my still limited skill level.

I know I probably need to create an array in a variable and that I need use a splice function(?) - but I have no idea how to make that happen or even where to run the script.
Probably, you'd need a large number like 100 and then do stuff like getting the remainder of 2, 3 and so on and compare it with any specific but low value like 1. There won't be a way to avoid a repetition here and there because true random numbers have never existed in programming, but you'll get as close as it's PC-possible.
I know I probably need to create an array in a variable and that I need use a splice function(?) - but I have no idea how to make that happen or even where to run the script.
That is indeed the simplest way to go about it. You can use a 'script' event command, and type in this:
$gameVariables.setValue(42, [1,2,3,4,5,6])
to set a variable to an array of possibilities - in this example variable 42 is set to an array of 6 numbers, but ofc you can use any variable and any length. You can even add a certain number multiple times if you want it to be more likely.
Then when you want to do the randomisation run another script:
JavaScript:
$gameVariables.setValue(43, $gameVariables.value(42).splice(Math.floor(Math.random()*$gameVariables.value(42).length),1)[0])
This will set the variable 43 to a random element from the array, as well as remove that item from it.

Lastly you can make a conditional branch with a script and check $gameVariables.value(42).length > 0 to see if there are still any elements left in the array. Note that this script will just produce 0 if you try to pick from an already empty array (since the engine will cast undefined to 0).
I hope you dont mind if i paste the Eventcode for a possible solution in here. It might help you structure the Scriptcode,
in case you want to make a plugin instead of Eventing it.

Code:
◆Label:reroll
◆Control Variables:#0021 Var21_Random = Random 1..6
◆If:Var21_Random = 1
  ◆If:Switch11 is OFF
    ◆Control Switches:#0011 Switch11 = ON
    ◆Change Gold:+ 5000
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
◆If:Var21_Random = 2
  ◆If:Switch12 is OFF
    ◆Control Switches:#0012 Switch12 = ON
    ◆Show Animation:Player, Fire All 1
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
◆If:Var21_Random = 3
  ◆If:Switch13 is OFF
    ◆Control Switches:#0013 Switch13 = ON
    ◆Change Items:Potion + 12
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
◆If:Var21_Random = 4
  ◆If:Switch14 is OFF
    ◆Control Switches:#0014 Switch14 = ON
    ◆Show Balloon Icon:Player, Exclamation
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
◆If:Var21_Random = 5
  ◆If:Switch15 is OFF
    ◆Control Switches:#0015 Switch15 = ON
    ◆Text:None, Window, Bottom
    :    :What is this?
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
◆If:Var21_Random = 6
  ◆If:Switch16 is OFF
    ◆Control Switches:#0016 Switch16 = ON
    ◆Text:None, Window, Bottom
    :    :Hello World!
    ◆
  :Else
    ◆Jump to Label:reroll
    ◆
  :End
  ◆
:End
Here's an example for the "splice" approach Andar & Mac suggested:
If you want to draw without replacement, you can use Array#splice to remove selected entries from the array so they aren't there next time, e.g. (source):
Spoiler: event example + script call
◆If:Script:!$gameVariables.value(1).length ◆Comment:No scenes remaining in list, or not initialised yet. ◆Control Variables:#0001 Blessings/Curses = [1,2,3,4,5] ◆ :End ◆Comment:Take a random number from the array and store in variable 2. ◆Script:var scList = $gameVariables.value(1); : :var r = Math.randomInt(scList.length); : :var scId = scList.splice(r, 1)[0]; : :$gameVariables.setValue(2, scId); ◆Text:None, Window, Bottom : :Selected scene \v[2]!
JavaScript:
var scList = $gameVariables.value(1);
var r = Math.randomInt(scList.length);
var scId = scList.splice(r, 1)[0];
$gameVariables.setValue(2, scId);
Technical details:
If you always have six events and you will experience them all, but in a random order, you can also shuffle the array in place and then grab the number with a pop.
Something like

Code:
const array = [1, 2, 3, 4, 5, 6];
for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
    };
$gameVariables.setValue(1, array);

Will set variable 1 to an array of 1 to 6 in a shuffled order. Then you can use

$gameVariables.setValue(2,($gameVariables.value(1).pop()));

each time you need to run an event to remove the last element of the array and store it as variable 2.
That is indeed the simplest way to go about it. You can use a 'script' event command, and type in this:
$gameVariables.setValue(42, [1,2,3,4,5,6])
to set a variable to an array of possibilities - in this example variable 42 is set to an array of 6 numbers, but ofc you can use any variable and any length. You can even add a certain number multiple times if you want it to be more likely.
Then when you want to do the randomisation run another script:
JavaScript:
$gameVariables.setValue(43, $gameVariables.value(42).splice(Math.floor(Math.random()*$gameVariables.value(42).length),1)[0])
This will set the variable 43 to a random element from the array, as well as remove that item from it.

Lastly you can make a conditional branch with a script and check $gameVariables.value(42).length > 0 to see if there are still any elements left in the array. Note that this script will just produce 0 if you try to pick from an already empty array (since the engine will cast undefined to 0).

That worked perfectly! Thank you SO much!!
That is indeed the simplest way to go about it. You can use a 'script' event command, and type in this:
$gameVariables.setValue(42, [1,2,3,4,5,6])
to set a variable to an array of possibilities - in this example variable 42 is set to an array of 6 numbers, but ofc you can use any variable and any length. You can even add a certain number multiple times if you want it to be more likely.
Then when you want to do the randomisation run another script:
JavaScript:
$gameVariables.setValue(43, $gameVariables.value(42).splice(Math.floor(Math.random()*$gameVariables.value(42).length),1)[0])
This will set the variable 43 to a random element from the array, as well as remove that item from it.

Lastly you can make a conditional branch with a script and check $gameVariables.value(42).length > 0 to see if there are still any elements left in the array. Note that this script will just produce 0 if you try to pick from an already empty array (since the engine will cast undefined to 0).

I just wanted to do a quick follow up because while I did get this to work, there is something weird going on that I "solved" but without understanding how I solved it.

Lemme walk through how I have this set up:

When the player enters the map I Autorun an event that calls out this script:

Code:
$gameVariables.setValue(15, [1,2,3,4,5,6]);

(15 is the number of the variable I'm using)

And then this Autorun event turns on a self switch so that it doesn't keep running over and over.

I then have the 6 events placed around the dungeon and each one calls a Common Event called "The 6 Events" when the player touches the event.

In "The Six Events" Common Event I run this script:

Code:
$gameVariables.setValue(16, $gameVariables.value(15).splice(Math.floor(Math.random()*$gameVariables.value(15).length),1)[0]);

(16 is the number of the other variable I'm using)

Below that script in the same Common Event I have all my conditional branches that ask:

If Variable 15 = 1 then display the text "You triggered Event 1!",
If Variable 15 = 2 then display the text "You triggered Event 2!",
If Variable 15 = 3 then display the text "You triggered Event 3!",
If Variable 15 = 4 then display the text "You triggered Event 4!",
If Variable 15 = 5 then display the text "You triggered Event 5!",
If Variable 15 = 6 then display the text "You triggered Event 6!",

(I'm just having it display text right now to ensure it's doing something in the right order - later I'll have it trigger a different event).

Then when the player touches a tile that can trigger one of these 6 event I have it call on the common event "The 6 Events" and then flip a self switch so that it doesn't try to run it over and over.

Everything works great, except ....

The WEIRD thing is that if I only have 6 conditional statements in "The 6 Events" common event the game will ALWAYS skip the very last tile stepped on and not do anything at all. I had to add a 7th conditional statement that says:

If Variable 15 = 7 then display the text "This is an unused event".

That way the game will actually cycle through the other 6 conditional statements while also NEVER picking the 7th conditional statement. And since I don't want the 7th statement to trigger it's all good, but I'm not sure if there will be a situation where it might trigger it?

I have ran this over and over and over again it only works correctly if I include a 7th conditional statement that never gets picked by the random script. The game NEVER picks the 7th statement. But if I delete the 7th statement then the game ALWAYS fails to trigger an event on the 6th and final tile the player touches

Does this have something to do with the way arrays are stored in an index? I know an index starts counting at 0, not 1, so is this somehow related?

I hope this makes sense the way I described it but I'm happy to help with more troubleshooting.
I hope this makes sense the way I described it but I'm happy to help with more troubleshooting.
It makes sense in that I understand what you wrote... but other than that it makes absolutely no sense xD As in I have no idea why it would do that. I'm guessing it has more to do with weird event things that the script, but I'm not sure. I guess at this point I'll ask if you can provide exact screenshots of these events - maybe there will be some clue in those that can help.

I've noticed that you're setting variable 16 to the random value, but wrote If Variable 15 = 1 then display the text "You triggered Event 1!", as your usage - I'm assuming that's just a typo? Since if you were setting variable 16 and checking variable 15 it probably wouldn't work at all.
Does this have something to do with the way arrays are stored in an index?
Very unlikely, since indexes and values have nothing to do with each other here. You could also create an array of [6,7,8,9,10,11] and it would work exactly the same (except returning larger numbers ofc).
I've noticed that you're setting variable 16 to the random value, but wrote If Variable 15 = 1 then display the text "You triggered Event 1!", as your usage - I'm assuming that's just a typo? Since if you were setting variable 16 and checking variable 15 it probably wouldn't work at all.

Whoops, yep, that was a typo.

Here's screenshots of all the events being used:

This first one is of the Autorun event that runs when the player enters the map. It then triggers a self switch so that it stops running.

01.png




This next screenshot is of one of the events that's on the map. It too is tied to a self switch so that once it runs the common event (see third image below) it won't try to run another event.
02.png




And below is the common event all the events on the map are calling from. I had to add that 7th event so that the game would actually cycle through all 6 events. When I delete that 7th conditional it will only cycle through 5 of them. BTW I renamed the variable from earlier today so now it's just called Array Storage, but it's still array 15.

03.png


I also crated a new map with 7 events and had it call a new Common Event (The Seven Events) and sure enough I had to create an 8th entry, otherwise it just wouldn't work.

I'm totally baffled.

But it DOES work, it's just ... weird.
Alright, now I'm definitely suspecting that we might be onto an actual MV bug here. My bet would be on some weird event combining (like how adjacent show message and show choices events combine into one) issue - maybe that additional if is enough to disturb that and make it work normally.

Could you also share that "Did you complete all events" event? I'm guessing it does something when you complete all events (i.e. when the problem occurs), so it might be relevant. After that I'll try to replicate it myself and just unleash all the debugging tools I have on it 😉

And it's probably a good idea to check one more thing: does this problem still happen if you turn off all your plugins?
Just a guess, but i thought Arrays start to count from 0, for example: 0, 1, 2, 3, 4, 5.
So the entry
0 would be 1
1 would be 2
2 would be 3
...
Just a guess, but i thought Arrays start to count from 0, for example: 0, 1, 2, 3, 4, 5.
So the entry
0 would be 1
1 would be 2
2 would be 3
...
As I mentioned earlier, indexes and values have nothing to do with each other here. You could also create an array of [6,7,8,9,10,11] and it would work exactly the same. Besides, the weird thing is that that last if doesn't activate yet still changes things.
The bug has to be on my end. I've now tried to replicate it all morning but it works fine now.

I must have changed something yesterday as I was working on other things in the game because I created a new map and a new common event with the correct number of conditional statements matching the array and I can't get it to fail.

I did, however still go into a deep dive by turning off each plugin one by one and testing everything then turning them all back on one by one and I can't get it to fail either. So I'm sure it's something I was doing, perhaps related to the "Did you complete all events" event. I probably adjusted something with that event and maybe the bug was in there.

Almost certainly since I'm inexperienced it's got to be on my end so I think we can call this solved, but I'll keep an eye out and if the issue crops up again I'll refer back to this thread.

And I'm gonna spend the rest of the day seeing if I can replicate that bug because it was really interesting how adding an additional conditional statement had an effect on the array somehow.
I must have changed something yesterday as I was working on other things in the game because I created a new map and a new common event with the correct number of conditional statements matching the array and I can't get it to fail.
Yes, I thought this would be it. You probably made some breaking change(s) at the same time as adding the extra branch, but forgot those other changes. No worries, it's a very common mistake!

It would be very unusual for an extra branch to have that kind of effect, plugins or no.
Ah, it's a shame it ended up being just that. Sounded like it would be a really weird and interesting mystery to solve 😉
But I'm glad you managed to get everything working 🙂
Ah, it's a shame it ended up being just that. Sounded like it would be a really weird and interesting mystery to solve 😉
But I'm glad you managed to get everything working 🙂

I tracked it down.

And it was me like we suspected. What was happeneing was I wasn't clearing
Hmm, that was weird. Sorry about the cut off post.

But I did track it down. I was not clearing out one of the variables in the array and so I kept starting a new map with one already taken out of the array. Thus the game could only cycle through the 5 remaining of the 6 total. I figured it out when I ran the debugger and checked all my variables when I went back and replicated older code.

So good advice for me is to always check that debugger lol.

Thanks, everyone!

And I hope this thread helps others who want to do something similar with arrays and variables. There's a lot of potential using these with a wide range of possibilities that are easy to implement.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-trigger-randomized-non-repeating-events.154578/