Objects and lists

BlitzMax Forums/BlitzMax Beginners Area/Objects and lists

BlitzSupport(Posted 2004) [#1]
Creating objects, adding them to a list and iterating through the list (this example is aimed mainly at existing Blitz users familiar with types and lists):

Type Vehicle
	Field name$
	Field maxspeed
	Field wheels = 4
End Type

VehicleList:TList = CreateList ()

v1:Vehicle = New Vehicle
	v1.name = "car"
	v1.maxspeed = 150
	ListAddLast VehicleList, v1

v2:Vehicle = New Vehicle
	v2.name = "motorbike"
	v2.maxspeed = 180
	v2.wheels = 2
	ListAddLast VehicleList, v2

For v:Vehicle = EachIn VehicleList
	Print "A " + v.name + " has " + v.wheels + " wheels and can travel at " + v.maxspeed + " mph."
Next


This example defines a generic 'Vehicle' object with properties describing name, maximum speed and number of wheels. Note that number of wheels is set to a default of 4 -- this can be changed once an object is created.

A list (of type TList) is created via the CreateList command, and two Vehicle objects created: one with the properties of a car and one with those of a motorbike. The motorbike example changes the number of wheels from the default 4 to 2. Each Vehicle object is added to the list via the ListAddLast command.

Finally, the program iterates through the list of vehicles, printing out the properties of each one.

There are other more 'object oriented' ways to create and manage lists -- check the sample code and documentation supplied with BlitzMax for examples.


Jeroen(Posted 2004) [#2]
This will be hard to get used to, with Blitz3D syntax stuffed in my mind!

What is the reason why BlitzMax supports more lists per object? Can you describe a situation where this is needed, or handy?


BlitzSupport(Posted 2004) [#3]
OK, there may be better examples, but I'll try: say you're writing an RTS game, where x number of players (we'll say 3) each control a bunch of tanks. You now have the option to use multiple lists using the same type definition to manage each player's tanks, like so (untested):

Type Tank
    Field x
    Field y
    Field damage
End Type

' Create 3 lists of Tank objects...

p1tanks:TList = CreateList ()
p2tanks:TList = CreateList ()
p3tanks:TList = CreateList ()

' Create 10 tanks for each player, adding to the player's list...

For a = 1 To 10
    p1:Tank = New Tank
    ListAddLast p1tanks, p1
Next

For a = 1 To 10
    p2:Tank = New Tank
    ListAddLast p2tanks, p2
Next

For a = 1 To 10
    p3:Tank = New Tank
    ListAddLast p3tanks, p3
Next

' Iterate through each player's tanks as required...

For t:Tank = EachIn p1tanks
   Print t.damage
Next

For t:Tank = EachIn p2tanks
   Print t.damage
Next

For t:Tank = EachIn p3tanks
   Print t.damage
Next


This isn't possible in previous Blitzes as all objects of a given type are automatically added to a single 'hidden' list for that type, so you'd have to add a field to track each player, and to update only 1 player's tanks you'd have to do an If/Then check on the player field.

Of course, there are other ways to go about the above, but the option's there... and there are probably better uses than this hastily whipped-up example!


AaronK(Posted 2004) [#4]
Good example. If you want another, then how about an Alive and a Dead list of units in a game. When a unit dies, remove from one list and put in the other. When you need a new unit, then you simply pop the top one from the Dead list and initialise it for use.

Aaron


BlitzSupport(Posted 2004) [#5]
It does seem like an extra layer of complication at first, but really there are only two extra steps, one of which you'll generally only do once:

· Create a list for your objects (once done, that's it);
· Add each object to the relevant list when you create it.

' You do this once...

mylist:TList = CreateList ()

' Then the only extra part on creating new objects is to add to the list...

blah:Example = New Example
ListAddLast mylist, blah



BlitzSupport(Posted 2004) [#6]
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