Game Development Reference
In-Depth Information
The script works by moving through the string one character at a time looking for
spaces (because spaces mark the end of words). The index variable is used to
keep track of the current character position within the string, and the count
variable is used to keep track of how many words have been counted so far. Both
of these are initialized to 0 in lines 7 and 8 and the complete length of the string is
also calculated in line 10.
A while loop is set up in line 12 that will repeat the subsequent block of code
as long as the number of words counted is less than the target number, and index
is less than the total length of the string. This means it will stop when it has
counted the required number of words, or it has reached the end of the string.
Line 14 increases the index variable one character at a time and line 16 uses the
string_char_at function to check if this character is a space. If it is, then the word
count is increased as well.
By the time the function reaches line 20, count will either be equal to words
(we've found the end of the required number of words) or index will be equal to
length (we've reached the end of the string). Either way, index is returned to
provide the number of characters at the point the script reached within the
string.
The Speaking Object
Our next task is to implement the obj_text object, which will need to keep track of such things as
the text string and the number of words that are currently being displayed from that string. It will
also need to draw the text and maintain finished , timeout , and selected variables so that other
objects can tell when it has finished displaying the current string of text, or when the mouse
pointer is over the text.
Implementing the obj_text_speak Object
1. Edit the Create event of obj_text and replace it with the following code:
1: {
2: text_width = 760;
3: word_delay = 3;
4: mouse_icon = true;
5:
6: text = "";
7: speak = "";
8: select = "";
9: text_previous = "";
10: displayed_words = 0;
11: displayed_index = 0;
12: displayed_text = "";
13:
14: finished = false;
15: timeout = false;
16: selected = false;
17: selectable = false;
18:
19: alarm[0] = word_delay;
20: }
 
Search WWH ::




Custom Search