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.

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!