Difference between revisions of "MessageBox Tutorial"

Jump to navigation Jump to search
808 bytes added ,  14:03, 25 August 2007
→‎Basics: Rewrite
imported>Haama
(→‎Intro: Rewrite)
imported>Haama
(→‎Basics: Rewrite)
Line 5: Line 5:
Most MessageBox mistakes stem from adapting a script that works in some cases, but not in more complex situations. To prevent this, this tutorial will show you how to make an all-purpose script that can be used and expanded for any situation. We'll start out with some of the basic mechanics of the MessageBox and related functions, followed by common mistakes in complex scripts, and then the all-purpose script and how to set it up. Finally, we'll go through how to use the script to easily move between multi-layered menus, and some extras that you can tack on to it.
Most MessageBox mistakes stem from adapting a script that works in some cases, but not in more complex situations. To prevent this, this tutorial will show you how to make an all-purpose script that can be used and expanded for any situation. We'll start out with some of the basic mechanics of the MessageBox and related functions, followed by common mistakes in complex scripts, and then the all-purpose script and how to set it up. Finally, we'll go through how to use the script to easily move between multi-layered menus, and some extras that you can tack on to it.


==Basics==
==Basic Mechanics of MessageBox and Related Functions==
First, some basic information on menus:
Every menu requires two functions: [[MessageBox]] to display the menu, and [[GetButtonPressed]] to return which button the player pressed. Follow the links for more details, but here are the important things to remember:


===GetButtonPressed===
#GetButtonPressed returns numbers from –1 to 9:
*'''''-1''''' means no decision has been made
*'''''0''''' means the player selected the first option
*'''''1''''' for the second
*...
*'''''9''''' for the tenth(you can have 10 options at most)
#GetButtonPressed will only return the correct button press the first time it's called in a script; any other use of GetButtonPressed will return -1. For instance, if the player presses the first button, then in the following script
<pre>if (GetButtonPressed == -1)
...
elseif (GetButtonPressed == 0)</pre>
GetButtonPressed will return 0 the first time, move on to the 'elseif' test, and return –1 the second time.


There are two sides to every menu – the display of the menu and catching the player's decision. You display the menu with the function [[MessageBox]] and catch the player's decision with the function [[GetButtonPressed]]. You can keep these two separated by changing a variable (i.e., "Choosing")
To take care of this, set a variable to GetButtonPressed, and test the variable instead, as such:
<pre>short Choice
...
set Choice to GetButtonPressed
if (Choice == -1)
...
elseif (Choice == 0)</pre>


===Where to place MessageBox and GetButtonPressed===
===Where to place MessageBox and GetButtonPressed===
[[MessageBox]] takes one frame to display, so you can use any [[:Category:Blocktypes|block]] to display it. However, [[GetButtonPressed]] needs to be in a [[:Category:Blocktypes|block]] that continuously runs (i.e., [[GameMode]], [[MenuMode]], [[ScriptEffectUpdate]]) and needs to be on a script that is running every frame (i.e., an activator that is in a loaded cell, a quest running every .001 seconds, etc.), until the player's decision is caught. This is because it will take at least a few frames for the player to read the menu and make a decision (as well as some other peculiarities).
MessageBox takes one frame to display, so you can use any [[:Category:Blocktypes|block]] to display it. However, GetButtonPressed needs to be in a block that continuously runs (i.e., [[GameMode]], [[MenuMode]], [[ScriptEffectUpdate]]) and needs to be on a script that is running every frame (i.e., an activator that is in a loaded cell, a quest running every .001 seconds, etc.), until it returns the player's button press. This is necessary as it will take more than a frame for the player to read the menu and make a decision (as well as some other peculiarities).


===Keep 'em separated===
===Keep 'em separated===
The menu will always be displayed first, followed by catching the player's decision. These are distinct parts of the script, so they will have to be kept separate. To do this, use a variable ("Choosing" will be used in this tutorial) and set it to a different number for each part. For instance, for displaying the menu you can use '''''set Choosing to -1''''' to start the menu and '''''if (Choosing == -1)''''' to make sure the menu needs to be displayed. For catching the player's decision, you can use '''''set Choosing to 1''''' and '''''if (Choosing == 1)'''''.
Putting  it all together, so far we end up with a script like this:
<pre>short Choice
...
messagebox "Your menu" "Button 0" ... "Button 9"
set Choice to GetButtonPressed
if (Choice == -1)
...
elseif (Choice == 0)
...</pre>
The problem with this script – every time it repeats while waiting for GetButtonPressed to return the player's button press, the MessageBox will be displayed again. To prevent this, you need to use a variable to keep them separated, as such:
<pre>short Choosing
short Choice
...
if (Choosing == -1)
  messagebox "Your menu" "Button 0" ... "Button 9"
  set Choosing to 1
  set Choice to GetButtonPressed
elseif (Choosing == 1)
  set Choice to GetButtonPressed
  if (Choice == -1)
  ...
  elseif (Choice == 0)
  ...</pre>
Note that both halves are required for each menu. To help keep things organized, you can set one to a negative number, and the other to a positive number, as in the above script.


===GetButtonPressed returns numbers from -1 to 9===
Another oddity - [[GetButtonPressed]] returns somewhat odd numbers for each decision:
*'''''-1''''' means no decision has been made
*'''''0''''' means the player selected the first option
*'''''1''''' for the second
*...
*'''''9''''' for the tenth(you can have 10 options at most)


===(Non-working, but insightful) Example Script for the above concepts===
===(Non-working, but insightful) Example Script for the above concepts===
Anonymous user

Navigation menu