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
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.
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