For any reason you would do that in Blitz+ or Blitz3d.
Indeed to call some update methods or whatever.
The reason for keeping a list has a more important reason though.
Consider the following:
doing this will create 2 instances of type A and store them in the variables myA1 and myA2.
Let's say you did this inside a function, this would mean that as soon as the function is finished executing, these 2 variables go 'out of scope' and they will be marked for garbage collecting. This means that the 2 instances you just created will be destroyed.
In blitz3d/Blitz+ this was not the case, because internally, b3d/b+ would add the instances you create to a list and you had to manually delete any instance when needed.
Getting the same functionality as B3D/B+ in Blitzmax requires that you add the instance to a List. This list makes sure that there is always at least 1 reference to your type instance and it will not be destroyed.
A simple way to 'automate' this is to add/remove the type instance to the List in it's Constructor/Destructors.
Note that in the example I have not used the standard Delete() Method that is normall used as a type's Destructor, but I have used a custom Dispose() Method. The reason for this is the Chicken or the Egg problem.
Delete() gets automaticly called when the object is being consumed by the Garbage Collector. The same way New() is being called when you create a new instance of your type.
But, the garbage collector only kicks in, if there is No more reference to the Type instance. So here you have the problem that Delete() will never be called, because there will always be at least 1 reference to the object stored in the list. Therefor we use a custom Dispose() method to force the object removal from the List. After this is done, Delete() will be called at some point.
Any other types you create that require maintaining a list, can simply inherit from this BaseType.
This ensures that SomeCharacterType will automaticly make use of the List and you dont have to keep track of it here as well. Remember to call SomeCharacterType.Dispose() to actually delete it if you wish to so though.
Indeed to call some update methods or whatever.
The reason for keeping a list has a more important reason though.
Consider the following:
type A field blah:int; end type local myA1:A = new A; local myA2:A = new A;
doing this will create 2 instances of type A and store them in the variables myA1 and myA2.
Let's say you did this inside a function, this would mean that as soon as the function is finished executing, these 2 variables go 'out of scope' and they will be marked for garbage collecting. This means that the 2 instances you just created will be destroyed.
In blitz3d/Blitz+ this was not the case, because internally, b3d/b+ would add the instances you create to a list and you had to manually delete any instance when needed.
Getting the same functionality as B3D/B+ in Blitzmax requires that you add the instance to a List. This list makes sure that there is always at least 1 reference to your type instance and it will not be destroyed.
A simple way to 'automate' this is to add/remove the type instance to the List in it's Constructor/Destructors.
Type MyBaseType Global List:TList = new TList; Method New() List.AddLast( Self ); '// as son as this instance is created, add it to the list. End Method Method Dispose() List.Remove( Self ); '// as soon as this instance requires being destroyed, remove its last existing reference, in the List. End method end Type
Note that in the example I have not used the standard Delete() Method that is normall used as a type's Destructor, but I have used a custom Dispose() Method. The reason for this is the Chicken or the Egg problem.
Delete() gets automaticly called when the object is being consumed by the Garbage Collector. The same way New() is being called when you create a new instance of your type.
But, the garbage collector only kicks in, if there is No more reference to the Type instance. So here you have the problem that Delete() will never be called, because there will always be at least 1 reference to the object stored in the list. Therefor we use a custom Dispose() method to force the object removal from the List. After this is done, Delete() will be called at some point.
Any other types you create that require maintaining a list, can simply inherit from this BaseType.
Type SomeCharacterType Extends MyBaseType Field Image:TImage; Method New() Image = CreateImage(....); End Method Method Dispose() '// We can even Override the Dispose method from the MyBaseType type. Image = null; Super.Dispose(); '// In order to keep the original functionality, you must manually call Super.Dispose() here though. End Method End Type
This ensures that SomeCharacterType will automaticly make use of the List and you dont have to keep track of it here as well. Remember to call SomeCharacterType.Dispose() to actually delete it if you wish to so though.