blob: f7e89a4958996d2fd00d783ca59825e943066804 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001 LPC Basics
2 Written by Descartes of Borg
3 first edition: 23 april 1993
4 second edition: 17 june 1993
5
6CHAPTER 3: LPC Data Types
7
83.1 What you should know by now
9LPC object are made up of zero or more variables manipulated by one or
10more functions. The order in which these functions appear in code is
11irrelevant. The driver uses the LPC code you write by loading copies of
12it into memory whenever it is first referenced and additional copies
13through cloning. When each object is loaded into memory, all the variables
14initially point to no value. The reset() function in compat muds, and
15create() in native muds are used to give initial values to variables in
16objects. The function for creation is called immediately after the object
17is loaded into memory. However, if you are reading this textbook with no
18prior programming experience, you may not know what a function is or how
19it gets called. And even if you have programming experience, you may
20be wondering how the process of functions calling each other gets started
21in newly created objects. Before any of these questions get answered,
22however, you need to know more about what it is the functions are
23manipulating. You therefore should thouroughly come to know the concept
24behind LPC data types. Certainly the most boring subject in this manual,
25yet it is the most crucial, as 90% of all errors (excepting misplaced
26{} and ()) involve the improper usage of LPC data types. So bear through
27this important chapter, because it is my feeling that understanding this
28chapter alone can help you find coding much, much easier.
29
303.2 Communicating with the computer
31You possibly already know that computers cannot understand the letters
32and numbers used by humans. Instead, the "language" spoken by computers
33consists of an "alphabet" of 0's and 1's. Certainly you know computers
34do not understand natural human languages. But in fact, they do not
35understand the computer languages we write for them either. Computer
36languages like BASIC, C, C++, Pascal, etc. are all intermediate
37languages. They allow you to structure your thoughts more coherently
38for translation into the 0's and 1's of the computer's languages.
39
40There are two methods in which translation is done: compilation and
41interpretation. These simply are differences betweem when the
42programming language is translated into computer language. With
43compiled languages, the programmer writes the code then uses a program
44called a compiler to translate the program into the computer's
45language. This translation occurs before the program is run. With
46interpreted languages however, the process of translation occurs as
47the program is being run. Since the translation of the program is
48occurring during the time of the program's running in interpreted
49languages, interpreted languages make much slower programs than
50compiled languages.
51
52The bottom line is, no matter what language you are writing in, at
53some point this has to be changed into 0's and 1's which can be
54understood by the computer. But the variables which you store in
55memory are not simply 0's and 1's. So you have to have a way in
56your programming languages of telling the computer whether or not
57the 0's and 1's should be treated as decimal numbers or characters or
58strings or anything else. You do this through the use of data types.
59
60For example, say you have a variable which you call 'x' and you give
61it the decimal whole number value 65. In LPC you would do this through
62the statement:
63
64-----
65x = 65;
66-----
67
68You can later do things like:
69
70_____
71write(x+"\n"); /* \n is symbolically represents a carriage return */
72y = x + 5;
73-----
74
75The first line allows you to send 65 and a carriage return to someone's screen.
76The second line lets you set the value of y to 70.
77The problem for the computer is that it does not know what '65' means when
78you tell it x = 65;. What you think of 65, it might think of as:
7900000000000000000000000001000001
80But, also, to the computer, the letter 'A' is represented as:
8100000000000000000000000001000001
82So, whenever you instruct the computer write(x+"\n");, it must have some
83way of knowing that you want to see '65' and not 'A'.
84
85The computer can tell the difference between '65' and 'A' through the use
86of data types. A data types simply says what type of data is being stored
87by the memory location pointed to by a given variable. Thus, each LPC
88variable has a variable type which guides conversions. In the example
89given above, you would have had the following line somewhere in the
90code *before* the lines shown above:
91
92-----
93int x;
94-----
95
96This one line tells the driver that whatever value x points to, it will
97be used as the data type "int", which is short for integer, or whole
98number. So you have a basic introduction into the reason why data types
99exist. They exist so the driver can make sense of the 0's and 1's that
100the computer is storing in memory.
101
1023.3 The data types of LPC
103All LPMud drivers have the following data types:
104
105void, status, int, string, object, int *, string *, object *, mixed *
106
107Many drivers, but not all have the following important data types which
108are important to discuss:
109
110float, mapping, float *, mapping *
111
112And there are a few drivers with the following rarely used data types
113which are not important to discuss:
114
115function, enum, struct, char
116
1173.4 Simple data types
118This introductory textbook will deal with the data types void, status,
119int, float, string, object, mand mixed. You can find out about the
120more complex data types like mappings and arrays in the intermediate
121textbook. This chapter deals with the two simplest data types (from the
122point of view of the LPC coder), int and string.
123
124An int is any whole number. Thus 1, 42, -17, 0, -10000023 are all type int.
125A string is one or more alphanumeric characters. Thus "a", "we are borg",
126"42", "This is a string" are all strings. Note that strings are always
127enclosed in "" to allow the driver to distinguish between the int 42 and
128the string "42" as well as to distinguish between variable names (like x)
129and strings by the same names (like "x").
130
131When you use a variable in code, you must first let the driver know
132what type of data to which that variable points. This process is
133called *declaration*. You do this at the beginning of the function
134or at the beginning of the object code (outside of functions before all
135functions which use it). This is done by placing the name of the data type
136before the name of the variable like in the following example:
137
138-----
139void add_two_and_two() {
140 int x;
141 int y;
142
143 x = 2;
144 y = x + x;
145}
146-----
147
148Now, this is a complete function. The name of the function is
149add_two_and_two(). The function begins with the declaration of an
150int variable named x followed by the declaration of an in variable
151named y. So now, at this point, the driver now has two variables which
152point to NULL values, and it expects what ever values end up there to be
153of type int.
154
155A note about the data types void and status:
156Void is a trivial data type which points to nothing. It is not used
157with respect to variables, but instead with respect to functions. You
158will come to understand this better later. For now, you need only
159understand that it points to no value.
160
161The data type status is a boolean data type. That is, it can only have
1621 or 0 as a value. This is often referred to as being true or false.
163
1643.5 Chapter summary
165For variables, the driver needs to know how the 0's and 1's the computer
166stores in memory get converted into the forms in which you intend them
167to be used. The simplest LPC data types are void, status, int, and string.
168You do not user variables of type void, but the data type does come
169into play with respect to functions. In addition to being used for
170translation from one form to the next, data types are used in determining
171what rules the driver uses for such operations as +, -, etc. For example,
172in the expression 5+5, the driver knows to add the values of 5 and 5
173together to make 10. With strings however, the rules for int addition
174make no sense. So instead, with "a"+"b", it appends "b" to the string "a"
175so that the final string is "ab". Errors can thus result if you mistakenly
176try to add "5"+5. Since int addition makes no sense with strings, the
177driver will convert the second 5 to "5" and use string addition. The final
178result would be "55". If you were looking for 10, you would therefore
179have ended up with erroneous code. Keep in mind, however, that in most
180instances, the driver will not do something so useful as coming up with
181"55". It comes up with "55" cause it has a rule for adding a string
182to an int, namely to treat the int as a string. In most cases, if you
183use a data type for which an operation or function is not defined
184(like if you tried to divide "this is" by "nonsense", "this is"/"nonsense"),
185the driver will barf and report an error to you.