types in types
Blitz3D Forums/Blitz3D Programming/types in types
| ||
I know you can make types in types, but run the code below, and you will see the problem im haveing....the comments describe it well i think.Type forest Field t.tree End Type Type tree Field treetype$ End Type Global f1.forest=New forest Global f2.forest=New forest ;make 5 trees in forest 1 and mark them as "green" For a = 1 To 5 f1\t = New tree f1\t\treetype$ = "green" Next ;make 5 more trees, but in forest 2 this time and mark them as "brown" For a = 1 To 5 f2\t = New tree f2\t\treetype$ = "brown" Next ;now to make my point when looping through the trees in forest 1, there should be no "brown" ;yet it goes through all trees.........anyone know how to go around this/or am i doing something wrong? ;should it look like this somehow in the for loop ? ;For f2\t = Each f2\t.tree ???? or ;for f2\t = Each tree in f2 For f2\t = Each tree Print f2\t\treetype$ Next WaitKey |
| ||
there is no command for this. I believe there are 4 ways to do this manually. someone should have written a guide on this |
| ||
Something like this:Type forest Field firstTree.tree Field lastTree.tree End Type Type tree Field treetype$ Field nextTree.tree End Type Global f1.forest=New forest Global f2.forest=New forest ;make 5 trees in forest 1 and mark them as "green" For a = 1 To 5 newTree.tree=New tree newTree\treetype$ = "green" If f1\firstTree=Null Then f1\firstTree=newTree f1\lastTree=newTree Else f1\lastTree\nextTree=newTree f1\lastTree=newTree EndIf Next ;make 5 more trees, but in forest 2 this time and mark them as "brown" For a = 1 To 5 newTree.tree=New tree newTree\treetype$ = "brown" If f2\firstTree=Null Then f2\firstTree=newTree f2\lastTree=newTree Else f2\lastTree\nextTree=newTree f2\lastTree=newTree EndIf Next currentTree.tree=f1\firstTree While currentTree<>Null Print currentTree\treetype$ currentTree=currentTree\nextTree Wend Print:Print currentTree.tree=f2\firstTree While currentTree<>Null Print currentTree\treetype$ currentTree=currentTree\nextTree Wend WaitKey |
| ||
You could easily achieve it like this :Type forest Field forestname End Type Type tree ; new : field treeforest.forest ; holds the forest that the tree belongs to ; end new Field treetype$ End Type Global f1.forest=New forest Global f2.forest=New forest ; new f1\forestname="green forest" f2\forestname="brown forest" ; end new ;make 5 trees in forest 1 and mark them as "green" For a = 1 To 5 ; changed : t.tree = New tree t\treetype$ = "green" ; end changed ; new t\treeforest=f1 ; end new Next ;make 5 more trees, but in forest 2 this time and mark them as "brown" For a = 1 To 5 ; changed : t.tree = New tree t\treetype$ = "brown" ; end changed ; new : t\treeforest=f2 ; end new Next ;now to make my point when looping through the trees in forest 1, there should be no "brown" ;yet it goes through all trees.........anyone know how to go around this/or am i doing something wrong? ;should it look like this somehow in the for loop ? ;For f2\t = Each f2\t.tree ???? or ;for f2\t = Each tree in f2 ; changed : For t.tree = Each tree if t\treetype$="brown" then Print t\treetype$ Next ; end changed WaitKey Or use [] arrays within the types. |
| ||
that last part should probably be:For t.tree = Each tree if t\forest=f2 then Print t\treetype$ Next |
| ||
here is the fourth way. this is kind of stupid usually...hmm..I havent tried it actuallyType forest1tree Field tree.tree End Type Type forest2tree Field tree.tree End Type Type tree Field name$ End Type Local f.forest1tree Local f2.forest2tree For a=1 To 5 f.forest1tree=New forest1tree f\tree=New tree f\tree\name="green"+a Next For a=1 To 5 f2=New forest2tree f2\tree=New tree f2\tree\name="brown"+a Next For f2=Each forest2tree Print f2\tree\name Next now someone give the \arrays[] example |
| ||
Nope,For t.tree = Each tree if t\treeforest=f2 then Print t\treetype$ Next But you're right, I was wrong. Didn't put much time in it you see :) |
| ||
Thanks for the replys guys :) LOL....storeing the next object within the current object, never would have thought of that. I guess ill go that route as i won't have to change my code any, just add to it a tad. well, aside from the for loops anyway. |
| ||
doh....1 more question, how would i clear the trees out of one of the forest's ? Set everything = null ? |
| ||
nm..... got it down now :) |
| ||
Nope, you'll have to use delete, because setting it to null will only make te variable or field point to null, but it does not destroy the current instance. Don't forget to always de FreeEntity with all entities contained by the instance, and - only when necessary - delete all child instances of other (or the same) type. |
| ||
heh, that was weird, you musta typed that while i was typein my reply..... and i gotcha, thanks again :) |
| ||
Think this is the only way to go right ?currentTree.tree=f1\firstTree While currentTree<>Null deleteMe.tree=currentTree currentTree=currentTree\nextTree Delete deleteMe Wend |
| ||
That's the correct way of deleting them :) |
| ||
This would be a good situation for SQL for Custom Types. A alternate means would be create two separate derivatives from the base tree object. Type tree Field treetype$ End Type Type greenforest Field t.tree End Type Type brownforest Field t.tree End TypeKind of a weird reverse inheritance thingy? You loop through the forest, not the tree. Of course with each derivative will have its own deletion function. Example: Function brownforestDelete(this.brownforest) If this<>Null delete this\tree delete this EndIf End FunctionIn this particular situation I dont see a need for a type within type so I would get rid of the tree or the forest and throw everything into one type, adding a field like 'typeid', 'kind', 'species' to disquingish them. Type Tree Field id% Field typeid% ;1:Green, 2:Brown End TypeSome similar to what GitTech did, but just with one type. |