Types within types issues

Blitz3D Forums/Blitz3D Beginners Area/Types within types issues

I think part of the problem is illustrated by the way you have named your types. The child object is called "children", but a better name would be "child". Since although the entire collection of objects of this type can be called a collection of children, each object in this collection is still an individual "child".

When you create an object of type "child", this is only one object. It is not a collection of objects. The same is true for a field inside another type: the "children" field in your parent type is really just one single child object.

I see you repeatedly create a new child and assign it to this "children" field in the parent type. However, that way what you are doing is simply replacing the previously created child with a new one: the "children" field will contain only the last object you assigned to it.

There is also a misunderstanding of the "For Each" command. This command will always visit *every* object of a given class, no matter when or where in your program each object was created. When you have created a number of "children" for one parent, and also a number of such objects for another parent, and also a few spare children; using "For Each Children" you will visit all of them one by one. This explains the result you got. (But it is also caused by the point I made above.)

So if you want to create a list of children for every parent, you need more than just one "child" field in the parent type. You need a list of child objects.

Maybe I am going to fast now, but you can create such a list by means of a "linked list" of child objects. For this, you add to the child type a field called "nextChild", and this should be of the type "Children". Now when you assign a child to a parent, you simply assign it to its Children field. Then when you want to assign a second child to the same parent, you assign the second child to the "nextChild" field of the first child. This way you are building a linked list of children objects.

Here's how the child type would look:

Type CHILD
   Field name$ 
   Field nextChild.CHILD
End Type

Type parents 
   Field name$ 
   Field child.CHILD
End Type


I didn't change the name "parents" but "parent" would be better (or "PARENT", I personally like to use all-uppercase names for types). You can now change the rest of the code to use the "linked list" idea:

;parent steve 
parent.parents = New parents 
parent\name$="steve" 

parent\child = New CHILD
parent\child\name$="steve/julie" 

parent\child\nextChild = New CHILD
parent\child\nextChild\name$="steve/justin" 

parent\child\nextChild\nextChild = Null

;parent bob 
parent.parents = New parents 
parent\name$="bob" 

parent\child = New CHILD 
parent\child\name$="bob/mike" 

parent\child\nextChild = New CHILD 
parent\child\nextChild\name$="bob/stacie" 

parent\child\nextChild\nextChild = Null


As you can see, with the second child, instead of saying "\child = new CHILD", it says "\child\nextChild = new Child". So a new CHILD is created and assigned to the "nextChild" field of the first child, building a linked list of two such objects.

Also note how the linked lists are closed off by assigning "Null" to the nextChild field of the last child in the list. "Null" means there is no object.

To print out the linked list of child objects for every parent, you have to walk through this linked list. For this you can use a temporary CHILD variable that you point at the objects in the list one by one, until you reach the end of the list.

For parent = Each parents 
   Print parent\name$ 

   tempChild.CHILD = parent\child

   While tempChild <> NULL
      Print " "+tempChild\name
      tempChild = tempChild\nextChild
   Wend

Next


I assign the first child the parent has to the tempChild variable. If it is not NULL, I print its name. Then, the nextChild is assigned to the temporary variable. This goes on until at some moment the variable is equal to NULL (meaning there is no object).

Maybe this linked list idea is all a bit heavy to start with but this (or similar techniques) is something you would need to get the kind of program you were aiming for.

edit: or an array, or a bank, as Surreal suggests ;)

The linked list method is great when you don't know in advance how many children you will need for each parent, you can just keep adding them.

By the way, they are even more handy when you also write a set of functions for handling these lists, as obviously the above example is a bit cumbersome; you have to keep repeating "nextChild\nextChild" etcetera while in reality you wouldn't know in advance how many there are; this would have to be done in a more automatic way by a function that decides which child is the last in the list, adding a new child behind that one.