Creating a list in a type (OOP)

BlitzMax Forums/BlitzMax Beginners Area/Creating a list in a type (OOP)

The r0nin(Posted 2005) [#1]
I have several types which I would like to keep track of using lists. Since I'm trying to be modular and OOP, I'm trying to segregate the creation and maintenance of the list within the type and file. However, unless I declare the list as a Global variable in the main program loop/file, I'm getting errors. I'm trying to do something like the following:
Type creature
     Field x#,y#
     Field name:String = Null

  Function Create(a#,b#,temp_name:string)
     temp_creature:creature = new creature
     temp_creature.x = a
     temp_creature.y = b
     temp_creature.name = temp_name
     If CreatureList = Null Then Global CreatureList:TList =  new TList
     ListAddLast(CreatureList, temp_creature)
     return temp_creature
  End Function
End Type

I get an error with this because the first time through the IF statement is trying to access something that doesn't exist. I've also tried it with "If (not CreatureList)" too, but it still doesn't work. Do I declare the Global list in the type as a field (but wouldn't that create a new list with each new creature)? Any help would be appreciated!


JazzieB(Posted 2005) [#2]
Type alien
  Field x,y
  Global alienList:TList

  Function Create()
    If alienList=Null Then alienList=CreateList()
    a:alien=New alien
    alienList.AddLast(a)
    a.x=Rand(0,640)
    a.y=Rand(0,480)
    Return a
  End Function
End Type

Basically, have the global within the Type and check whether it's null or not when creating a new type.


The r0nin(Posted 2005) [#3]
Thanks!