-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.html
More file actions
208 lines (206 loc) · 14.3 KB
/
Copy patharrays.html
File metadata and controls
208 lines (206 loc) · 14.3 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
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
<!-- HTML header for doxygen 1.15.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.16.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Hatch Game Engine Documentation: Arrays</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="logo.svg" width="55" height="55"/></td>
<td id="projectalign">
<div id="projectname">Hatch Game Engine Documentation<span id="projectnumber"> v1.4.0</span>
</div>
<div id="projectbrief">The documentation for the Hatch Game Engine</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" class="search-icon" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()"><span class="search-icon-dropdown"></span></span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><div id="MSearchCloseImg" class="close-icon"></div></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.16.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(function(){initNavTree('arrays.html','',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">Arrays </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>As shown in <a class="el" href="variables.html" title="Variables">Variables</a>, <b>arrays</b> are objects that hold a resizable ordered list of values.</p>
<h2 class="doxsection"><a class="anchor" id="declaring-an-array"></a>
Declaring an array</h2>
<p>Arrays can be declared in 2 ways. The first, and more straightfoward way, is using the <span class="tt">[]</span> operator. Then you can optionally - between <span class="tt">[]</span> - fill the array with the desired values, separating them using a comma.</p>
<div class="fragment"><div class="line">var myArray = [<span class="stringliteral">"H"</span>, <span class="stringliteral">"a"</span>, <span class="stringliteral">"t"</span>, <span class="stringliteral">"c"</span>, <span class="stringliteral">"h"</span>, <span class="stringliteral">" "</span>, <span class="stringliteral">"E"</span>, <span class="stringliteral">"n"</span>, <span class="stringliteral">"g"</span>, <span class="stringliteral">"i"</span>, <span class="stringliteral">"n"</span>, <span class="stringliteral">"e"</span>];</div>
<div class="line">print(myArray);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: ["H", "a", "t", "c", "h", " ", "E", "n", "g", "i", "n", "e"]</div>
</div><!-- fragment --><p>Due to the object nature of arrays, it's also possible to create one by calling <a class="el" href="classArray.html#a6bfb46776366ee19dc47625086a66838" title="Array.Create">Array.Create</a>, which takes a length and a initial value as parameters. The initial value gets used to fill the array up to the desired length.</p>
<div class="fragment"><div class="line">print(<a class="code hl_class" href="classArray.html">Array</a>.<a class="code hl_function" href="classArray.html#a6bfb46776366ee19dc47625086a66838">Create</a>(4, 9 + 10));</div>
<div class="ttc" id="aclassArray_html"><div class="ttname"><a href="classArray.html">Array</a></div><div class="ttdoc">A resizable list of values.</div><div class="ttdef"><b>Definition</b> Array.dox:8</div></div>
<div class="ttc" id="aclassArray_html_a6bfb46776366ee19dc47625086a66838"><div class="ttname"><a href="classArray.html#a6bfb46776366ee19dc47625086a66838">Array.Create</a></div><div class="ttdeci">array Create(integer size, value initialValue)</div><div class="ttdoc">Creates an array.</div></div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: [19, 19, 19, 19]</div>
</div><!-- fragment --><h2 class="doxsection"><a class="anchor" id="reading--writing-to-individual-values"></a>
Reading & writing to individual values</h2>
<p>You can read the value of an item held by an array by typing the array's name then the item's index between <span class="tt">[]</span>. The first value of an array in HSL will always be 0.</p>
<div class="fragment"><div class="line">print(myArray[2] + myArray[6] + myArray[1] + myArray[3] + myArray[4] + myArray[9] + myArray[7] + myArray[8] + myArray[5] + myArray[0] + myArray[11] + myArray[10]);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: tEaching Hen</div>
</div><!-- fragment --><p>Similarly, you can also set the value by adding the <span class="tt">=</span> operator to the end of the above syntax followed by the value you want.</p>
<div class="fragment"><div class="line"><span class="comment">// repeat is a statement that repeats a set of instructions by a specified amount of times. Statements will be explained in the next chapter.</span></div>
<div class="line">repeat (myArray.Length(), number) {</div>
<div class="line"> <span class="comment">// Length() is a method found in the Array object class that returns the number of items present in the array, in this case, 11. Methods will be thoroughly explained in further chapters.</span></div>
<div class="line"> myArray[number] = myArray[(number + 1) % myArray.Length()];</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">print(myArray);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: ["a", "t", "c", "h", " ", "E", "n", "g", "i", "n", "e", "a"]</div>
</div><!-- fragment --><p>In both cases, trying to access values greater than or equal to the array's length or below 0 results in an error.</p>
<div class="fragment"><div class="line">print(myArray[4096]);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">ERROR: [...]</div>
<div class="line"> </div>
<div class="line"> Index 4096 is out of bounds of array of size 12.</div>
</div><!-- fragment --><div class="fragment"><div class="line">print(myArray[-32]);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">ERROR: [...]</div>
<div class="line"> </div>
<div class="line"> Index -32 is out of bounds of array of size 12.</div>
</div><!-- fragment --><h2 class="doxsection"><a class="anchor" id="inserting--removing-items"></a>
Inserting & removing items</h2>
<p>In this section we'll show a few methods that can be used to insert or remove items inside an array, those being: <a class="el" href="classArray.html#a7eaa9b3cddc5091a50e252847f126ffa" title="Array.Push">Array.Push</a>, <a class="el" href="classArray.html#afb7a03d033c289660ba7071e6cbb3dba" title="Array.Pop">Array.Pop</a>, <a class="el" href="classArray.html#ad31758907ac42342a06c2020f987387b" title="Array.Insert">Array.Insert</a>, <a class="el" href="classArray.html#a5d6da6e22b7647b346ca3f6f5e72df5b" title="Array.Erase">Array.Erase</a>, and <a class="el" href="classArray.html#a9a045036ddca9accb8ba76bb14f01fa1" title="Array.Clear">Array.Clear</a>. As mentioned previously, methods will be explained in future chapters.</p>
<p>You may find other methods accompanied by explanations in the <a class="el" href="classArray.html" title="Array">Array</a> page.</p>
<h3 class="doxsection"><a class="anchor" id="push--pop"></a>
Push & Pop</h3>
<p>The <span class="tt">Push</span> method can be used to append values to the end of an array.</p>
<div class="fragment"><div class="line">var myFavoriteNumbers = [0, 8, 16];</div>
<div class="line">myFavoriteNumbers.Push(32);</div>
<div class="line"> </div>
<div class="line">print(myFavoriteNumbers);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: [0, 8, 16, 32]</div>
</div><!-- fragment --><p><span class="tt">Pop</span>, on the other hand, removes the last item inside an array, returning the removed value after.</p>
<div class="fragment"><div class="line">myFavoriteNumbers.Pop();</div>
<div class="line">myFavoriteNumbers.Pop();</div>
<div class="line"> </div>
<div class="line">print(myFavoriteNumbers);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: [0, 8]</div>
</div><!-- fragment --><h3 class="doxsection"><a class="anchor" id="insert--erase"></a>
Insert & Erase</h3>
<p><span class="tt">Insert</span> takes an index and a value as arguments. The value gets appended at the index defined by first argument. Using <span class="tt">Insert</span> shifts all the items after the specified index to the right to accomodate the new value.</p>
<div class="fragment"><div class="line">var dayAction = <span class="stringliteral">"Game"</span>;</div>
<div class="line">var nightAction = <span class="stringliteral">"Engine"</span>;</div>
<div class="line">var combination = <span class="stringliteral">"Hatch"</span>;</div>
<div class="line"> </div>
<div class="line">var encouragement = [<span class="stringliteral">"First we "</span>, <span class="stringliteral">", then we "</span>, <span class="stringliteral">". Let\'s "</span>, <span class="stringliteral">"!"</span>]; </div>
<div class="line"> </div>
<div class="line">encouragement.Insert(1, dayAction);</div>
<div class="line">encouragement.Insert(3, nightAction);</div>
<div class="line">encouragement.Insert(5, combination);</div>
<div class="line"> </div>
<div class="line">print(encouragement);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: ["First we ", "Game", ", then we ", "Engine", ". Let's ", "Hatch", "!"]</div>
</div><!-- fragment --><p>As a counterpart, <span class="tt">Erase</span> removes any value specified in the index argument, shifting all the values after the index to the left.</p>
<div class="fragment"><div class="line">encouragement.Erase(0);</div>
<div class="line">encouragement.Erase(2);</div>
<div class="line">encouragement.Erase(4);</div>
<div class="line">encouragement.Erase(6);</div>
<div class="line"> </div>
<div class="line">print(encouragement);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: ["Game", Engine", "Hatch"]</div>
</div><!-- fragment --><h3 class="doxsection"><a class="anchor" id="clear"></a>
Clear</h3>
<p>Finally, <span class="tt">Clear</span> erases all the elements inside the array.</p>
<div class="fragment"><div class="line">var competitors = [<span class="stringliteral">"Blender"</span>, <span class="stringliteral">"Scratch"</span>, <span class="stringliteral">"Doom Engine"</span>];</div>
<div class="line"> </div>
<div class="line">competitors.Clear();</div>
<div class="line"> </div>
<div class="line">print(competitors);</div>
</div><!-- fragment --> <div class="fragment"><div class="line">INFO: []</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.16.0 </li>
</ul>
</div>
</body>
</html>