Blitz2D/3D/+ like type access...

BlitzMax Forums/BlitzMax Tutorials/Blitz2D/3D/+ like type access...

Hi,

A lot of people coming from previous versions of Blitz seem to have problems grasping the concept of how BlitzMax handles types. The good news is that you can emulate oldschool Blitz behavior... It doesn't make it any simpler, but this is one way of doing it:
Type mytype
	Field _value:Int	= 0

	'
	' Global list which holds all instances
	Global _list:TList = New TList

	'
	' This adds a new instance to the global list
	Method New()
		_list.addlast(Self)
	EndMethod

	'
	' Inserts this instance after the 'other' instance
	Method InsertAfter(other:mytype)
		_list.remove(Self)
		_list.InsertAfterLink(Self,_list.FindLink(other))
	EndMethod

	'
	' Inserts this instance before the 'other' instance
	Method InsertBefore(other:mytype)
		_list.remove(Self)
		_list.InsertBeforeLink(Self,_list.FindLink(other))
	EndMethod
	
	'
	' Gets the next instance in the global list
	Method After:mytype()
		Local link:TLink = _list.FindLink(Self).NextLink()
		If link
			Return mytype(link.Value())
		Else
			Return Null
		EndIf
	EndMethod

	'
	' Gets the previous instance in the global list
	Method Before:mytype()
		Local link:TLink = _list.FindLink(Self).PrevLink()
		If link
			Return mytype(link.Value())
		Else
			Return Null
		EndIf
	EndMethod
	
	'
	' Removes this instance from the list
	Method Destroy()
		_list.remove(Self)
	EndMethod
	
	'
	' Removes all instances
	Function DestroyEach()
		_list.clear()
	EndFunction
	
	'
	' Gets the last instance in the list
	Function Last:mytype()
		Return mytype(_list.last())
	EndFunction

	'
	' Gets the first instance in the list
	Function First:mytype()
		Return mytype(_list.first())
	EndFunction
		
End Type

'
' Lets create a bunch of dummy type instances
For i = 0 To 5
	m:mytype = New mytype
	m._value = i
Next

'
' For..EachIn
Print "Going through each instance, using a For..EachIn loop"
For m:mytype = EachIn mytype._list
	Print m._value
Next
Print "---~n"

'
' InsertBefore
Print "Inserting new instance before first instance"
m:mytype = New mytype
m._value = 6
m.InsertBefore mytype.First()
For m:mytype = EachIn mytype._list
	Print m._value
Next
Print "---~n"

'
' InsertAfter
Print "Inserting new instance after first instance"
m:mytype = New mytype
m._value = 7
m.InsertAfter mytype.First()
For m:mytype = EachIn mytype._list
	Print m._value
Next
Print "---~n"

'
' While..Wend (forward)
Print "Going through each instance, using a While..Wend loop"
m:mytype = mytype.first()
While m
	Print m._value
	m = m.after()
Wend
Print "---~n"

'
' While..Wend (backward)
Print "Going Backwards through each instance, using a While..Wend loop"
m:mytype = mytype.last()
While m
	Print m._value
	m = m.before()
Wend
Print "---~n"

'
' Destroy
Print "Removing the first instance, and showing remaining instances"
m:mytype = mytype.first()
m.Destroy()
For m:mytype = EachIn mytype._list
	Print m._value
Next
Print "---~n"

'
' DestroyEach
Print "Removing each instance, and showing whats left"
mytype.DestroyEach()
For m:mytype = EachIn mytype._list
	Print m._value
Next
Print "---~n"

Print "That's all folks!"
As you can see the basic principle is to have a global list inside a type. And then when adding a new type instance (using bla:type = New type), it is automatically added to the type list.

As you will also notice is that to emulate all the oldschool Blitz commands you need quite a few methods and functions inside your type. The good part is that you will need to change very little to get it working on a new type of type.

One function/method is missing from this, namely the infamous 'Insert bla1 before/after bla2', but if you examine the code I'm sure you can add that yourself.

As mentioned at the beginning, it doesn't make it any easier to use a type, but typically you will only need a 'New()' and 'Destroy()' method...

Did that make any sense?

[Edit] Added InsertBefore + InsertAfter and tuned a bit...