Open main menu

Difference between revisions of "AGIWiki/Message"

135 bytes added ,  15:06, 25 October 2018
m
Text replacement - "</source>" to "</syntaxhighlight>"
m (Text replacement - "<source lang=" to "<syntaxhighlight lang=")
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
Line 4: Line 4:
<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
#message 2 "He's not here."
#message 2 "He's not here."
</source>
</syntaxhighlight>
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">
<syntaxhighlight lang="cpp">
print("He's not here.");
print("He's not here.");
</source>
</syntaxhighlight>
When a message is implicitly defined in this manner, the compiler will essentially add a <code>#message</code> command to the logic, but it will be invisible. Although possible, it is not generally practical to find out which message number is assigned to the message. Furthermore, additional implicit assignments in the logic file can change which message number is assigned to it. If you need to know the number of a message for sure, use an explicit message definition.
When a message is implicitly defined in this manner, the compiler will essentially add a <code>#message</code> command to the logic, but it will be invisible. Although possible, it is not generally practical to find out which message number is assigned to the message. Furthermore, additional implicit assignments in the logic file can change which message number is assigned to it. If you need to know the number of a message for sure, use an explicit message definition.


Line 15: Line 15:
print("He's not here.");
print("He's not here.");
print(m2);
print(m2);
</source>
</syntaxhighlight>
As you might expect, there are advantages and disadvantages to both methods of defining messages.
As you might expect, there are advantages and disadvantages to both methods of defining messages.


Line 33: Line 33:


print("%m2 But he'll be back soon.");
print("%m2 But he'll be back soon.");
</source>
</syntaxhighlight>
The message is stored like this in the logic resource, but when the [[AGIWiki/Interpreter|interpreter]] displays it, it will replace <code>%m2</code> with message 2, so the text "He’s not here. But he’ll be back soon." will be displayed. See [[AGIWiki/Message#Building messages|Building messages]] for more details.
The message is stored like this in the logic resource, but when the [[AGIWiki/Interpreter|interpreter]] displays it, it will replace <code>%m2</code> with message 2, so the text "He’s not here. But he’ll be back soon." will be displayed. See [[AGIWiki/Message#Building messages|Building messages]] for more details.


Line 55: Line 55:
     }
     }
}
}
</source>
</syntaxhighlight>
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">
<syntaxhighlight lang="cpp">
Line 75: Line 75:


#message 1 "You're not close enough."
#message 1 "You're not close enough."
</source>
</syntaxhighlight>


Or, using [[AGIWiki/Defines|defines]]:
Or, using [[AGIWiki/Defines|defines]]:
Line 98: Line 98:


#message 1 "You're not close enough."
#message 1 "You're not close enough."
</source>
</syntaxhighlight>


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.
Line 117: Line 117:
     }
     }
}
}
</source>
</syntaxhighlight>


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!
Line 138: Line 138:
     }
     }
}
}
</source>
</syntaxhighlight>
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">
<syntaxhighlight lang="cpp">
Line 160: Line 160:


#message 1 "You're too far away." // changed
#message 1 "You're too far away." // changed
</source>
</syntaxhighlight>


== Building messages ==
== Building messages ==
Line 194: Line 194:
v255 = 20;
v255 = 20;
print("The value of v255 is %v255.");
print("The value of v255 is %v255.");
</source>
</syntaxhighlight>
This code will print the message "The value of v255 is 20."
This code will print the message "The value of v255 is 20."


Line 225: Line 225:
   }
   }
}
}
</source>
</syntaxhighlight>


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:
Line 235: Line 235:
#message 14 "\"%w5\" is not in my vocabulary."
#message 14 "\"%w5\" is not in my vocabulary."
#message 15 "What is \"%w6\""
#message 15 "What is \"%w6\""
</source>
</syntaxhighlight>
'''Note:''' the message numbers were chosen arbitrarily and are not necessarily the message numbers that will be generated by the compiler.
'''Note:''' the message numbers were chosen arbitrarily and are not necessarily the message numbers that will be generated by the compiler.


Line 272: Line 272:
#message 11 "\" is not in my vocabulary."
#message 11 "\" is not in my vocabulary."
#message 12 "What is \""
#message 12 "What is \""
</source>
</syntaxhighlight>


'''Note:''' this code uses <code>%g</code><var>N</var> because the messages are defined in logic 0.
'''Note:''' this code uses <code>%g</code><var>N</var> because the messages are defined in logic 0.
Line 287: Line 287:
#message 17 "\"%w5%g11"
#message 17 "\"%w5%g11"
#message 18 "%g12%w6\""
#message 18 "%g12%w6\""
</source>
</syntaxhighlight>
The next obvious question is how much memory this actually saves. Because the message numbers are chosen at random and do not necessarily reflect what the real message numbers would be, this figure is not necessarily exact (you will have to give or take 6 bytes):
The next obvious question is how much memory this actually saves. Because the message numbers are chosen at random and do not necessarily reflect what the real message numbers would be, this figure is not necessarily exact (you will have to give or take 6 bytes):


TrustedUser
2,147

edits