RANDOM THUNDER GENERATOR

This tutorial guides users through the process of creating a system to randomly generate thunder.

My first-ever RPG included a brief trek through a dense forest in the middle of a heavy rainstorm. Looking back on it now, the simplicity of the programming makes me laugh: I had a very simple event set to flash the screen and play a rumbling thunder-like sound every thirty seconds on the dot.

But, of course, that's not how thunderstorms work. Lightning doesn't strike on a fixed ration schedule in real life; it's random and unpredictable. This tutorial will guide you through the process of creating a system to randomly generate lightning/thunder effects for those situations where your character finds himself mired in a storm. To begin, you must...

1. Create an event.

Put it somewhere out of the way. As usual, set it to parallel process so it's always running and doesn't crowd out control of the character on the screen.


2. Create a Thunder variable and a Thunder switch.

In the Event Commands window, first select Variable Operations. Create a variable (we'll call it Thunder). Then select Switch Operations and create a switch (which we'll also call Thunder).


3. Start plugging in code on page 1 of the event.

First thing's first: put in a 10-second wait (that's 100 milliseconds!). This is to ensure that, even if the random generator randomly triggers the lightning/thunder effect every single time, there will still be no more than one lightning strike every 10 seconds.

Next, go to Variable Operations. The variable to change is our Thunder variable. Under Operation, select "Set to." Under Operand, select "Random Number between" and then enter 1 in the first window next to it and 10 in the second window.

Finally, after the variable operations, go to Switch Operations and have it turn on our Thunder switch.

EXPLANATION: The first page of the event sets up the conditions under which it is determined whether or not lightning will strike. (The lightning/thunder effect itself is then executed on the second page). After a 10-second pause, a random number is generated between 1 and 10. Then a switch is activated that kicks the event over to the second page, where the lighting effect is (or isn't) executed. This is done in order to keep the event from constantly generating a new random number before it has the opportunity to generate the lightning effect.


4. Start plugging in code on page 2 of the event.

Create a second page in the event with a precondition: the Thunder switch must be on.

Create a branch condition. The condition in question is Variable (select Thunder here, of course) with a Number equal to X (for a description of X, read on). In the drop-down window, select "Greater than or equal to." Recall that, on page 1, we created a Variable Operations line that randomly generates a number between 1 and 10. This branch condition says that if the randomly-generated number is greater than or equal to X, a lightning strike will occur. In this case, if you set X to 6, there is a 50% chance of a lightning strike occurring. (I've found an X of 7 or 8 works just fine for run-of-the-mill storms, though if you're dealing with particularly nasty weather, a 4 or 5 might be more desirable).

Do not check the box that reads "Execute Custom Handler if Condition Not Met." This is because, if the condition is not met, nothing happens -- i.e., no lightning strikes.

Next, you need to populate the branch condition field with the effects of what will happen if the randomly-generated variable meets or exceeds your chosen X. A simple lightning/thunder effect might be as follows: in the Event Commands window, select Flash Screen on page 2, then Flash Screen Once (do not halt all processes while it's doing this) for maybe half a second. Have a 0.5 second wait (or longer, depending on how close and intense you want the storm to be) and play the appropriate thunder sound. If you're using basic RTP sounds, have it play bolt09, with the tempo turned all the way down; this provides the kind of deep, chattering boom you might expect to hear after lightning strikes.

Finally, at the end of the branch condition, have it wait a few more seconds and then turn our Thunder switch off.

EXPLANATION: In essence, this page of the event takes the randomly-generated number from page 1 and compares it against the threshold you set. If the number from page 1 meets or exceeds that threshold, it triggers the lightning strike. If not, it doesn't. The event then turns off the Thunder switch, kicking it back to page 1 and starting the whole process all over again.

---

Testing this in practice (with an X of 7), I've found no real problems with this system, although I've been trying it "naive," i.e., without a large number of possibly conflicting events. With an X of 7, the system yields a lightning strike on average every 20 to 45 seconds -- though remember, the system is random, so some strikes will occur more closely together than others.

Just like it should in real life.

The code as it appears in my Random Thunder Generator event reads as follows:

PAGE ONE
Wait: 10.0 sec
Variable Oper: Set, Rnd
Switch Operation: ON

PAGE TWO (precondition: Switch is ON)
Branch if Var is 7 or more
Flash Screen: (R31, G31, B31, V31), 0.5 sec
Wait 0.5 sec
Play Sound: bolt09

: End
Wait: 3.0 sec
Switch Operation: OFF