-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch02.html
More file actions
152 lines (151 loc) · 8.99 KB
/
Copy pathch02.html
File metadata and controls
152 lines (151 loc) · 8.99 KB
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
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
td, th { border: 1px solid #c3c3c3; padding: 0 3px 0 3px; }
table { border-collapse: collapse; }
img { max-width: 100%; }
</style>
<meta name="generator" content="ReText 7.2.3">
<title>ch02</title>
<style type="text/css">
</style>
</head>
<body>
<p><a href="ch01.html">Previous</a> <a href="index.html">Index</a> <a href="ch03.html">Next</a></p>
<hr>
<h1>2 - Your first script</h1>
<h4>Table of Contents</h4>
<ul>
<li><a href="#2.1">2.1 Hello, world!</a></li>
<li><a href="#2.2">2.2 Running the script</a></li>
<li><a href="#2.3">2.3 Other scripts</a></li>
<li><a href="#2.4">2.4 Axmud's data directory</a></li>
<li><a href="#2.5">2.5 The Launch task</a></li>
<li><a href="#2.6">2.6 Running a script as a task</a></li>
<li><a href="#2.7">2.7 Editing a script</a></li>
</ul>
<hr>
<h2><a name="2.1">2.1 Hello, world!</a></h2>
<p>Traditionally, programming tutorials begin with a script that displays a simple greeting.</p>
<p>Here is your first Axbasic script. You don't need to type anything yet - just keep reading for now.</p>
<pre><code> REM A trivial Axbasic script
PRINT "Hello, world!"
END
</code></pre>
<p>The script contains three lines. Let's examine them one by one.</p>
<pre><code> REM A trivial Axbasic script
</code></pre>
<p>REM is short for <em>remark</em>. When running the script, Axmud ignores the whole line, so you can type anything you like after the REM.</p>
<pre><code> PRINT "Hello, world!"
</code></pre>
<p>In the early days of computing, computers did not have monitors. Output was actually printed on a roll of paper, not displayed on a screen.</p>
<p>In Axbasic, PRINT is an instruction to display something in Axmud's main window. It doesn't send anything to your printer.</p>
<p>The message must be enclosed within double quotes. If you miss out the quotes, when you try to run the script you'll see something other than what you were expecting.</p>
<pre><code> END
</code></pre>
<p>END is, unsurprisingly, an instruction to stop running the script.</p>
<p>Every Axbasic script <em>must</em> contain exactly one END - no more, no less.</p>
<p>If you were to add anything after the END - another PRINT, perhaps - it wouldn't be executed. You wouldn't even see an error message.</p>
<h2><a name="2.2">2.2 Running the script</a></h2>
<p>Above we said that you didn't need to type anything. That's because Axmud comes with three Axbasic scripts, including the one we've just been discussing.</p>
<p>Let's run the script now. The script is called <strong>hello</strong>, so type this client command:</p>
<pre><code> ;runscript hello
;rs hello
</code></pre>
<p>The output will look like this:</p>
<pre><code> AXBASIC: Executing 'hello'
Hello world!
AXBASIC: Execution of 'hello' complete
</code></pre>
<p>In a moment we'll modify the script to display a different message.</p>
<p>By the way, you can use a so-called <em>script command</em> to run an Axbasic script. Script commands, if they are enabled, start with an ampersand character followed by the name of the script:</p>
<pre><code> &hello
</code></pre>
<h2><a name="2.3">2.3 Other scripts</a></h2>
<p>The second script is called <strong>test</strong>.</p>
<pre><code> REM Write your own Axbasic code here
END
</code></pre>
<p>Try it for yourself:</p>
<pre><code> ;runscript test
;rs test
</code></pre>
<p>The output will look like this:</p>
<pre><code> AXBASIC: Executing 'test'
AXBASIC: Execution of 'test' complete
</code></pre>
<p>As you can see, this script literally does nothing. Execution of the script starts and then stops.</p>
<p>Why do you need a script that does nothing? Well, in a moment we're going to start editing the scripts ourselves.</p>
<p>You can use the <strong>test</strong> script to try new ideas, or to test lines of code. When you're ready to execute them, you <em>could</em> type this long command:</p>
<pre><code> ;runscript test
;rs test
</code></pre>
<p>But, instead, you can miss out the name of the script altogether. When you don't specify a name, Axmud executes the <strong>test</strong> script.</p>
<pre><code> ;runscript
;rs
</code></pre>
<p>The third script is called <strong>wumpus</strong>. It's a copy of the 1972 classic <em>Hunt the Wumpus</em>, one of the first adventure games ever written.</p>
<p>It’s not much fun to play, but it proves that Axmud really <em>can</em> run BASIC programmes written over the span of many decades.</p>
<pre><code> ;runscript wumpus
;rs wumpus
</code></pre>
<p>If you decide, after several moments of play, that <em>Hunt the Wumpus</em> isn't quite as much fun as <em>Grand Theft Auto</em>, you can halt the script by clicking the <strong>Cancel</strong> button in one of the <strong>INPUT</strong> windows that appear.</p>
<h2><a name="2.4">2.4 Axmud's data directory</a></h2>
<p>Axmud stores all of its data files in its own data <em>directory</em>. (MS Windows users prefer to call it a <em>folder</em>.)</p>
<p>Inside the data directory there's another directory called <strong>scripts</strong>, and this is where you can store all your Axbasic scripts.</p>
<p>That directory can be easy or difficult to find, depending on the system you're using. The foolproof way is to use the following client command:</p>
<pre><code> ;listdirectory
;ldr
</code></pre>
<p>The files themselves must end with the extension <strong>.bas</strong> (short for BASIC, of course). We've already mentioned that Axmud comes with three scripts; the files are called:</p>
<pre><code> hello.bas
test.bas
wumpus.bas
</code></pre>
<p>Axmud won't interfere with any of the files in this directory, so you can add, edit and delete scripts as you please.</p>
<p>When you run a script with this command:</p>
<pre><code> ;runscript something
</code></pre>
<p>Axmud will look in the directory for a file called <strong>something.bas</strong>. If the file doesn't exist, the script won't run. (Later we'll discuss how to you can store scripts in different directories, if you need to.)</p>
<h2><a name="2.5">2.5 The Launch task</a></h2>
<p>Of course, you're free at any time to open a file browser, navigate through several directories, find the script you want to edit and then open it in a text editor. But there's a simpler way.</p>
<p>Axmud comes with a task, called the Launch task, that makes things easy. Use this client command to open it:</p>
<pre><code> ;starttask launch
;st launch
</code></pre>
<p>The task opens a window which lists all of your Axbasic scripts.</p>
<p>You can run a script by clicking on <strong>hello.bas</strong>, then clicking the <strong>Run script</strong> button.</p>
<h2><a name="2.6">2.6 Running a script as a task</a></h2>
<p>Axbasic scripts tend to fall into one of two types.</p>
<p>The first type of script is one that runs very quickly. It starts, does something, and then stops.</p>
<p>During this time, Axmud can't do anything else - it can't react to the commands you type, or communicate with the world - but that doesn't matter, because the script takes only a tiny fraction of a second to run.</p>
<p>The <strong>hello</strong> script is one such script.</p>
<p>The second type of script takes several minutes (or hours, or days) to run. It might even run continuously.</p>
<p>Axmud tasks are designed to do exactly that - run continuously without interrupting everything else. If you click the <strong>Run as task</strong> button, Axmud will create a task especially for the script you want to run.</p>
<p>The <strong>wumpus</strong> script is a game that takes several minutes to play, so it should definitely be run as a task. By the way, you can also run a script as a task using this client command:</p>
<pre><code> ;runscripttask wumpus
;rst wumpus
</code></pre>
<h2><a name="2.7">2.7 Editing a script</a></h2>
<p>Let's modify the <strong>hello</strong> script to display a different message. (If you don't want to modify it, you can create a brand new script with the <strong>New script</strong> button, and modify that instead.)</p>
<p>In the Launch task window, click on <strong>hello.bas</strong> and then click <strong>Edit script</strong>.</p>
<p>Try editing the script to show a different message. For example, you might change it to:</p>
<pre><code> "Hello, mother!"
</code></pre>
<p>You can make the script display as many messages as you want. You can also add as many comments as you want.</p>
<p>For example, your edited script might look something like this:</p>
<pre><code> REM A trivial Axbasic script
PRINT "Hello, mother!"
PRINT "Where is my cat?"
REM I can add as many comments as I want
PRINT "Oh, never mind, I found him"
END
</code></pre>
<p>When you've finished editing, don't forget to save the file. (CTRL + S works in most text editors.)</p>
<p>As soon as you've saved the file, you can run the script. If you can get it to run without an error message, you're ready to move on to the next Section.</p>
<hr>
<p><a href="ch01.html">Previous</a> <a href="index.html">Index</a> <a href="ch03.html">Next</a></p>
</body>
</html>