Literal Tutorial

In time, you will learn to program your game to accept all kinds of data from many different sources (a MMOG server, perhaps). Literals are the first step in understanding how games acquire and use data.

Literal Tutorial

Data is the heart and soul of a game. [[Function]]s are the how, data is the what. To put it another way : functions are verbs, data is nouns. The literal is the most basic kind of data. Literals are put directly into the game code by the game programmer. You may have not known it at the time, but you have already been introduced to a literal in a previous tutorial. Let's review the Hello World code.

print "Hello World"
sleep
end

We know that three things in this code are functions : [[doc:print]], sleep, and end. This only leaves "Hello World", our mystery literal.

"Hello World" is what print displays on the screen. Since what is between the quotes is literally the characters that are written, it is a called a literal.

Two Types of Literals

There are two primary types of literals as there are two primary types of data : Numbers and strings. It's easy to understand the difference.

String Literals

As above, string literals have quotes around them: "string literal". String literals are used for things that require text. When you display a message for the player, such as "You see a dragon, you are F*ed.", it's a string. Also, character's names are strings : "Bob, the Shortest Hobbit".

print "You see a dragon, you are F*ed"
print "Bob, the shortest hobbit."
print "Hello, I am Joe 6502."

Strings can contain just about any character, including numbers.

Numeric Literals

A numeric literal is, well, numeric. Numeric literals are used for doing math with. For example, a character's hit points or remaining lives. Numeric literals cannot contain letters.

print 42
print 30234
print 300
print 3.5

Conclusion

You now know about the most basic kind of data your game can have : literals. In later tutorials, you'll learn to put them into boxes called variables so that you can do more with this information that just display it to the screen.

What next?

  • Learn how to read the secret messages programmers put in their code in the Comment Tutorial.

Something Easier?

page_revision: 4, last_edited: 1245736357|%e %b %Y, %H:%M %Z (%O ago)