Scripting Random Encounters and Events

by solostrike

<< All Articles | Print

Overview and Prerequisites

This article discusses the concept of random encounters and events using the scripting feature of the site. Random encounters/events spice up the average storygame and can add a little excitement for the player. It is a good idea to read Basic Scripting article, the Random Variable Discussion article and the Intermediate Scripting article before reading this article. Also, reading the Page Recall concept in the Advanced Scripting article is recommended.

Basic Random Encounters/Events

Random Encounters/Events require the use of random variables which is why it is a good idea to read the Random Variable article. The following is a simple example of a random encounter using only one variable:

%RAND := 1D6 # using a six sided dice to gain a random variable
IF %RAND := 3 THEN # if the random variable is 3, 3 being the random encounter
%LASTPAGEID := $PAGEID # then remember the current page for future use
$DEST := @P30 # the destination is altered to go to the random encounter page, in this case P30

When the random encounter/event is finished and you want to return to the game, put this into the random encounters link script to "Go Back". This concept is discussed in great detail in the Advanced Scripting article, and therefore, I will not discuss its function here.


To outline the entire code, basically if a 3 turns up in the random calculation, the user is taken to a page where you have set up the random encounter. After the random encounter is complete, the user is taken back to where they were and they can continue the game.

Advanced Random Encounters/Events

This concept builds on the previous one with a few added random event. With a random encounter/event, you may not want the user to almost always stumble upon a random event, so you alter the odds. Instead of having your random variable base the event on a six sided dice, use 2 dice with 50 sides each for a maximum of a value of 100 to be calculated. This leaves room for many numbers where only a select few lead to a random event. For example, if you had 2D50, the numbers that cause a random event could be 3, 11, 25, 31, 45, 57, 64, 72, 81, and 98. Now there are 10 numbers that are scattered throughout the possibilities, instead of having the random encounter with numbers 1-10, you have them scattered thus making it less possible for a random event but still keeping it at a chance.

Also, you could include other factors that affect a random event. For a simple example, you are in a forest and a factor that decreases your odds of encountering bugs is a bug repellent. The example code defines the forest as the second chapter of your game and this is, therefore, placed in the global link script because, although it is in the global script, the first line of the code limits it to the forest chapter.

%RAND := 2D50
IF %RAND > 0 AND < 10 THEN
$DEST := @C3
$DEST := @C3

In the above script, the bug repellent is represented by an item with the ID of 1, if the user does not have the bug repellent, his chances of an encounter in theory is 10%, however, if he does have the bug repellent, then his chances of the encounter in theory is 5%. The $DEST are just placeholders for where the user is taken in the encounter.


This article's objective is to assist members in designing a random encounter script and using it effectively in their storygames. At times, only a basic script is needed, and at others, a more advanced script is required. An advanced script requires more effort but provides the player of your game with a more interactive experience.