Files
RCBASIC4/doc/doc_files/udt.html
2024-10-24 23:25:02 -04:00

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>&nbsp;player&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Dim</span>&nbsp;x,&nbsp;y&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">Type</span>&nbsp;<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>&nbsp;hero&nbsp;<span class="rc_keyword">As</span>&nbsp;player&nbsp;<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&nbsp;=&nbsp;<span class="rc_number">23</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hero x is "</span>;&nbsp;hero.x&nbsp;<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>&nbsp;enemy<b>[</b><span class="rc_number">20</span><b>]</b>&nbsp;<span class="rc_keyword">As</span>&nbsp;player&nbsp;<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>&nbsp;Player_Stats&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Dim</span>&nbsp;health&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Dim</span>&nbsp;power&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">Type</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">Type</span>&nbsp;Player&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Dim</span>&nbsp;x,&nbsp;y&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class="rc_keyword">Dim</span>&nbsp;stats&nbsp;<span class="rc_keyword">As</span>&nbsp;Player_Stats&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">Type</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">Dim</span>&nbsp;hero&nbsp;<span class="rc_keyword">As</span>&nbsp;Player&nbsp;<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&nbsp;=&nbsp;<span class="rc_number">100</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hero Health = "</span>;&nbsp;hero.stats.health&nbsp;<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>