Random Menus, using item values and indirect jumps
By Hal MacLean
Download the project files here.

Scripting Random Menus
From time to time it is nice to be able to offer your user a different menu, but occasionally you want to send them to a menu at random. This is actually very easy to do, but why would you?
Well, you could provide a series of identical menus that change the background graphic, or you can use menus as quiz items with questions on them. Making a quiz is not the topic for this tutorial, and will be covered in depth elsewhere. For now, we will explore the ways to make random menus appear. How you use this is up to you, but the point is about learning the scripting involved.
It is important to remember that every item in your DVD Studio Pro project has a value attached to it that is used by your DVD player. For example, the first track that you have in your list has a value of 49,280. This means that if ever you want to go to that track, regardless of the name for it, you would simply have to write a line into a script that reads ‘Jump 49,280′. However, no such command exists, so we have to work around that limitation. More information about item values is here.
Jump Indirect
When you use an indirect jump, what you are doing is putting a value into a GPRM, then jumping to whatever item it is that has that value. So instead of the inadequate line above, you would script this as:
mov GPRM0, 49280 Jump Indirect, GPRM0
Knowing the item values is crucial to simplifying the scripts that you write. There are only three things to think about – menus, tracks and scripts. Everything else is a variant on these. Stories and slideshows, for example, are actually counted as tracks, and in terms of the item value, behave according to the rules for tracks.
Menus have an item value of 32, and increment in jumps of 32. This means the second menu you create will have a value of 64, the third will be 96, and so on. If you want to go to menu 3 you would need to ‘jump indirect’ to a value of ’96′. This really simplifies our code.
In the case of this tutorial, the attached file includes five menus with different backgrounds, and just three sample buttons. The ‘Main Menu’ button points to a script, the ‘Chapter Selection’ button points back to the example menu in each case, and the ‘Bonus Features’ button points to the track. Obviously, your system will be slightly more intricate, but as long as you are using item values you should have no problems getting to a randomly selected menu.
The only scripting needed is this:
ran GPRM0, 5 mul GPRM0, 32 Jump Indirect, GPRM0
When this is examined closely, it reads as “Make a random number from one to five, then multiply it by 32. Then jump to the item in the disc which has that value”. Therefore, if the random number is ’4′, it will be multiplied by 32 to become ’128′ and this will equate to menu 4 in your disc.
The alternative would be to write this out longhand, using menu names and so on. It will work this way, but you’ll increase the size of the script, restrict yourself to only those menu names, and add to the time it takes to create the project. For those of you who prefer, have a look at this:
ran GPRM0, 5 Jump Menu 1 if (GPRM0 = 1) Jump Menu 2 if (GPRM0 = 2) Jump Menu 3 if (GPRM0 = 3) Jump Menu 4 if (GPRM0 = 4) Jump Menu 5 if (GPRM0 = 5)
Not only is it tedious to do, it is not necessary in the first place! However, it will work just the same and if your disc is simple, or if you prefer to ‘see’ the logic in the code, then perhaps this is a better method for you.
Non Repeating Random Selection
To make sure that nothing repeats you have to create a new set of rules. This would be ideal for a quiz where the menu with the question on can only appear once in the game. The way this is done is to track which numbers have been randomly generated and if they come up again, go back a line in the script to create a new random number. There are several things to consider here – firstly, with a simple system such as the one in the tutorial files, it is probably overkill to do this. Secondly, you will introduce a pause of increasing length as you move through the menus, and narrow down the number of options that could be drawn at random. The more menus you use, the worse this gets, and some additional logic is needed to resolve the possibility of waiting a long time for the number of the only remaining menu to be ‘drawn’ from the generator.
Tracking all of the choices is beyond the scope of this tutorial, but is a feature of quiz making, and is included in the non-repeat quiz tutorial and sample files.
It doesn’t seem very random
The very nature of a random number generator is that you could conceivably have the same number chosen repeatedly. It is possible to prevent this and force a different menu on each selection by keeping track of the last number drawn and if the same arises, going back to draw another, or simply adding or subtracting ’1′ from it so that a different menu then appears. You only track the value of the menu showing (use the ‘Current Item’ or ‘Last Item’ operands in the ‘Special’ set of commands when you set the GPRM) and make sure that value doesn’t reappear in the next choice.
One way to do this would be to re-write your script as follows, using a second GPRM:
mov GPRM0, GPRM1 ran GPRM0, 5 mul GPRM0, 32 goto 2 If (GPRM0 = GPRM1) Jump Indirect, GPRM0
What this is doing depends on whether this is the first time the script has run or not. If it is the first time, GPRM0 will be empty, and so GPRM1 will get a value of ’0′ moved into it. You then generate the random number and multiply it by 32, but before you jump, you check to see if the new value is the same as that in GPRM1. On the first run it won’t be, so the script ignores line 4 and the jump goes ahead. We don’t ever clear GPRM0, so when you go back to the menu the previous value still exists in GPRM0. This gets copied into GPRM1, then a new value is generated. On this occasion it is entirely possible that the new value is the same as the old, so the fourth line prevents the jump from happening if the values are the same. Instead, a loop back to the random number generator gets a new value drawn. It continues this loop until the two GPRMs hold different values. When they do, the jump takes place.
This ensures no two consecutive menus are the same, but doesn’t prevent the same menu showing more than once if the menus are cycled repeatedly. It has the great advantage of not introducing a long pause as the script runs, too!
Conclusion
Random menus is a nice feature for certain types of disc, and add an opportunity to reveal different pieces of information to your viewers in different ways. You might have decided to put a button to an easter egg (hidden content) on one menu, and have ten other identical menus. When the viewer notices the new button they are rewarded with the hidden content! You could add a ‘time out’ to the ‘special’ menu and make it harder for the viewer to get to the content if you wish. Combining these sorts of ideas into your projects helps keep the viewing experience that little bit special. It takes time and effort, and a little thinking about, but is well within the capabilities of any DVD Studio Pro author.
Tutorial Files
Download the project files here.
This project was created by Hal MacLean, using images freely available from the Internet. The images are of Disney characters, and credit must be given to Disney for them.
