Going through types..

BlitzMax Forums/BlitzMax Programming/Going through types..

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


In b3d thats the only way you could do it, but in max, you have to do it manually.

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.

In b3d thats the only way you could do it, but in max, you have to do it manually.


I know thats the only way you can do it in B3D (and all the earlier basic's), does anyone have an example of doing this:
For t.type = each type
       ' stuff.
Next

in Blitz Max?

Edit: :/ fast posters here :D

Does anyone know why they changed it?

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.

Will it hurt to put Global MyTestList:TList = CreateList() inside of the type declaration?
Example:
type mytype
    Global MyTestList:TList = CreateList()
     field x, y, z
end type


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

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.

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.

How do you delete a type? (t.remove?)


.. nullify those reference ..


If I null all the fields will it remove itself?

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 the field declarations.

please post a runnable piece of code

Heres the full type...

Edit: Problem Fixed.

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)

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.

You only need Link=List.AddLast(Self)


Ohh.. i was putting it in after i created the object.. its fixed now, i put Link=List.AddLast(Self) inside a new() method.

How can i remove an object within a type? (Like Blitz commands delete)

Remove it from any list it is on and then allow any (edited from 'the') pointer to go out of scope or 'null' it.

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.