TrustedUser
2,147
edits
m (syntax -> source) |
m (Text replacement - "<source lang=" to "<syntaxhighlight lang=") |
||
Line 2: | Line 2: | ||
A '''message''' is a string of text that is used by commands like [[AGIWiki/Print|print]] to give the user information. Each [[AGIWiki/Logic|logic resource]] can contain up to 255 messages, with message numbers starting from 1 and going to 255. A message can also be as long as you like; however, there are practical limitations to this. See the [[AGIWiki/Message too verbose error|message too verbose error]]. Messages can be defined implicitly or explicitly. An explicit message definition uses the <code>#message</code> command: | A '''message''' is a string of text that is used by commands like [[AGIWiki/Print|print]] to give the user information. Each [[AGIWiki/Logic|logic resource]] can contain up to 255 messages, with message numbers starting from 1 and going to 255. A message can also be as long as you like; however, there are practical limitations to this. See the [[AGIWiki/Message too verbose error|message too verbose error]]. Messages can be defined implicitly or explicitly. An explicit message definition uses the <code>#message</code> command: | ||
< | <syntaxhighlight lang="cpp"> | ||
#message 2 "He's not here." | #message 2 "He's not here." | ||
</source> | </source> | ||
This sets the text of message 2, or <code>m2</code>, to "He's not here." Messages are implicitly defined when they do not appear in any <code>#message</code> command but do appear within a print command: | This sets the text of message 2, or <code>m2</code>, to "He's not here." Messages are implicitly defined when they do not appear in any <code>#message</code> command but do appear within a print command: | ||
< | <syntaxhighlight lang="cpp"> | ||
print("He's not here."); | print("He's not here."); | ||
</source> | </source> | ||
Line 12: | Line 12: | ||
If you use an explicit message definition such as the one shown previously, you can print the message by either duplicating the message in full or specifying the message number: | If you use an explicit message definition such as the one shown previously, you can print the message by either duplicating the message in full or specifying the message number: | ||
< | <syntaxhighlight lang="cpp"> | ||
print("He's not here."); | print("He's not here."); | ||
print(m2); | print(m2); | ||
Line 29: | Line 29: | ||
You can refer to one message from within another by using message referencing. This is done by including %m and then the message number within the other message. For example: | You can refer to one message from within another by using message referencing. This is done by including %m and then the message number within the other message. For example: | ||
< | <syntaxhighlight lang="cpp"> | ||
#message 2 "He's not here." | #message 2 "He's not here." | ||
Line 39: | Line 39: | ||
For an example where explicitly defining a message can avoid code duplication, let's take a simple "You're not close enough." message that occurs commonly in [[AGIWiki/AGI|AGI]] games. | For an example where explicitly defining a message can avoid code duplication, let's take a simple "You're not close enough." message that occurs commonly in [[AGIWiki/AGI|AGI]] games. | ||
< | <syntaxhighlight lang="cpp"> | ||
if (said("get", "cat")) | if (said("get", "cat")) | ||
{ | { | ||
Line 57: | Line 57: | ||
</source> | </source> | ||
Obviously here we've got some duplicate code: print("You're not close enough.");. At first glance, using explicit messages can provide a small advantage: | Obviously here we've got some duplicate code: print("You're not close enough.");. At first glance, using explicit messages can provide a small advantage: | ||
< | <syntaxhighlight lang="cpp"> | ||
if (said("get", "cat")) | if (said("get", "cat")) | ||
{ | { | ||
Line 78: | Line 78: | ||
Or, using [[AGIWiki/Defines|defines]]: | Or, using [[AGIWiki/Defines|defines]]: | ||
< | <syntaxhighlight lang="cpp"> | ||
#define NotCloseEnough m1 | #define NotCloseEnough m1 | ||
Line 101: | Line 101: | ||
Obviously, either example saves a few keystrokes, but it also provides another advantage. Suppose you want to change your "not close enough" message to read "You're too far away." In the original code, where the message is implicitly defined, you will have to search and replace in your code for every place where the text "You're not close enough." appears. | Obviously, either example saves a few keystrokes, but it also provides another advantage. Suppose you want to change your "not close enough" message to read "You're too far away." In the original code, where the message is implicitly defined, you will have to search and replace in your code for every place where the text "You're not close enough." appears. | ||
< | <syntaxhighlight lang="cpp"> | ||
if (said("get", "cat")) | if (said("get", "cat")) | ||
{ | { | ||
Line 120: | Line 120: | ||
The danger that is not necessarily obvious here is that if you miss one or make a typo, you will accidently create a new implicitly-defined message! | The danger that is not necessarily obvious here is that if you miss one or make a typo, you will accidently create a new implicitly-defined message! | ||
< | <syntaxhighlight lang="cpp"> | ||
if (said("get", "cat")) | if (said("get", "cat")) | ||
{ | { | ||
Line 140: | Line 140: | ||
</source> | </source> | ||
Using explicit message definitions allows the change to be made in just one place: the message definition. | Using explicit message definitions allows the change to be made in just one place: the message definition. | ||
< | <syntaxhighlight lang="cpp"> | ||
#define NotCloseEnough m1 | #define NotCloseEnough m1 | ||
Line 191: | Line 191: | ||
=== Example === | === Example === | ||
< | <syntaxhighlight lang="cpp"> | ||
v255 = 20; | v255 = 20; | ||
print("The value of v255 is %v255."); | print("The value of v255 is %v255."); | ||
Line 202: | Line 202: | ||
Take the following code from logic 0 of the [[AGIWiki/AGI Studio Template Game|AGI Studio Template Game]], which informs the player that he or she used a word that the game doesn't understand: | Take the following code from logic 0 of the [[AGIWiki/AGI Studio Template Game|AGI Studio Template Game]], which informs the player that he or she used a word that the game doesn't understand: | ||
< | <syntaxhighlight lang="cpp"> | ||
if (input_recieved && | if (input_recieved && | ||
unknown_word_no > 0) { | unknown_word_no > 0) { | ||
Line 228: | Line 228: | ||
This code is in fact wasting a few dozen bytes of memory. Although this does not seem like much, in a programming environment like AGI where memory is a precious resource, these bytes can make a difference. When the compiler sees this code, it will generate invisible message definitions similar to the following: | This code is in fact wasting a few dozen bytes of memory. Although this does not seem like much, in a programming environment like AGI where memory is a precious resource, these bytes can make a difference. When the compiler sees this code, it will generate invisible message definitions similar to the following: | ||
< | <syntaxhighlight lang="cpp"> | ||
#message 10 "I don't understand \"%w1\"" | #message 10 "I don't understand \"%w1\"" | ||
#message 11 "\"%w2\" is not in my vocabulary." | #message 11 "\"%w2\" is not in my vocabulary." | ||
Line 245: | Line 245: | ||
Each of these phrases appears in two of the messages above. Using explicit message definitions we can reduce the amount of memory used, as shown below: | Each of these phrases appears in two of the messages above. Using explicit message definitions we can reduce the amount of memory used, as shown below: | ||
< | <syntaxhighlight lang="cpp"> | ||
if (input_recieved && | if (input_recieved && | ||
unknown_word_no > 0) { | unknown_word_no > 0) { | ||
Line 277: | Line 277: | ||
This code does the exact same thing as the earlier code, but it uses less memory. The obvious disadvantage here is that the code is a little less readable. Unfortunately, this trade-off is somtimes necessary. You can use [[AGIWiki/Defines|defines]] to overcome it if you like. The advantage is that the compiler will now use something similar to the following message definitions: | This code does the exact same thing as the earlier code, but it uses less memory. The obvious disadvantage here is that the code is a little less readable. Unfortunately, this trade-off is somtimes necessary. You can use [[AGIWiki/Defines|defines]] to overcome it if you like. The advantage is that the compiler will now use something similar to the following message definitions: | ||
< | <syntaxhighlight lang="cpp"> | ||
#message 10 "I don't understand \"" | #message 10 "I don't understand \"" | ||
#message 11 "\" is not in my vocabulary." | #message 11 "\" is not in my vocabulary." |