TGC Codebase Backup



Arrays in UDT Workaround (Beginner by The Wendigo

17th Jul 2005 1:35
Summary

A way to handle a dynamic amount of data inside of a user defined type. This is a workaround which is very useful in all languages to an extent.



Description

First off, this may actually working in Classic as well as Pro but I haven't tested it. The method I present is actually very similar to how databases link information between tables. The idea is that you have an array of user defined types we will call Catagory. Next you have a second array of user defined types we will call SubCatagory. As you would guess, for our purposes SubCatagory should reside inside of Catagory, but Dark Basic does not support arrays in UDTs. So what we simply do is create an element in SubCatagory called Parent as an Integer. Parent is a handle to an index in Catagory. Then if I need all the SubCatagories related to Catagory(Index), I would loop through each SubCatagory and see which one's Parent equals Index. The source code should make it easier to follow.

The source code contains two examples. The first example shows my way of taking care of arrays within UDT arrays. The second under "Equivelent Code" will not run under DarkBASIC but shows how another programming language might handle the same operations.

There are, ofcourse, advantages and disadvantages for each method. The major disadvantage with the DarkBASIC (my) method is that you have to itterate through an entire pool of values pertaining to all Catagories every time you need to find subcatagories. This is a bit more time consuming as we are sifting through SubCatagories that don't belong to our Catagory, but there are ways to speed up the amount of time (buffering is a good example). The other problem is that we are dealing with a fixed pool of SubCatagories. This means that you will undoubtably over or under size your SubCatagory array leading to memory waist or buffer overruns. In the grand scheme of things, generally the former is not nearly as bad as the latter. The advantage is that the number of SubCatagories per Catagory is adjustable to the needs of that Catagory. Therefore, if you have a set of SubCatagories the size of 20 elements and one Catagory has 15 SubCatagories and the other has 5, then you won't have a memory waist and a buffer overrun like you would if you had the same two Catagories with SubCatagories inside each with 10 elements each.

Whether you prefer one method over the other doesn't matter unfortunetly, because you are basically stuck with the method I've presented as DarkBASIC doesn't support arrays within UDTs. I think you will find that this method, in most situations, is not only painless, but relatively easy to manage.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem /* The Main Catagory */
Type tCatagory
  Var1 as Integer
  Var2 as Integer
EndType

Rem /* Our sub catagory (NOTE: if you need catagories of the same type, you could
Rem    just as well put a Parent in tCatagory */
Type tSubCatagory
  Var1 as Integer
  Var2 as Integer
  Rem /* This is our handle to the main catagory */
  Parent as Integer
EndType

Dim Catagory(5) as tCatagory
Dim SubCatagory(20) as tSubCatagory

Rem /* Set each sub catagory to a random catagory */
For Index = 0 to 20
  SubCatagory(Index).Parent = Rnd(5)
  SubCatagory(Index).Var1 = Index
Next Index

Rem /* Find all subcatagories of Catagory(2) */
For Index = 0 to 20
   If SubCatagory(Index).Parent = 2 Then Print SubCatagory(Index).Var1
Next Index

Rem /* ------------ Equivelent Code (Will not run) ------------ */

Type tSubCatagory
  Var1
  Var2
EndType

Type tCatagory
  Var1
  Var2
  SubCatagory(20) as tSubCatagory
EndType

Dim Catagory(5) as tCatagory

For Index = 0 to 20
  Print Catagory(2).SubCatagory(Index).Var1
Next Index