Combining Random Text Strings
In this lesson we will see how to write a custom function that combines several literal strings with random words chosen from three lists to create an insult.
Note: The words list for this insult generator has been floating around the Internet for years. The original source is unknown.
Our custom function
We are going to write the function
shakespeareanInsult()
The function chooses three words, two adjectives and a noun, from three lists of words. The any keyword selects a random word from each list.
Our example stack
For this lesson we need a stack containing 3 fields, holding our lists of words and a button to generate an insult.
The any keyword
The shakespeareanInsult() function
We will place the shakespeareanInsult()
function on the card script, this gives the script direct access to the 3 fields as they are controls on the card.
To generate the insult we combine the string "Begone, thou" with a randomly chosen word from each string using the && operator. The && operator combines 2 strings and inserts a space between them.
function shakespeareanInsult
return "Begone, thou" \
&& any word of field "adjectives" \
&& any word of field "compound adjectives" \
&& any word of field "nouns" & "!"
end shakespeareanInsult
Calling the function
Now all we need to do is call the function from out button and use the answer command to display the result in an answer dialog.
on mouseUp
answer shakespeareanInsult()
end mouseUp
Using custom properties instead of fields
If you wanted to include this function in a stack and you didn't want to use fields to store the lists of words you could use custom properties instead. If the custom properties belong to the card the function would be
function shakespeareanInsult
return "Begone, thou" \
&& any word of the cAdjectives of me \
&& any word of the cCompoundAdjectives of me\
&& any word of the cNouns of me & "!"
end shakespeareanInsult
We can use the keyword me as it refers to the object whose script contains the handler that is executing. In this case, me refers to the card.
0 Comments
Add your comment