Going through types..

BlitzMax Forums/BlitzMax Programming/Going through types..

plash(Posted 2006) [#1]
Is there a BASIC way of iterating through all the objects created into that type, or do I have to use TLists and create another object onto the list every time I create a new type object??

This is what I mean by BASIC:
For t.type = each type
       ' stuff.
Next



AntonyWells(Posted 2006) [#2]
In b3d thats the only way you could do it, but in max, you have to do it manually.


klepto2(Posted 2006) [#3]
SuperStrict

Type TTest
 Field A:Int
End Type

GLobal MyTestList:TList = CreateList()

For Local I:Int = 0 to 99
  Local T:TTest = New TTest
  T.A = RND(1000)
  MyTestList.Addlast(T)
Next

For Local T:TTest = Eachin MyTestList
 Print T.A
Next


A Small Sample, you could more details if you check the tutorials section.


plash(Posted 2006) [#4]
In b3d thats the only way you could do it, but in max, you have to do it manually.
I know that is the only way you can do it in B3D (and all the earlier basic's).

Does anyone have an example of doing that in BlitzMax?

EDIT: Fast posters here :D


plash(Posted 2006) [#5]
Does anyone know why they changed it?


Gabriel(Posted 2006) [#6]
Does anyone know why they changed it?


Because the B3D way is horribly inflexible. You couldn't have multiple lists, you couldn't put different objects in the same list ( which would get a bit screwy with inheritance in BMax anyway. ) The BMax way is far more flexible and far more sensible.


plash(Posted 2006) [#7]
Will it hurt to put Global MyTestList:TList = CreateList() inside of the type declaration?
Example:
Type TMyType
	Global MyTestList:TList = New TList
	Field x:Int, y:Int, z:Int
End Type


And, can I get an example for removing an object from the list when the object is deleted?


Gabriel(Posted 2006) [#8]
Will it hurt to put Global MyTestList:TList = CreateList() inside of the type declaration?

No. Lots of people do it that way. I don't do it because I find it more readable to do it elsewhere, but there's nothing wrong with your method. You can put a MyTestList.AddLast(Self) in the New() method so that it's called automatically on object creation if you like.

And, can i get an example for removing the objects from the list when i delete the type?

You can't delete objects. They are deleted automatically when they go out of scope, and they won't go out of scope all the time they're in the list.

To get something similar to B3D type usage, try something like this :

Type MyType

   Global List:TList
   Field Link:TLink

   Method New()
      If List=Null
         List=New TList
      End If
      Link=List.AddLast(Self)
   End Method
   
   Method Destroy()
      Link.Remove()
   End Method
End Type


Keeping the TLink returned by the AddLast() method saves you looking through the list when you want to destroy it. Keep in mind, however, that your Destroy() method will only work if there are no other references to your object. If you add it to other lists, or put it in the fields of other objects ( parents/children for example ) then you will need to nullify those reference in your Destroy() method as well.


AntonyWells(Posted 2006) [#9]
Try something like this


type mytype

global list:TList = createlist()

method new()
list.addlast( self )
end method
method delete()
list.remove( self )
end method

end type


on each type

-Beaten by 4 seconds.


plash(Posted 2006) [#10]
How do you delete a type? (t.remove?)


.. nullify those reference ..


If I null all the fields will it remove itself?


plash(Posted 2006) [#11]
Ahh!! I get this error:
Unable to convert from 'Type' to 'Object'
when I run this code:
t.Link = List.AddLast(Self)


EDIT: I am using
Global List:TList
Field Link:TLink

as my field declarations.


bradford6(Posted 2006) [#12]
please post a runnable piece of code


plash(Posted 2006) [#13]
Heres the full type...

Edit: Problem Fixed.


Gabriel(Posted 2006) [#14]
If I null all the fields will it remove itself?


No, references to the object.

IE:

A:MyType=New MyType '<< Now A has a reference to our object.
A=Null ' << Now it doesn't. We've nulled it.


Ahh!! i get this error: Unable to convert from 'Type' to 'Object', when i run this code:

Where did t come from? Where is it declared? If you're doing it in the New() method as I suggested, the variable you're assigning the object to has not yet been declared and does not have scope anyway. You only need Link=List.AddLast(Self)


Koriolis(Posted 2006) [#15]
Does anyone know why they changed it?
There is a very good reason: BlitzMax uses a garbage collector, and Delete doesn't exist anymore. If every object was implicitely added to a global list, they would always be reachable and thus never removed from memory.
Granted, such a global list could have been kept for convenience if the garbage collector simply ignored references in that global list, but that would introduce a serious indeterminism problem : you'd never know if an object in that list would still be there the next moment, as the garbage collector could collect it just about when it wants.


plash(Posted 2006) [#16]
You only need Link=List.AddLast(Self)
Ohh.. I was putting it in after I created the object. It is fixed now, I put Link = List.AddLast(Self) inside the New() method.


plash(Posted 2006) [#17]
How can I remove an object within a type (like the Blitz command 'Delete')?


tonyg(Posted 2006) [#18]
Remove it from any list it is on and then allow any (edited from 'the') pointer to go out of scope or 'null' it.


Curtastic(Posted 2006) [#19]
To remove it from your list use:
List.Remove(object)

Object aren't within types in blitzmax. The type is just what kind of variable it is. Its like saying "local x:int" how can I remove x from within int? err you don't. Just remove it from any tlists that x is in.