141 lines
5.2 KiB
HTML
141 lines
5.2 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
table, th, td { border: 1px solid black; } th, td { padding: 10px; }
|
|
body { background-color: #064066; color: #FFFFFF; }
|
|
a { color: #40bfb8; }
|
|
a::before {
|
|
content: "---";
|
|
font-family: monospace, monospace;
|
|
display: inline-block;
|
|
margin-right: 6px;
|
|
color: #064066;
|
|
}
|
|
|
|
.rc_number { color: #0b9898; }
|
|
.rc_string { color: #dd4040; }
|
|
.rc_keyword { color: #6084a8; font-weight: bold; }
|
|
.rc_comment { color: #6e716e; }
|
|
#rc_code { font-family: Consolas,"courier new"; background-color: #2d3335; padding: 2px; font-size: 105%; }
|
|
|
|
|
|
ul, #myUL {
|
|
list-style-type: none;
|
|
}
|
|
|
|
#myUL {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
|
|
.box {
|
|
cursor: pointer;
|
|
-webkit-user-select: none; /* Safari 3.1+ */
|
|
-moz-user-select: none; /* Firefox 2+ */
|
|
-ms-user-select: none; /* IE 10+ */
|
|
user-select: none;
|
|
}
|
|
|
|
.box::before {
|
|
content: "[+]";
|
|
font-family: monospace, monospace;
|
|
font-weight: bold;
|
|
color: #40bfb8;
|
|
display: inline-block;
|
|
margin-right: 6px;
|
|
}
|
|
|
|
.box {
|
|
font-weight: bold;
|
|
color: #40bfb8;
|
|
text-decoration: underline;
|
|
}
|
|
|
|
.check-box::before {
|
|
content: "[-]";
|
|
font-family: monospace, monospace;
|
|
font-weight: bold;
|
|
//color: dodgerblue;
|
|
color: #40bfb8;
|
|
}
|
|
|
|
.nested {
|
|
display: none;
|
|
}
|
|
|
|
.active {
|
|
display: block;
|
|
}
|
|
|
|
|
|
</style>
|
|
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
|
|
<title>RCBasic UDTs [RCBasic Doc] </title>
|
|
</head>
|
|
|
|
<body>
|
|
<p><h1>USER DEFINED TYPES (UDTs) </h1></p>
|
|
<p>
|
|
RCBasic v4 and up introduces the ability to create user defined types. These are basically structures that allow you to store and manage related data. To create a user defined type you need to use the <b>TYPE</b> keyword. Look at the following:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
<span class="rc_keyword">Type</span> player <br>
|
|
<span class="rc_keyword">Dim</span> x, y <br>
|
|
<span class="rc_keyword">End</span> <span class="rc_keyword">Type</span> <br>
|
|
</code></p>
|
|
<p>
|
|
In the above code, a type called player is created. The <b>DIM</b> keyword must be used to add attributes to our type. Now we can create a variable with the data type player as follows:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
<span class="rc_keyword">Dim</span> hero <span class="rc_keyword">As</span> player <br>
|
|
</code></p>
|
|
<p>
|
|
Notice that in the above code, we are using the <b>DIM</b> keyword we have used in previous sections to create variables and arrays. We now have a variable called <i>hero</i> whose data type is <i>player</i>.
|
|
</p>
|
|
<p>
|
|
Since our <i>hero</i> variable is of type <i>player</i>, it has all the attributes of that type. So we can access the attribute's with a "." like so:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
hero.x = <span class="rc_number">23</span> <br>
|
|
<span class="rc_keyword">Print</span> <span class="rc_string">"Hero x is "</span>; hero.x <br>
|
|
</code></p>
|
|
<p>
|
|
The attributes of a UDT variable are accessed the same way a normal variable is. You can also create an array of UDT's the same way you would create a normal array. Look at the following:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
<span class="rc_keyword">Dim</span> enemy<b>[</b><span class="rc_number">20</span><b>]</b> <span class="rc_keyword">As</span> player <br>
|
|
</code></p>
|
|
<p>
|
|
If you read through the section on arrays then this should make sense. We are using the <b>DIM</b> keyword to make an array called <i>enemy</i> and then we use the <b>AS</b> keyword to set the type of <i>enemy</i> to <i>player</i>.
|
|
</p>
|
|
<p>
|
|
UDTs can also be used for attributes inside other UDTs. Lets say we wanted each player to have stats like health and power. We could create a UDT for player stats and have an attribute of that stat type inside our player UDT. Here is that example demonstrated:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
<span class="rc_keyword">Type</span> Player_Stats <br>
|
|
<span class="rc_keyword">Dim</span> health <br>
|
|
<span class="rc_keyword">Dim</span> power <br>
|
|
<span class="rc_keyword">End</span> <span class="rc_keyword">Type</span> <br>
|
|
<br>
|
|
<span class="rc_keyword">Type</span> Player <br>
|
|
<span class="rc_keyword">Dim</span> x, y <br>
|
|
<span class="rc_keyword">Dim</span> stats <span class="rc_keyword">As</span> Player_Stats <br>
|
|
<span class="rc_keyword">End</span> <span class="rc_keyword">Type</span> <br>
|
|
<br>
|
|
<span class="rc_keyword">Dim</span> hero <span class="rc_keyword">As</span> Player <br>
|
|
</code></p>
|
|
<p>
|
|
In the above example, <i>hero</i> now has an attribute called <i>stats</i> that is of type <i>Player_Stats</i>. So now we can access the stats attributes like so:
|
|
</p>
|
|
<p id="rc_code"><code>
|
|
hero.stats.health = <span class="rc_number">100</span> <br>
|
|
<span class="rc_keyword">Print</span> <span class="rc_string">"Hero Health = "</span>; hero.stats.health <br>
|
|
</code></p>
|
|
<p>
|
|
If used effectively, you can drastically increase the readability and maintainability of your code. Especially in large projects.
|
|
</p>
|
|
<p>
|
|
|
|
</body>
|
|
</html> |