Files
RCBASIC4/doc/doc_files/udt.html
2024-10-27 11:52:28 -04:00

73 lines
4.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>RCBasic UDTs [RCBasic Doc] </title>
</head>
<body>
<p><h2>USER DEFINED TYPES (UDTs) </h2></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>