73 lines
4.0 KiB
HTML
73 lines
4.0 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> 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> |