TLists
BlitzMax Forums/BlitzMax Beginners Area/TLists
| ||
| Can anyone point me to a REALLY GOOD tutorial on using Lists? I've seen about 1000 examples of how to use the For/Eachin routine to make particles, but I'd like to know more about using Lists in a random access/Database sort of way. Let's say I have 100 TCar Objects, and I put them in a List. How do I selectively look through them to find a particluar TCar based on it's Make and Model fields? How do I edit the Cost field of just that one TCar? How do I delete that TCar from the List if it's been sold? After a lifetime of using Arrays, I can see the benefits of Lists but am having trouble adapting my thinking to use them. |
| ||
| This is pretty good... Wave Tutorial and these *might* help...
Graphics 640,480,0
mylist:TList=CreateList()
Type mytype
Field count:Int
End Type
For Local x = 1 To 50
s:mytype = New mytype
s.count=x
ListAddLast mylist,s
Next
my_firstlink:TLink = mylist.firstlink()
currentlink:TLink = my_firstlink
s:mytype = mytype(my_firstlink.value())
While Not KeyHit(key_escape)
Cls
Select mykey
Case key_left
my_firstlink:TLink = mylist.firstlink()
currentlink:TLink = my_firstlink
s:mytype = mytype(my_firstlink.value())
DrawText "my_firstlink:TLink = mylist.firstlink() ; s:mytype = mytype(my_firstlink.value())",0,0
DrawText "my_firstlink count field = " + s.count, 0,10
Case key_right
my_lastlink:TLink = mylist.lastlink()
currentlink:TLink = my_lastlink
s:mytype = mytype(my_lastlink.value())
DrawText "my_lastlink:TLink = mylist.lastlink() ; s:mytype = mytype(my_lastlink.value())",0,0
DrawText "my_lastlink count field = " + s.count, 0,10
Case key_up
my_prevlink:TLink = currentlink.prevlink()
If my_prevlink <> Null
currentlink:TLink = my_prevlink
s:mytype = mytype(my_prevlink.value())
DrawText "my_prevlink:TLink = mylist.prevlink() ; s:mytype = mytype(my_prevlink.value())",0,0
DrawText "my_prevlink count field = " + s.count, 0,10
Else
DrawText "No prevlink. Current position : " + s.count,0,0
EndIf
Case key_down
my_nextlink:TLink = currentlink.nextlink()
If my_nextlink <> Null
currentlink:TLink = my_nextlink
s:mytype = mytype(my_nextlink.value())
DrawText "my_nextlink:TLink = mylist.nextlink() ; s:mytype = mytype(my_nextlink.value())",0,0
DrawText "my_nextlink count field = " + s.count, 0,10
Else
DrawText "No nextlink : Current position : " + s.count,0,0
EndIf
Default
DrawText "Invalid Selection. Current position " + s.count,0,0
End Select
Flip
FlushMem
mykey=WaitKey()
If mykey=27 Exit
Wend
' 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.InsertBeforeLink(object,link--'three')"
Case 4
mylist:TList = ListFromArray(a$)
listfunction$ = "Mylist.ListFromArray(Array)"
mylist.remove("three")
listfunction$ = "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
The first was me messing about and I think the second was from this site somewhere. <edit> and some more...
Graphics 640,480,0
Global tanknumbers = 10
Type tank
Global TankList:TList
Field x#,y#
Field dir%,armor% =100
Field speed# = 0.2, size% = 25
Global Tanknumber = 0
Function create()
If TankList = Null TankList = CreateList()
Local newtank:tank
newTank = New Tank
NewTank.Armor = 150 + Rand(1,5)*10 'Set a random Armor 10,20,30,40 or 50
NewTank.X = Rand(5,800) ;NewTank.Y = Rand(5,600)'Random Start Location
NewTank.Dir = Rand(0,360)
ListAddLast TankList,NewTank ' Put this tank called NewTank into our TankList
Tank.TankNumber:+1 'Add to the number of tanks
End Function
Function my_remove()
If Not ListIsEmpty(tanklist)
RemoveLink(tanklist.lastlink())
tank.tanknumber=tank.tanknumber-1
EndIf
End Function
Method draw()
DrawRect(X,Y,Size,Size)
X:+speed*Cos(Dir)
Y:+Speed*Sin(Dir)
End Method
End Type
For nr = 1 To tanknumbers
tank.create()
Next
While Not KeyDown(Key_Escape)
If KeyHit(key_space) tank.my_remove()
If MouseHit(1) tank.create()
For T:Tank = EachIn tank.TankList 'Local T is declared to hold the current Tank in this loop
t.draw
Next 'This loop will loop for every Tank that was added to TankList
DrawText "Number of Tanks : "+Tank.TankNumber,20,20
DrawText MemAlloced(),20,40
Flip ;Cls
FlushMem
Wend
End
my_list:TList=CreateList() Type my_type Field my_num End Type For x = 5 To 11 f:my_Type = New my_Type f.my_num=x ListAddLast my_list,f Next currentLink:TLink=my_list.firstlink() Repeat currenttype:my_type = my_type(currentlink.value()) If currenttype.my_num=9 ' This is the value/field you're looking for. my_next_link:TLink=currentlink.nextlink() my_prev_link:TLink=currentlink.prevlink() my_next_type:my_type = my_type(my_next_link.value()) my_next_type.my_num=999 my_prev_type:my_type = my_type(my_prev_link.value()) my_prev_type.my_num=888 currentlink:TLink=currentlink.nextlink() Else currentlink:TLink=currentlink.nextlink() EndIf Until currentlink=Null For a:my_type = EachIn my_list Print a.my_num Next and to finish off... TLISTS Not *really good* but it's a start. |
| ||
| This is what I needed! Thanks very much Tonyg. |
| ||
| I suspect a database module would be more appropriate... :) *hint, hint* *nudge, nudge* |