forked from LegoGMI/ManualeGM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path214_00_scripts.html
347 lines (292 loc) · 15 KB
/
214_00_scripts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<title>Scripts</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body background="images/back.gif">
<!--START-->
<h1>Scripts</h1>
<p>
<i>GameMaker</i> has a built-in programming language called GML. Once you become more
familiar with <i>GameMaker</i> and want to use it to its fullest extend, it
is advisable to start learning to use this language. For more details in the
GML language see <A HREF="400_index.html">Part 4</A> of this documentation.
</p>
<p>
There are two ways to
use the language. First of all you can create scripts. These are pieces of
code to which you give a name. They are shown in the resource tree and can be
saved to a file and loaded from a file. They can even be used to form a library
that extends the possibilities of <i>GameMaker</i>. Alternatively, you can
add a code action to some event and type a piece of code there. Adding code
actions works in exactly the same way as adding scripts except for two
differences. Code actions don't have a name and cannot use arguments. Also
they have the well-known field to indicate to what objects the action should
apply. For the rest you enter code in exactly the same way as in scripts. We
recommend you to use scripts for all but simple pieces of code as <i>GameMaker</i>
has many more features that deal with scripts.
So we further concentrate on scripts in this chapter.
</p>
<p>
As stated before, a script is written with code in GML (the built-in programming language)
and is meant to perform a particular task. Scripts can takes input-variables called arguments
(sometimes called parameters). To execute a script from any event, you can use
the script action. In the script action you specify
the script you want to execute, together with the up to five arguments.
</p>
<p>
You can also execute a script from within a piece of code (a different script) in the same way you
call a GM-function. In that case you can use up to 16 arguments.
Scripts can return a value. This is often used to build calculating methods
(mathematical methods). The <b>return</b> keyword is used for this.
No code after the return keyword is executed!
When a script returns a value, you can also use it as a function when
providing values in other actions.
</p>
<p>
Scripts are extremely useful to extend the possibilities of <i>GameMaker</i>.
This does though require that you design your scripts carefully. Scripts
can be stored in files that can be added to your game. To import a
script file, use the item <b>Import scripts</b> from the <b>Scripts</b> menu. To save your
scripts in the form of a file use <b>Export scripts</b>. Script libraries
are simple text files (although they have the extension .gml). Preferably
don't edit them directly because they have a special structure.
</p>
<p>
To create a script in your game, choose <b>Create Script</b> from the <b>Resources</b> menu.
A code editor will appear that will be described next.
</p>
<h3>The code editor</h3>
<p>
When creating or editing a script (or a code action) the following code editor form
will appear (in the example we already added a
little script that computed the product of the two arguments).
</p>
<p>
<img src="images/form_script.jpg">
</p>
<p>
At the top right you can indicate the name of the script. You should give all
of your scripts a sensible name. Make sure the name only consists of letter, digits,
and the _ symbol. It should not start with a digit. In this way you can use the
script later as a function in code.
</p>
<p>
At the top you have a toolbar with some useful buttons. From left to right you
have the OK button to close the editor, saving the changes, buttons to load the
script from a text file, save it to a text file, and print it.
</p>
<p>
Next there are undo and redo buttons. The editor will basically remember all
changes you did and you can undo them (or redo the undone changes). You can change
the maximal number of undo operations in the Preferences. Here you can also
indicate whether you want to group the undo operations (such that e.g. typing
a line of text can be undone in one step) or that every keystroke is separate
saved. You can use <b><Ctrl> + Z</b> for undo and <b><Ctrl></b> + <b><Shift> + Z</b> for
redo.
</p>
<p>
Next to this there are buttons to cut, copy, and paste text, as you should be familiar
with. The final two buttons are for finding and replacing and for code checking. These will
be explained below in detail.
</p>
<p>
Below the toolbar there is the area where you can type the text. At the left there are the
line numbers. (You can switch these on and off using the F9 key.) The line with the cursor
on it is marked in bold. The dark red bar behind certain line numbers indicated that these
lines have changed. You will notice that you can type text anywhere on the lines. So
a line does not really have an end. This tends to be useful when alligning text. There
are many editing possibilities, that will be described below.
</p>
<p>
At the bottom there is the status bar. This shows from left to right the current line number,
total number of lines and cursor position. Whether you are in Insert mode or Overwrite mode. (In
Overwrite mode also the cursor chnanges to make it clear that you are overwriting the existing text.)
Next there is the point size for the font used. You can use F7 and F8 to decrease or increase the
font size. (You have more control over the font size and type in the Preference.)
</p>
<h3>Editing tools</h3>
<p>
The editor has many editing possibilities. Some of these can also be accessed through the
pop-up menu that appears when you press the right mouse button. Here are the most important ones:
</p>
<ul>
<li>Multiple undo and redo either per key press or in groups</li>
<li>The arrow keys move you around in the text. Use <b><Ctrl></b> with
the left and right arrow key to move to the beginning of the previous
or next word. Use <b><Ctrl></b> with the up and down key to also scroll
the text. Hold the <b><Shift></b> key to select text.</li>
<li>Double click the line number to select the complete line.</li>
<li>The Home key moves to the beginning of the line and the End key
to the end of the line. With <b><Ctrl></b> they move to the start and end of the
script. Hold the <b><Shift></b> key to select text.</li>
<li>You can scroll through the text with the mouse wheel.
With <b><Ctrl></b> the cursor scrolls with the text. Hold the <b><Shift></b> key to select text.</li>
<li>Intelligent auto indent that aligns with the previous line when you use the
return key of the backspace key (can be set in the preferences). </li>
<li>Intelligent tabbing that tabs till the first non space in the previous
lines (can be set in the preferences).</li>
Use <b><TAB> </b> to indent <i>selected </i>lines and <b><Shift> +
<TAB></b> to unindent <i>selected</i> lines.</li>
<li>Hold <b><Ctrl></b> with the Backspace or Delete key to delete until the
beginning of the previous or next word.</li>
<li>You can drag a selection with the mouse. (Use b><Ctrl></b> to copy it.) The new location is immediately shown.</li>
<li>Cut, copy and paste of text between script forms and other programs.ograms.</li>
Use <b>F12</b> to open the script or resource whose name is at the cursor
position.</li>
</ul>
<h3>Color coding</h3>
As you might have noticed, parts of the script text are colored. The editor
knows about existing objects, built-in variables and functions, etc. Color-
coding helps a lot in avoiding mistakes. In particular, you see immediately
if you misspelled some name or use a keyword as a variable.
If you don't like the color coding, in the Preferences you can switch
it on and off (you can also use <b>F10</b>). In the Preferences you can also change the
color for the different components of the code.
</p>
<h3>Function and variable help</h3>
<pGameMaker</i> contains many built-in functions and variables and you add your own
resources and scripts to this. So it might be hard to remember all of them. Fortunately
the editor helps you with this. When you type part of a name (at least two characters) and
wait a little, a list is shown of all possibilities. For example, as follows:
</p>
<p>
<img src="images/form_completion.jpg">
</p>
<p>
You can continue typing and the list will adapt. You can use the up and down keys to scroll through
the list and press Enter to replace the text you are typing with the selected function or variable name.
</p>
<p>
You can switch this feature on and off in the preferences. You can always force the list to appear
by pressing <b><Ctrl> + Space,</b> and you can cancel it at anytime by
pressing <b><ESC></b>.</p>
<p>
A second form of help is given when you are typing in the arguments for a function. In this case, in the
status bar at the bottom the function with its arguments is given. The current argument you are typing is
shown in bold. In this way you will never forget arguments or get the order wrong.
ong.
</p>
<h3>Find and replace</h3>
<p>
It is common that you want to find some piece of text in your code or replace some text with another one.
To this end you can either press the Find button in the toolbar, or press <b>
<CTRL>+F</b>. A panel will show up at the right of the form, as follows: </p>
<p>
<img src="images/form_script2.jpg">
</p>
<p>
You can type the find string at the top. Note that in the code all occurrences of the find string are immediately shown
with a yellow background. (You can switch this off in the Preferences.) As usual you can indicate whether the search
must be case sensitive (which is normally what you want as variable and function names in GML are also case sensitive)
and whether you want to only search for whole words. Note that the editor remembers your most recent searches.
/p>
<p>
With the buttons with the blue arrows you can now move to the previous, next, first, and last occurrence of the
find string. It will be selected in the text such that you can e.g. delete it.
</p>
<p>
When you want to replace occurrences, also type a text in the replace box. Now you can use the buttons to replace the
previous occurrence, the next, the first, the last, or all occurrences of the find text. Remember you can always
use Undo to undo the replacements.
</p>
<h3>Code snippets</h3>
<p>
Code snippets are small pieces of code that you will often use. You can add them in the code through the snippet menu.
To this end press F2. A menu appears from which you can select the snippet. It will be inserted at the cursor in
your code. Some pieces consists of capital letters only. These must still be replaced by you. The first one is
selected for replacement. To replace another, simply double click on it.
</p>
<p>
You can define your own code snippets if you want. The snippets are stored in the file <tt>snippets.txt</tt> in
the folder in which GameMaker</i> is installed. You can edit this file with any text editor. Each snippet is
on a separate line and there should be no blank lines. It starts with the name of the snippet (shown in the menu)
followed by the colon (:) followed by the actual snippet. Use # for a newline and put the text that needs to
be selected first between | symbols. See the examples that are already in the file. (Better make a backup of
it before you start changing it.)
</p>
<h3>Error checking</h3>
<p>
In the toolbar at the top there is a button with which you can switch error checking on or off.
When error checking is on, GameMaker</i> constantly tests the code you are typing and reports
the first error it finds at the bottom of the window. Also the line number for the line with the
error is drawn in red. You can click on the error message to go to the correct line and position to
correct the error. Note that not all aspects can be tested at this stage but the syntax of your
script will be tested, together with the existence of functions used.
</p>
<p>
When you start typing a script, error checking might be annoying so you can switch it off. But when the
script is almost ready you better switch it on to correct all the errors. Note that also the
color coding will help you catch errors. Finally, when you put your cursor at a bracket the editor
will show you the corresponding opening or closing bracket. If it cannot find it, it is shown in red.
(You can switch this off in the Preferences.)
)
</p>
<h3>Debugging</h3>
<p>
When creating scripts you can easily make mistakes. Always test the scripts by
using the error checking button. However, this will not catch all errors. So
you will need to run the game to test your code.
</p>
<p>
When an error occurs during the execution of a
script this is reported, with an indication of the type of error, the script or
action it occurs in, and the line and position.
Rarely you will see a popup with the text
"Unexpected error occurred during the game".
This error message indicate that some problem occurred in windows or in
the hardware. Often the reason for this is infinite recursion, lack of
memory or insufficient hardware, drivers or firmware.
Generally speaking, these errors have to do with problems outside the GameMaker</i> environment.
</p>
<p>
If you need to check things more carefully, you can run the game in
debug mode. Now a form appears in which you can monitor lots of information
in your game.
.
</p>
<p>
<img src="images/form_debug.jpg">
</p>
<p>
Under the <b>Run</b> menu you can pause the game, run it step by step and
even restart it. Under the <b>Watch</b> menu you can watch the value of
certain expressions. Use <b>Add</b> to type in some expression whose value
is shown in each step of the game. In this way you can see whether your game
is doing things the right way. You can watch many expressions. You can save
them for later use (e.g. after you made a correction to the game). Under the
<b>Tools</b> menu you find items to see even more information. You can see a
list of all instances in the game, you can watch all global variables (well,
the most important ones) and the local variables of an instance (either use
the object name or the id of the instance). You can also view messages which
you can send from your code using the function <tt>show_debug_message(str)</tt>.
Finally you can give the game commands and change the speed of the
game. If you make complicated games you should really learn how to use the
debug options.
</p>
<!--END-->
</body>
</html>
<!-- KEYWORDS
adding scripts
creating scripts
scripts
importing scripts
exporting scripts
script files
color coding
completion
auto-completion
function help
code snippets
snippets
testing scripts
scripts, function help
code editor
script, editor
debugging
watching variables
watching instances
show_debug_message
frames per second
-->