how to use insertafterlink?

BlitzMax Forums/BlitzMax Beginners Area/how to use insertafterlink?

I have the following code:

If mh=1
	Local m:map=New map
	m.x=cursor.x
	m.y=cursor.y
	m.z=cursor.z
	maplist.InsertAfterLink m,cursor
EndIf


cursor and m are of type map, which has a single linked list called maplist.

cursor was made earlier and resides in the map. However this throws up an error: [ERROR]: Compile Error:Unable to convert from 'map' to 'TLink'

I tried info I found elsewhere on these forums, noticably a post by dreamora, but the following snippet still doesn't compile:

'create one map entry for the cursor and make cursor handle pointer thing
'maplist.insertafterlink(m)
Local m:map=New map
Local t:tile = tile(tilelist.First())
t.selected=1
m.image=t.image
maplist.addlast(m)
cursor:map=m

Now there's a map entry called cursor which I can access. I'd like to be able to add something after the position cursor is at. I am sorting this list sometimes by Z position (it's a map editor with x,y,z hence the need for the cursor to also reside inside the map data)....

			'if click, add a new map item		
			If mh=1
				Local m:map=New map
				m.x=cursor.x
				m.y=cursor.y
				m.z=cursor.z
				maplist.InsertAfterLink m,maplist.findlink(cursor)
			EndIf


As you can see, I tried the insertafterlink stuff with cursor and a new map entry. It bombs badly.

The error when it crashes is: [ERROR]: Unhandled Exception: Attempt to access field or method of Null object

Function InsertAfter(list:TList,a:Object,b:Object)
	Local	link:TLink
	link=list.FindLink(a)
	list.InsertAfterLink(b,link)
End Function

t:TList=New TList
t.AddLast "A"
t.AddLast "B"

InsertAfter t,"A","C"

For a$=EachIn t
	Print a
Next


Thanks skid, a couple of questions if I may, as I am trying to learn blitzmax at the same time and want to understand why you are doing this.

Could someone explain how it works and why it works?

okay it still bombs with the new function. I think I need to convert cursor somehow?

cursor was defined as:

Global cursor:map = m

Any clues as to why it does an unhandled exception?

if you are adding an object after cursor in a list it will bomb if cursor has not alread been added to that list

maybe you added cursor to a different list?

the debugger is nearly there which should help you out a lot getting to grips with things but you hopefully can work out from the error message which object is null

here is a version with a little more error checking:

Function InsertAfter(list:TList,a:Object,b:Object)
	Local	link:TLink
	link=list.FindLink(a)
	Assert link Else "Can't find Object "+a.ToString()+" In List"
	list.InsertAfterLink(b,link)
End Function

t:TList=New TList
t.AddLast "A"
t.AddLast "B"

InsertAfter t,"D","C"

For a$=EachIn t
	Print a
Next


Thank you for your help, I managed to see what I was doing wrong. Blitzmax might not be impossible after all!

here are a few other LIST methods worth looking into (from an earlier thread)

' LIST METHODS
Strict
Global n$ 
Global list$
Global mylist:Tlist
Global this_func = 1
Global link_count
Global this_link:Tlink
Global total_funcs = 5
Global listfunction$
Local timer
Const WAIT_TIME = 25


Global GH = 640 ; Global GW = 480 ; Global GD = 0
Graphics( GH,GH,GD )

Global a$[] = ["one","two","three","four"]
mylist:Tlist = ListFromArray(a$)

For n$ = EachIn mylist
	list$ = list$ + n$ + " "
Next

listfunction$ = "ListFromArray"
Repeat
Cls
	timer:-1
	If timer < 0 Then timer = 0
	
	
	
	If MouseDown(1) And timer = 0
	 	changefunction()
		timer = WAIT_TIME
	EndIf
	
	SetScale 1,1
	SetColor 255,255,255
	DrawText "Click Mouse",MouseX(),MouseY()-48
	SetColor 255,255,255
	DrawText this_func,MouseX(),MouseY()-32
	SetColor 255,255,0
	DrawText listfunction$,MouseX(),MouseY()-16
	SetScale 1.5,1.5
	SetColor 255,0,0
	DrawText list$,MouseX(),MouseY()

Flip
Until KeyHit(KEY_ESCAPE)

Function changefunction()
	
	
	this_func:+1
	If this_func > total_funcs Then this_func = 1
	
	Select this_func
		Case 1
			mylist:Tlist = ListFromArray(a$)
			listfunction$ = "Mylst.ListFromArray(Array)"

		Case 2
			
			this_link:Tlink = ListFindLink(mylist,"three")
			mylist.insertbeforelink("2-1/2",this_link)
			listfunction$ = "Mylist.InsertBeforeLink(object,link--'three')"
		Case 3
			this_link:Tlink = ListFindLink(mylist,"three")
			mylist.insertafterlink("3-1/2",this_link)
			listfunction$ = "Mylist.InsertAfterLink(object,link--'three')"

		Case 4
			mylist:Tlist = ListFromArray(a$)
			listfunction$ = "Mylist.ListFromArray(Array)"

			mylist.remove("three")
			listfunction$ = "Mylist.remove('three')"

		Case 5
			mylist:Tlist = ListFromArray(a$)
			link_count = mylist.count()
			listfunction$ = "Mylist.count() = "+link_count

			
			

	End Select
	
	list$ = ""
	For n$ = EachIn mylist
			list$ = list$ + n$ + " "
	Next


End Function


Blitzmax might not be impossible after all!

Which would be just as well, considering a whole language was built on it ;]