Types within Types
Blitz3D Forums/Blitz3D Programming/Types within Types
| ||
Could you let me know if the way I'm doing "types within types" is the best way to manage my data. Basically I want to have a parent type, this one can have a whole bunch of child types inside. It seems that I must give each child type an id that will link it to the parent type in order to keep the child types mixing with other parents. Is this the best method? t2 value is what I want, however the "id" thing seems to be absurd, that's like adding extra stuff that shouldn't be needed. |
| ||
What you're doing is basically right. However, I would add a 'parent' field to the children, instead of a 'child' field to the parents. In this part: For p.parent = Each Parent For p\c.Child = Each Child t = t + 1 Next Exit Next Using "p\c" is redundant, as well as using "Exit". By reversing the dependancy (not sure if that is the correct term), the structure becomes more logical: Another way of doing lists is storing the First and Last element of each list. Not sure if that is more handy, but that way you don't need to iterate through all the children if you only need to acces a specific list: |
| ||
Assuming you want to include other variables within both the child and parent type .. this is what I would do ...Graphics3D 320,240, 0, 2 Global Camera=CreateCamera() Type Parent Field Stuff ;??? End Type Type Child Field Otherstuff Field Parent.Parent End Type For i = 1 To 10 p.parent = New Parent ;p\Stuff = ??? CHILDadd( p , Rand( 5, 10 ) ) Next Color 255, 255, 255 While Not KeyHit(1) RenderWorld() Parents = 0 TotalChildren = 0 For p.parent = Each Parent Parents = Parents + 1 Children = 0 For c.Child = Each Child If c\Parent = p Children = Children + 1 EndIf Next TotalChildren = TotalChildren + Children Text 0, Parents*10 , "Parent "+Parents+" has "+Children+" children" Next Text 0, 200, "Parents " + Parents Text 0, 210, "Children " + TotalChildren Flip Wend End Function CHILDadd( p.parent, NumberToAdd ) For N = 1 To NumberToAdd c.child = New child c\Parent = p Next End Function |