Difference between revisions of "MessageBox Tutorial"
→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. | ||
== | ==Basic Mechanics of MessageBox and Related Functions== | ||
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. | |||
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 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 | 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. | |||
===(Non-working, but insightful) Example Script for the above concepts=== | ===(Non-working, but insightful) Example Script for the above concepts=== |