Objects and lists

BlitzMax Forums/BlitzMax Beginners Area/Objects and lists

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

If you want to mimick previous Blitz lists, it's easy enough once you know how: just copy and paste the New method and type-specific global (a variable that's shared by all objects of this type) from the example below...

Type Rocket

    Global MyList:TList

    Method New ()
        If MyList = Null Then MyList = CreateList ()
        ListAddLast MyList, Self
    End Method

    ' Add the rest of your type definition here as normal...

End Type


This creates a global list that all objects of this type can access. When you do this...

r:Rocket = New Rocket


... the New method is automatically called, which is handy for initialising stuff. In this case, it checks whether the list, MyList, has had a list assigned to it yet, and if not it creates one (it's done this way because you can only pre-define a type-specific global as a constant). Creation of a list for this type will only happen once, as once the list is assigned to MyList, that line will be skipped.

The next line adds the 'current' object (the one being created) to the list -- Self refers to the object calling the method, ie. the one being created in this case.

You can then iterate through the list like so, using the type name and the list name to access it:

For r:Rocket = EachIn Rocket.MyList
    ' Whatever...
Next