Help: LinkedLists with Type's
BlitzMax Forums/BlitzMax Beginners Area/Help: LinkedLists with Type's
| ||
| Can someone point me out how to create a linkedlist with my own type? Something like this... Type PLAYER Field x,y,z EndType Me:PLAYER=Createlist() |
| ||
Type PLAYER Field x,y,z EndType Me:TList = CreateList() MyPlayer:PLAYER = New PLAYER ListAddLast Me, MyPlayer |
| ||
What exactly are you trying to do? If you want a linked list of all the players then you would do:Type Player 'Caps looks like shouting Global List:TList=New TList 'Specific to type decleration, not instances Method New() 'Automatically called, its the constructor List.AddLast(self) 'Adds the created Player to the list of players EndMethod Field x#,y#,z# '# is for floating point variables End Type Me:Player=New Player 'Auto-calls new method |
| ||
| I'm trying to create a linked list where i can add players that have their own type's (x,y,z). Listadd()-> element (player x,y,z) So that later on i can parse the entire list with 'for eachin' and place them on screen according to their x,y,z, and also sort the list according to the Z-order... |
| ||
| An alternative you might want to consider (not that linked lists are wrong) is using an array of types instead of a list, and doing a little extra management yourself. Something like... Type Player Field x:Int Field y:Int Field z:Int Field AnimFrame:Int Field LifeForce:Int Field PlayerIDNumber:Int End Type Const MaxNumberOfPlayers:Int=8 Local PlayerArray:Player[MaxNumberOfPlayers] Local TotalPlayers:Int=0 'Add a player PlayerArray[TotalPlayers]=New Player PlayerArray[TotalPlayers].x=50 PlayerArray[TotalPlayers].y=100 PlayerArray[TotalPlayers].z=400 PlayerArray[TotalPlayers].AnimFrame=1 PlayerArray[TotalPlayers].LifeForce=100 PlayerArray[TotalPlayers].PlayerIDNumber=TotalPlayers TotalPlayers:+1 'Remove a player TotalPlayers:-1 Local PlayerToRemove:Int=3 'whatever number player PlayerArray[PlayerToRemove]=PlayerArray[TotalPlayers] PlayerArray[TotalPlayers]=Null 'Could do a Release or FlushMem on PlayerArray[TotalPlayers] If that suits your needs then all well and good. Couple of drawbacks though ... if you wanted to be able delete a player and then add a new player, you probably would want to add the new player with the ID number of a deleted player. To do that you may need an additional array to keep track of which player number is mapped to which entry in the PlayerArray. Also if you want to sort the players you'd have to write your own sort routine. *sorry* - maybe not suited to every application. You might also want a `mapping` array that keeps track of which player is stored at which index in the player array. |
| ||
| This *might* be what you're after and I'm not sure how well coded it is. It creates a 'mainlist' and then 2 sub-object lists. Each object appears on it's own list and the mainlist. You can do a for/eachin the mainlist for all objects (or certain sub-objects) or the sub-object lists. Imagine the mainlist is all your players...
Type objects
Field x:Int
Field y:Int
Field legs:Int
End Type
Type human Extends objects
Field name:String
Field sex:String
Global humanlist:tlist
Function create(x:Int,y:Int,legs:Int,name:String,sex:String)
If humanlist=Null humanlist=CreateList()
newhuman:human = New human
newhuman.x = x
newhuman.y = y
newhuman.legs = legs
newhuman.name = name
newhuman.sex = sex
humanlist.addlast(newhuman)
objectlist.addlast(newhuman)
End Function
End Type
Type furn Extends objects
Field material:String
Global furnlist:tlist
Function create(x:Int,y:Int,legs:Int,material:String)
If furnlist=Null furnlist = CreateList()
newfurn:furn = New furn
newfurn.x = x
newfurn.y = y
newfurn.legs = legs
newfurn.material = material
furnlist.addlast(newfurn)
objectlist.addlast(newfurn)
End Function
End Type
Global objectlist:tlist
objectlist=CreateList()
human.create(100,100,2,"Bob","Male")
human.create(100,100,2,"Jane","Female")
furn.create(100,100,4,"Wood")
Print "Number of objects in mainlist " + CountList(objectlist)
Print "Cycling through ALL objects in mainlist"
For Local s:objects=EachIn objectlist
Print s.x + " " + s.y + " " + s.legs
s.x = s.x + 1
Next
Print "Cycling only humans in mainlist"
For Local w:human = EachIn objectlist
Print w.legs
w.legs = w.legs + 1
Next
Print "Cycling all humans in humanlist"
For Local t:human= EachIn human.humanlist
Print t.name + " " + t.x + " " + t.legs
Next
Print "Cycling all furniture in furniturelist"
For Local m:furn = EachIn furn.furnlist
Print m.material + " " + m.x + " " + m.legs
Next
|